您如何获取列是否是MySQL中的主键?
要获取列是否为主键,请使用COLUMN_NAME和COLUMN_KEY='PRI'。这样,整个语法如下:
select column_name, case when column_key= 'PRI' then 'yourMessage1' else ''yourMessage2' end as anyAliasName from information_schema.columns where table_schema =database() and `table_name` = yourTableName order by `table_name`, ordinal_position;
要了解上述语法,让我们创建一个表-
mysql> create table DemoTable1886 ( Id int NOT NULL, FirstName varchar(20), LastName varchar(20), Age int, DateOfBirth datetime, Education varchar(40), PRIMARY KEY(Id) );
这是获取特定列是否为主键的查询-
mysql> select column_name, case when column_key= 'PRI' then 'This is a Primary key Column' else 'This is not a Primary key Column' end as Output from information_schema.columns where table_schema =database() and `table_name` = 'DemoTable1886' order by `table_name`, ordinal_position;
这将产生以下输出-
+-------------+--------------------------------+ | COLUMN_NAME | Output | +-------------+--------------------------------+ | Id | This is a Primary key Column | | FirstName |This is not a Primary key Column| | LastName |This is not a Primary key Column| | Age |This is not a Primary key Column| | DateOfBirth |This is not a Primary key Column| | Education |This is not a Primary key Column| +-------------+--------------------------------+ 6 rows in set (0.00 sec)
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短