如何生成SQL表结构作为文本表?

时间:2013-11-29 07:14:41

标签: mysql sql database structure

何时将MySQL表结构导出为文本版本表?

我的意思是这样的:

+-----------+-----------+------+-----+-------------------+-----------------------------+
| Field     | Type      | Null | Key | Default           | Extra                       |
+-----------+-----------+------+-----+-------------------+-----------------------------+
| EID       | int(11)   | NO   | PRI | 0                 |                             |
| MOD_EID   | int(11)   | YES  |     | NULL              |                             |
| EXIT_TIME | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+-----------+------+-----+-------------------+-----------------------------+

我确信有一些工具可以像这样导出表格。有谁知道如何从MySQL做到这一点?

4 个答案:

答案 0 :(得分:2)

你可以通过3种方式实现这一目标。

  • DESC:易用性
  • SHOW CREATE TABLE:使用另一个表的相同架构轻松创建新表
  • information_schema:难以使用,但功能强大。

<强> 1。使用DESCRIBE

DESC $DB_NAME.$TBL_NAME;

sample output
mysql> DESC jsheo_test.test;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name   | varchar(10) | YES  |     | NULL    |       |
| age    | int(11)     | YES  |     | NULL    |       |
| spent  | int(11)     | YES  |     | NULL    |       |
| gender | char(1)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

<强> 2。使用SHOW CREATE TABLE

SHOW CREATE TABLE $DB_NAME.$TBL_NAME; 

sample output
mysql> SHOW CREATE TABLE jsheo_test.test\G
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `name` varchar(10) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `spent` int(11) DEFAULT NULL,
  `gender` char(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

第3。使用信息架构

SELECT  TABLE_NAME
   , COLUMN_NAME 
   , ORDINAL_POSITION
   , DATA_TYPE
   , IS_NULLABLE 
FROM    INFORMATION_SCHEMA.COLUMNS 
WHERE   TABLE_SCHEMA = '$DB_NAME'
   AND TABLE_NAME = '$TBL_NAME'
ORDER BY TABLE_NAME, ORDINAL_POSITION;

sample output
+------------+-------------+------------------+-----------+-------------+
| TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | DATA_TYPE | IS_NULLABLE |
+------------+-------------+------------------+-----------+-------------+
| test       | name        |                1 | varchar   | YES         |
| test       | age         |                2 | int       | YES         |
| test       | spent       |                3 | int       | YES         |
| test       | gender      |                4 | char      | YES         |
+------------+-------------+------------------+-----------+-------------+

<强> 4。来自shell

运行使用-e选项

$  mysql -uusername -S ~/tmp/mysql.sock -e "DESC jsheo_test.test"
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name   | varchar(10) | YES  |     | NULL    |       |
| age    | int(11)     | YES  |     | NULL    |       |
| spent  | int(11)     | YES  |     | NULL    |       |
| gender | char(1)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

否则输出格式很奇怪。如下所示。

$ echo "desc jsheo_test.test;" | mysql -uusername -S /tmp/mysql.sock
Field   Type    Null    Key     Default Extra
name    varchar(10)     YES             NULL
age     int(11) YES             NULL
spent   int(11) YES             NULL
gender  char(1) YES             NULL

答案 1 :(得分:2)

您可以使用以下命令选择一些字段并将它们存储在OUTFILE中:

  

SELECT * FROM table_name INTO OUTFILE' textile.txt'

答案 2 :(得分:1)

mysql命令行工具允许您使用命令

执行此操作
desc tablename;

答案 3 :(得分:1)

你可以去

$>SELECT * FROM TABLE_NAME
INTO OUTFILE '/PATH/TO/TEXTFILE.TXT'

OR

$>mysql -h<mysqlhostname> -u<username> -p <databasename> -e "SELECT * FROM TABLE_NAME" > myfile.txt

替换SQL。