【问题标题】:How to delete composite primary key如何删除复合主键
【发布时间】:2014-03-16 12:23:37
【问题描述】:

我有表 Friends_list 我想删除它的复合主键(friend_of_id,friends_id_is)并想创建一个新的复合主键(friend_of_aid,friends_aid)。

为了删除主键,我编写了查询

ALTER TABLE friends_list  DROP PRIMARY KEY

但这显示以下错误

#1025 - Error on rename of '.\xrcwrn_sms\#sql-14d4_e0' to '.\xrcwrn_sms\friends_list' (errno: 150)

我正在关注this post,但对于复合主键,这不起作用 我的表结构图片如下

【问题讨论】:

    标签: mysql


    【解决方案1】:

    检查此表上的外键,例如使用下一个查询 -

    SELECT
      *
    FROM information_schema.REFERENTIAL_CONSTRAINTS
      WHERE CONSTRAINT_SCHEMA = 'db name' AND REFERENCED_TABLE_NAME = 'your table';
    

    您需要重新创建所有这些外键:

    1. 删除外键
    2. 重新创建主密钥
    3. 创建外键

    【讨论】:

    • 我尝试查询显示错误#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCED_TABLE_NAME = friends_list` LIMIT 0, 30' at line 4`
    • 在条件之间添加 AND 关键字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    相关资源
    最近更新 更多