中国站
帮助中心 > 数据库 > 云数据库MySQL > 最佳实践 > 为无主键表添加主键

查看无主键表

执行如下命令,查看是否有无主键表。

  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. 执行如下命令,查看参数implicit_primary_key的值是否为ON

    1. show global variables like 'implicit_primary_key';

    查看隐式主键是否启用

  2. 执行如下命令,修改无主键表。

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

直接添加主键

执行如下SQL语句,为无主键表添加主键。

  1. ALTER TABLE <表名> ADD PRIMARY KEY (<需要设为主键的列名>);
咨询电话

400-100-2938

售前咨询
小张
点击立即咨询
小虞
点击立即咨询
小黄
点击立即咨询
小李
点击立即咨询
小温
点击立即咨询
小来
点击立即咨询
小易
点击立即咨询
小杨
点击立即咨询
小罗
点击立即咨询
15902059193
3007326032
点击立即咨询

添加客服小罗微信

添加客服小罗QQ

售后服务
400-100-2938
800811969
点击立即咨询

售后微信服务号

售后企业QQ

提供7*24小时全天候不间断的售后服务