Jak pokazać schemat tabeli w bazie danych MySQL?

Z konsoli MySQL, jakie polecenie wyświetla schemat danej tabeli?

Author: Brian Tompsett - 汤莱恩, 2009-09-30

5 answers

describe [db_name.]table_name;

Dla sformatowanego wyjścia lub

show create table [db_name.]table_name;

Dla instrukcji SQL, która może być użyta do utworzenia tabeli.

 405
Author: Omry Yadan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-28 19:44:50
SHOW CREATE TABLE yourTable;

Lub

SHOW COLUMNS FROM yourTable;
 89
Author: Bobby,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-24 06:57:17

Możesz również użyć skrótu do opisu jako desc do opisu tabeli.

Desc [db_name.]table_name;

Lub

Use db_name;
desc table_name;

Możesz również użyć explain do opisu tabeli.

Wyjaśnij [db_name.]table_name;

Zobacz oficjalny dokument

Da wyjście takie jak:

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(10)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| age      | int(10)     | YES  |     | NULL    |       |
| sex      | varchar(10) | YES  |     | NULL    |       |
| sal      | int(10)     | YES  |     | NULL    |       |
| location | varchar(20) | YES  |     | Pune    |       |
+----------+-------------+------+-----+---------+-------+
 12
Author: Somnath Muluk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-03 12:23:01
SELECT COLUMN_NAME, TABLE_NAME,table_schema
FROM INFORMATION_SCHEMA.COLUMNS;
 5
Author: Lam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-14 10:31:49

Być może pytanie powinno być nieco bardziej precyzyjne, co jest wymagane, ponieważ można czytać na dwa różne sposoby. tj.

  1. Jak uzyskać strukturę / definicję tabeli w mysql?
  2. Jak uzyskać nazwę schematu/bazy danych, w której znajduje się ta tabela?

Biorąc pod uwagę przyjętą odpowiedź, OP wyraźnie zamierzał ją zinterpretować w pierwszy sposób. Dla każdego, kto czyta pytanie w drugą stronę spróbuj

SELECT `table_schema` 
FROM `information_schema`.`tables` 
WHERE `table_name` = 'whatever';
 5
Author: Paul Campbell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-10 11:33:46