简体   繁体   English

MYSQL 5.5删除主键

[英]MYSQL 5.5 Drop Primary Key

I am upgrading my quartz.net version from 1.0.3 to 2.0.2 There is a migration script for database schema, which was was written for MSSQL, and I am trying to write a MYSQL version of it. 我正在将我的quartz.net版本从1.0.3升级到2.0.2。有一个用于数据库模式的迁移脚本,它是为MSSQL编写的,我正在尝试编写它的MYSQL版本。

However, I haven't been able to drop primary keys (which I need to). 但是,我无法删除主键(我需要)。

Original MSSQL version of script: 原始MSSQL版脚本:

ALTER TABLE BLOB_TRIGGERS DROP CONSTRAINT BLOB_TRIGGERS_PKEY;
ALTER TABLE BLOB_TRIGGERS DROP CONSTRAINT BLOB_TRIGGERS_TRIGGER_NAME_FKEY;
ALTER TABLE SIMPLE_TRIGGERS DROP CONSTRAINT PK_SIMPLE_TRIGGERS;
ALTER TABLE SIMPLE_TRIGGERS DROP CONSTRAINT FK_SIMPLE_TRIGGERS_TRIGGERS;
ALTER TABLE CRON_TRIGGERS DROP CONSTRAINT PK_CRON_TRIGGERS;
ALTER TABLE CRON_TRIGGERS DROP CONSTRAINT FK_CRON_TRIGGERS_TRIGGERS;
ALTER TABLE TRIGGERS DROP CONSTRAINT PK_TRIGGERS;
ALTER TABLE TRIGGERS DROP CONSTRAINT FK_TRIGGERS_JOB_DETAILS;
ALTER TABLE JOB_DETAILS DROP CONSTRAINT PK_JOB_DETAILS;

For simplicity, I am trying the first statement there 为简单起见,我在那里尝试第一个声明

ALTER TABLE BLOB_TRIGGERS DROP CONSTRAINT BLOB_TRIGGERS_PKEY;

Here are what I have tried and results: 以下是我尝试过的结果:

  • ALTER TABLE BLOB_TRIGGERS DROP PRIMARY KEY; ALTER TABLE BLOB_TRIGGERS DROP PRIMARY KEY;

[Err] 1025 - Error on rename of '.\\quartz_local#sql-df8_9' to '.\\quartz_local\\BLOB_TRIGGERS' (errno: 150) [错误] 1025 - 将'。\\ quartz_local#sql-df8_9'重命名为'。\\ quartz_local \\ BLOB_TRIGGERS'时出错(错误号:150)

  • ALTER TABLE BLOB_TRIGGERS DROP INDEX 'PRIMARY'; ALTER TABLE BLOB_TRIGGERS DROP INDEX'PRIMARY';

[Err] 1064 - You have an error in your SQL syntax; [错误] 1064 - 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''PRIMARY'' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行的''PRIMARY'附近使用正确的语法

  • ALTER TABLE BLOB_TRIGGERS DROP INDEX PRIMARY ; ALTER TABLE BLOB_TRIGGERS DROP INDEX PRIMARY ;

[Err] 1025 - Error on rename of '.\\quartz_local#sql-df8_9' to '.\\quartz_local\\BLOB_TRIGGERS' (errno: 150) [错误] 1025 - 将'。\\ quartz_local#sql-df8_9'重命名为'。\\ quartz_local \\ BLOB_TRIGGERS'时出错(错误号:150)

  • ALTER TABLE BLOB_TRIGGERS DROP PRIMARY KEY; ALTER TABLE BLOB_TRIGGERS DROP PRIMARY KEY;

[Err] 1025 - Error on rename of '.\\quartz_local#sql-df8_9' to '.\\quartz_local\\BLOB_TRIGGERS' (errno: 150) [错误] 1025 - 将'。\\ quartz_local#sql-df8_9'重命名为'。\\ quartz_local \\ BLOB_TRIGGERS'时出错(错误号:150)

My Mysql version is 5.5.16 我的Mysql版本是5.5.16

EDIT: To check the indexes: 编辑:检查索引: MYSQL显示索引结果

EDIT2: Foreign keys on request: EDIT2:请求外键: 创建表sql

(errno: 150) is the giveaway: This means Foreign key definition problem. (errno: 150)是赠品:这意味着外键定义问题。 I suspect some other table has a foreign key constraint depending this PK, so you need to drop that first and rebuild it later. 我怀疑某些其他表具有依赖于此PK的外键约束,因此您需要先删除它并稍后重建它。

Edit: With the images you posted, this becomes clearer: 编辑:使用您发布的图像,这变得更加清晰:

The FK from BLOBS_TRIGGERS to TRIGGERS is made up from the PK. 从BLOBS_TRIGGERS到TRIGGERS的FK由PK组成。 So if you drop the PK, the contraint becomes stale. 因此,如果你放弃PK,那么约束就会变得陈旧。 You need to drop and later recreate the constraint. 您需要删除并稍后重新创建约束。

After brief Googling, I'm pretty sure the error message is a little misleading. 经过简短的谷歌搜索,我很确定错误信息有点误导。 There seem to be a lot of ALTER TABLE statements that might result in that error message. 似乎有很多ALTER TABLE语句可能导致该错误消息。

I'd check to see if there are foreign key references to this table. 我要检查是否有对该表的外键引用。

I had the same problem. 我有同样的问题。 Deleting foreign keys in the table did not help. 删除表中的外键没有帮助。 There were no other tables referencing the one that had the primary key I was trying to drop. 没有其他表引用具有我试图删除的主键的表。 I finally solved the problem by using mysqldump to export the table to an ASCII file. 我终于通过使用mysqldump将表导出到ASCII文件来解决问题。 I then edited the file to change the primary key to one I wanted, then I reimported using the mysql command line interface. 然后我编辑了文件以将主键更改为我想要的主键,然后使用mysql命令行界面重新导入。

ALTER TABLE tablename DROP PRIMARY KEY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM