열이 기본 키인지 확인하려면 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) ); Query OK, 0 rows affected (0.00 sec)
다음은 특정 열이 기본 키인지 여부를 확인하는 쿼리입니다. -
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)