MYSQL 5.5 删除主键

发布于 2025-01-04 20:23:26 字数 1849 浏览 1 评论 0原文

我正在将quartz.net版本从1.0.3升级到2.0.2 有一个数据库模式的迁移脚本,它是为 MSSQL 编写的,我正在尝试编写它的 MYSQL 版本。

但是,我无法删除主键(我需要这样做)。

脚本的原始 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;

为简单起见,我正在尝试其中的第一个语句

ALTER TABLE BLOB_TRIGGERS DROP CONSTRAINT BLOB_TRIGGERS_PKEY;

以下是我尝试过的内容和结果:

  • ALTER TABLE BLOB_TRIGGERS DROP PRIMARY KEY;

[Err] 1025 - 将“.\quartz_local#sql-df8_9”重命名为“.\quartz_local\BLOB_TRIGGERS”时出错(errno:150)

  • ALTER TABLE BLOB_TRIGGERS DROP INDEX 'PRIMARY';

[Err] 1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“PRIMARY”附近使用的正确语法

  • ALTER TABLE BLOB_TRIGGERS DROP INDEX PRIMARY;

[Err] 1025 - 将“.\quartz_local#sql-df8_9”重命名为“.\quartz_local\BLOB_TRIGGERS”时出错(errno:150)

  • ALTER TABLE BLOB_TRIGGERS DROP PRIMARY KEY;

[Err] 1025 - 将“.\quartz_local#sql-df8_9”重命名为“.\quartz_local\BLOB_TRIGGERS”时出错(errno:150)

我的 Mysql 版本是 5.5.16

编辑:要检查索引: MYSQL 显示索引结果

EDIT2:根据请求外键: 创建表sql

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.

However, I haven't been able to drop primary keys (which I need to).

Original MSSQL version of script:

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;

[Err] 1025 - Error on rename of '.\quartz_local#sql-df8_9' to '.\quartz_local\BLOB_TRIGGERS' (errno: 150)

  • ALTER TABLE BLOB_TRIGGERS DROP INDEX 'PRIMARY';

[Err] 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 ''PRIMARY'' at line 1

  • 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)

  • 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)

My Mysql version is 5.5.16

EDIT: To check the indexes:
MYSQL Show index results

EDIT2: Foreign keys on request:
Create table sql

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青萝楚歌 2025-01-11 20:23:26

(errno: 150) 是赠品:这意味着外键定义问题。我怀疑其他一些表具有取决于此 PK 的外键约束,因此您需要先删除它,然后再重建它。

编辑:通过您发布的图像,这一点变得更加清晰:

从 BLOBS_TRIGGERS 到 TRIGGERS 的 FK 是由 PK 组成的。因此,如果你放弃 PK,约束就会变得陈旧。您需要删除并稍后重新创建约束。

(errno: 150) is the giveaway: This means Foreign key definition problem. I suspect some other table has a foreign key constraint depending this PK, so you need to drop that first and rebuild it later.

Edit: With the images you posted, this becomes clearer:

The FK from BLOBS_TRIGGERS to TRIGGERS is made up from the PK. So if you drop the PK, the contraint becomes stale. You need to drop and later recreate the constraint.

洒一地阳光 2025-01-11 20:23:26

经过简短的谷歌搜索后,我很确定错误消息有点误导。似乎有很多 ALTER TABLE 语句可能会导致该错误消息。

我会检查是否有对此表的外键引用。

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.

I'd check to see if there are foreign key references to this table.

我也只是我 2025-01-11 20:23:26

我也有同样的问题。删除表中的外键没有帮助。没有其他表引用具有我试图删除的主键的表。我最终通过使用 mysqldump 将表导出到 ASCII 文件解决了这个问题。然后我编辑该文件以将主键更改为我想要的主键,然后使用 mysql 命令行界面重新导入。

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. I then edited the file to change the primary key to one I wanted, then I reimported using the mysql command line interface.

野稚 2025-01-11 20:23:26

ALTER TABLE 表名 DROP PRIMARY KEY;

ALTER TABLE tablename DROP PRIMARY KEY;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文