中國站
幫助中心 > 數(shù)據(jù)庫 > 云數(shù)據(jù)庫MySQL > 最佳實踐 > 為無主鍵表添加主鍵

為無主鍵表添加主鍵

查看無主鍵表

執(zhí)行如下命令,查看是否有無主鍵表。

  1. select table_schema,table_name from information_schema.tables
  2. where (table_schema,table_name) not in(
  3. select distinct table_schema,table_name from information_schema.columns where COLUMN_KEY='PRI'
  4. )
  5. and table_schema not in (
  6. 'sys','mysql','information_schema','performance_schema'
  7. );

查找無主鍵表

添加主鍵

添加隱式主鍵

  1. 執(zhí)行如下命令,查看參數(shù)implicit_primary_key的值是否為ON。

    1. show global variables like 'implicit_primary_key';

    查看隱式主鍵是否啟用

  2. 執(zhí)行如下命令,修改無主鍵表。

    1. alter table <表名> engine=innodb;

直接添加主鍵

執(zhí)行如下SQL語句,為無主鍵表添加主鍵。

  1. ALTER TABLE <表名> ADD PRIMARY KEY (<需要設為主鍵的列名>);