Di seguito sono riportati quattro modi per elencare le viste in un database MySQL utilizzando SQL o la riga di comando.
Il SHOW TABLES Comando
Il SHOW TABLES il comando elenca il non-TEMPORARY tabelle, sequenze e viste in un dato database MySQL. Possiamo usare il WHERE clausola per restringerla alle sole visualizzazioni.
Possiamo anche usare il FULL modificatore per restituire una seconda colonna che mostra il tipo:
SHOW FULL TABLES
WHERE Table_Type LIKE 'VIEW'; Risultato:
+-----------------+------------+ | Tables_in_music | Table_type | +-----------------+------------+ | valbumsartists | VIEW | | valbumsgenres | VIEW | | vallalbums | VIEW | | vallartists | VIEW | | vallgenres | VIEW | +-----------------+------------+
Omettendo il WHERE la clausola restituisce tutti i tipi:
SHOW FULL TABLES; Risultato:
+-----------------+------------+ | Tables_in_music | Table_type | +-----------------+------------+ | Albums | BASE TABLE | | Artists | BASE TABLE | | Genres | BASE TABLE | | valbumsartists | VIEW | | valbumsgenres | VIEW | | vallalbums | VIEW | | vallartists | VIEW | | vallgenres | VIEW | +-----------------+------------+
Lo SHOW TABLE STATUS Comando
In MySQL, SHOW TABLE STATUS il comando è simile a SHOW TABLES comando ma fornisce informazioni più estese su ciascuno (non-TEMPORARY ) tabella.
Esempio:
SHOW TABLE STATUS; Risultato:
+----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+ | Albums | InnoDB | 10 | Dynamic | 20 | 819 | 16384 | 0 | 32768 | 0 | 21 | 2021-11-13 12:56:02 | 2021-11-13 12:56:13 | NULL | utf8mb4_0900_ai_ci | NULL | | | | Artists | InnoDB | 10 | Dynamic | 16 | 1024 | 16384 | 0 | 0 | 0 | 17 | 2021-11-13 12:56:02 | 2021-11-13 12:56:13 | NULL | utf8mb4_0900_ai_ci | NULL | | | | Genres | InnoDB | 10 | Dynamic | 8 | 2048 | 16384 | 0 | 0 | 0 | 9 | 2021-11-13 12:56:02 | 2021-11-13 12:56:13 | NULL | utf8mb4_0900_ai_ci | NULL | | | | valbumsartists | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:02:24 | NULL | NULL | NULL | NULL | NULL | VIEW | | valbumsgenres | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:09:47 | NULL | NULL | NULL | NULL | NULL | VIEW | | vallalbums | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:12:51 | NULL | NULL | NULL | NULL | NULL | VIEW | | vallartists | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:13:02 | NULL | NULL | NULL | NULL | NULL | VIEW | | vallgenres | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:12:36 | NULL | NULL | NULL | NULL | NULL | VIEW | +----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
Accetta anche un WHERE e LIKE clausola nel caso in cui desideri restringere i risultati.
Il information_schema.TABLES Tabella
Possiamo anche interrogare il information_schema.TABLES tabella:
SELECT
TABLE_SCHEMA,
TABLE_NAME,
TABLE_TYPE
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA LIKE 'music' AND TABLE_TYPE LIKE 'VIEW'; Risultato:
+--------------+----------------+------------+ | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | +--------------+----------------+------------+ | Music | valbumsartists | VIEW | | Music | valbumsgenres | VIEW | | Music | vallgenres | VIEW | | Music | vallalbums | VIEW | | Music | vallartists | VIEW | +--------------+----------------+------------+
In questo caso, ho restituito tutte le visualizzazioni dal database chiamato music . Interrogazione senza filtrare i risultati per TABLE_SCHEMA restituisce visualizzazioni da tutti i database. Allo stesso modo, interrogandolo senza filtrare per TABLE_TYPE restituisce tutti i tipi di tabella.
Il mysqlshow Cliente
Un altro modo per farlo è con mysqlshow utilità.
Per utilizzare questa opzione, apri un prompt della riga di comando/finestra del terminale ed esegui quanto segue:
mysqlshow --user root --password music;
Assicurati di sostituire music con il database che ti interessa e root con l'utente applicabile. Il --password bit fa sì che all'utente venga richiesta la password.
Risultato:
Enter password: Database: music +----------------+ | Tables | +----------------+ | Albums | | Artists | | Genres | | valbumsartists | | valbumsgenres | | vallalbums | | vallartists | | vallgenres | +----------------+
Il mysqlshow il cliente restituisce viste e tabelle.
L'output mostra solo i nomi di quei database, tabelle o colonne per i quali l'utente dispone di alcuni privilegi.
Se non viene fornito alcun database, vengono visualizzati tutti i database corrispondenti. Se non viene fornita alcuna tabella, vengono visualizzate tutte le tabelle corrispondenti nel database. Se non viene fornita alcuna colonna, vengono visualizzate tutte le colonne e i tipi di colonna corrispondenti nella tabella.