登录
首页 >  数据库 >  MySQL

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

来源:tutorialspoint

时间:2023-08-30 09:30:35 430浏览 收藏

学习数据库要努力,但是不要急!今天的这篇文章《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)

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。

声明:本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>