首页 > 数据库 > mysql教程 > MySQL如何识别某个列是否存在于所有表中?

MySQL如何识别某个列是否存在于所有表中?

王林
发布: 2023-09-01 22:57:03
转载
964 人浏览过

MySQL如何识别某个列是否存在于所有表中?

要识别列名,请在MySQL中使用INFORMATION_SCHEMA.COLUMNS。以下是语法 -

select table_name,column_name
from INFORMATION_SCHEMA.COLUMNS
where table_schema = SCHEMA()
andcolumn_name='anyColumnName';
登录后复制

Let us implement the above query in order to identify a column with its existence in all tables. Here, we are finding the existence of column EmployeeAge −

mysql> select table_name,column_name
   FROM INFORMATION_SCHEMA.COLUMNS
   WHERE table_schema = SCHEMA()
   AND column_name='EmployeeAge';
登录后复制

This will produce the following output displaying the tables with specific column “EmployeeAge” −

+---------------+-------------+
| TABLE_NAME    | COLUMN_NAME |
+---------------+-------------+
| demotable1153 | EmployeeAge |
| demotable1297 | EmployeeAge |
| demotable1303 | EmployeeAge |
| demotable1328 | EmployeeAge |
| demotable1378 | EmployeeAge |
| demotable1530 | EmployeeAge |
| demotable1559 | EmployeeAge |
| demotable1586 | EmployeeAge |
| demotable1798 | EmployeeAge |
| demotable1901 | EmployeeAge |
| demotable511  | EmployeeAge |
| demotable912  | EmployeeAge |
+---------------+-------------+
12 rows in set (0.00 sec)
登录后复制

为了证明,让我们检查上述任何一个表的描述 −

mysql> desc demotable1153;
登录后复制

这将产生以下输出,显示在demotable1153中存在EmployeeAge列 −

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| EmployeeId   | int(11)     | NO   | PRI | NULL    | auto_increment |
| EmployeeName | varchar(40) | YES  | MUL | NULL    |                |
| EmployeeAge  | int(11)     | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
登录后复制

以上是MySQL如何识别某个列是否存在于所有表中?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板