如果给定一个列名,如何找出 MySQL 数据库中包含该列的表?


使用 COLUMN_NAME 找出数据库中包含特定列的表。我们首先创建一个表 −

mysql> create table DemoTable
   -> (
   -> CustomerId int,
   -> CustomerName varchar(20),
   -> CustomerCountryName varchar(100)
   -> );
Query OK, 0 rows affected (1.05 sec)

以下查询查找特定列 “'CustomerCountryName'” 存在于哪些表中 −

mysql> select *from information_schema.columns WHERE COLUMN_NAME =
'CustomerCountryName';

输出

这将产生以下输出 −

+---------------+--------------+--------------+---------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+--------------+------------+-------+---------------------------------+----------------+-----------------------+-------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME  | COLUMN_NAME          |ORDINAL_POSITION  | COLUMN_DEFAULT | IS_NULLABLE | DATA_TYPE |CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH |NUMERIC_PRECISION | UMERIC_SCALE | DATETIME_PRECISION |CHARACTER_SET_NAME | COLLATION_NAME | COLUMN_TYPE | COLUMN_KEY | EXTRA | PRIVILEGES | COLUMN_COMMENT | GENERATION_EXPRESSION | SRS_ID |
+---------------+--------------+--------------+---------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+--------------+------------+-------+---------------------------------+----------------+-----------------------+-------+
| def | sample | demotable189 | CustomerCountryName | 4 | NULL |YES | varchar | 100 | 300 | NULL | NULL | NULL | utf8 | utf8_general_ci | varchar(100) | | |select,insert,update,references | | | NULL |
| def | web | DemoTable | CustomerCountryName | 4 | NULL |YES | varchar | 20 | 60 | NULL | NULL |NULL | utf8 | utf8_unicode_ci | varchar(20) | | |select,insert,update,references | | | NULL |
| def | web | DemoTable | CustomerCountryName | 3 | NULL |YES | varchar | 100 | 300 | NULL | NULL |NULL | utf8 | utf8_unicode_ci | varchar(100) | | |select,insert,update,references | | | NULL |
+---------------+--------------+--------------+---------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+--------------+------------+-------+---------------------------------+----------------+-----------------------+-------+
3 rows in set (0.68 sec)

以上内容显示,列 “CustomerCountryName” 存在于 3 个表中。

更新于: 30-6 月 -2020

116 次查看

开启您的 职业生涯

通过完成课程获得认证

开始
广告