È possibile in qualche modo ottenere la struttura del database MySQL o solo una tabella con una query semplice?
O c'è un altro modo, come posso farlo?
È possibile in qualche modo ottenere la struttura del database MySQL o solo una tabella con una query semplice?
O c'è un altro modo, come posso farlo?
Risposte:
Penso che quello che cerchi sia DESCRIBE
DESCRIBE table;
Puoi anche usare SHOW TABLES
SHOW TABLES;
per ottenere un elenco delle tabelle nel database.
Per ottenere l'intera struttura del database come un insieme di istruzioni CREATE TABLE , utilizzare mysqldump :
mysqldump database_name --compact --no-data
Per le singole tabelle, aggiungi il nome della tabella dopo il nome db in mysqldump. Ottieni gli stessi risultati con SQL e SHOW CREATE TABLE :
SHOW CREATE TABLE table;
O DESCRIVERE se si preferisce un elenco di colonne:
DESCRIBE table;
show create tableera esattamente quello che stavo cercando. Grazie!
Dai un'occhiata a INFORMATION_SCHEMA. TABLEStavolo. Contiene metadati su tutte le tue tabelle.
Esempio:
SELECT * FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE TABLE_NAME LIKE 'table1'
Il vantaggio di questo rispetto ad altri metodi è che puoi facilmente utilizzare query come quella sopra come sottoquery nelle altre tue query.
information_schema. columns(usando columnstable invece di tables? Perché tablesnon contiene alcuna informazione su quali tipi sono le colonne della tabella
usando questo:
SHOW CREATE TABLE `users`;
ti darà il DDL per quella tabella
DESCRIBE `users`
elencherà le colonne in quella tabella
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME ='products';
dove Table_schemaè il nome del database
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_db_name' AND TABLE_NAME ='users';
Questa è la query SHOW CREATE TABLE . Puoi anche interrogare le TABELLE SCHEMA .
SHOW CREATE TABLE YourTableName;
Una variante della prima risposta che ho trovato utile
Apri il prompt dei comandi e inserisci (non è necessario accedere al server mysql)
mysqldump -hlocalhost -u<root> -p<password> <dbname> --compact --no-data > </path_to_mydump/>mysql.dmp
SELEZIONA COLUMN_NAME
DA INFORMATION_SCHEMA. COLUMNS
WHERE TABLE_SCHEMA= 'bodb' AND TABLE_NAME= 'abc';
funziona per ottenere tutti i nomi di colonna
Nel seguente esempio,
playgroundè il nome del database edequipmentè il nome della tabella
Un altro modo è usare SHOW-COLUMNS: 5.5 (disponibile anche per 5.5>)
$ mysql -uroot -p<password> -h<host> -P<port> -e \
"SHOW COLUMNS FROM playground.equipment"
E l'output:
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| type | varchar(50) | YES | | NULL | |
| quant | int(11) | YES | | NULL | |
| color | varchar(25) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
Si può anche usare mysqlshow-client (disponibile anche per 5.5>) come il seguente:
$ mysqlshow -uroot -p<password> -h<host> -P<port> \
playground equipment
E l'output:
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: playground Table: equipment
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int(11) | | NO | PRI | | auto_increment | select,insert,update,references | |
| type | varchar(50) | latin1_swedish_ci | YES | | | | select,insert,update,references | |
| quant | int(11) | | YES | | | | select,insert,update,references | |
| color | varchar(25) | latin1_swedish_ci | YES | | | | select,insert,update,references | |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
SHOW TABLES FROM database_name