如何識別任何 MySQL 資料庫表的複合主鍵?
你可以使用聚合函式 count(*)。如果它返回的值大於 1,那就意味著表有複合主鍵。
我們首先建立一個表 −
mysql> create table DemoTable1324 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentAge int, -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
以下是新增複合主鍵的查詢 −
mysql> alter table DemoTable1324 ADD CONSTRAINT constr_IdAgeCountry PRIMARY KEY (StudentId, StudentAge,StudentCountryName); Query OK, 0 rows affected (1.29 sec) Records: 0 Duplicates: 0 Warnings: 0
以下是識別任何 MySQL 資料庫表中複合主鍵的查詢 −
mysql> select count(*) AS Total -> from information_schema.KEY_COLUMN_USAGE -> where table_name='DemoTable1324' and table_schema=database();
這將生成以下輸出 −
+-------+ | Total | +-------+ | 3 | +-------+ 1 row in set, 2 warnings (0.76 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP