MySQL: 获取约束名称并将其放在一条语句中
MySQL: Get constraint name and drop it in one statement
我正在从以下位置获取约束名称:
SELECT
constraint_name
FROM
information_schema.REFERENTIAL_CONSTRAINTS
WHERE
constraint_schema = 'db' AND table_name = 'table';
然后将其从 table 中删除。它在本地机器上手动完成时有效。现在它正在扰乱我的 Jenkins 构建的数据库迁移脚本。
有没有办法在一条语句中做到这一点?
Update
Following query gave me Access denied for user root@localhost
DELETE
FROM
information_schema.REFERENTIAL_CONSTRAINTS
WHERE
constraint_schema = 'db' AND table_name = 'table';
自动生成下降约束为 运行:
SELECT concat('ALTER TABLE ', TABLE_NAME, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';') as theString
FROM information_schema.key_column_usage
WHERE CONSTRAINT_SCHEMA = 'so_gibberish'
AND referenced_table_name IS NOT NULL;
+---------------------------------------------------+
| theString |
+---------------------------------------------------+
| ALTER TABLE fc_junction DROP FOREIGN KEY fc_cat; |
| ALTER TABLE fc_junction DROP FOREIGN KEY fc_food; |
+---------------------------------------------------+
它会自动生成 运行 的字符串。如有必要,按摩订单。然后你走吧。请注意,在 Workbench/sqlyog 中,您当然不会有 table 栏输出。
我正在从以下位置获取约束名称:
SELECT
constraint_name
FROM
information_schema.REFERENTIAL_CONSTRAINTS
WHERE
constraint_schema = 'db' AND table_name = 'table';
然后将其从 table 中删除。它在本地机器上手动完成时有效。现在它正在扰乱我的 Jenkins 构建的数据库迁移脚本。
有没有办法在一条语句中做到这一点?
Update
Following query gave me Access denied for user root@localhost
DELETE
FROM
information_schema.REFERENTIAL_CONSTRAINTS
WHERE
constraint_schema = 'db' AND table_name = 'table';
自动生成下降约束为 运行:
SELECT concat('ALTER TABLE ', TABLE_NAME, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';') as theString
FROM information_schema.key_column_usage
WHERE CONSTRAINT_SCHEMA = 'so_gibberish'
AND referenced_table_name IS NOT NULL;
+---------------------------------------------------+
| theString |
+---------------------------------------------------+
| ALTER TABLE fc_junction DROP FOREIGN KEY fc_cat; |
| ALTER TABLE fc_junction DROP FOREIGN KEY fc_food; |
+---------------------------------------------------+
它会自动生成 运行 的字符串。如有必要,按摩订单。然后你走吧。请注意,在 Workbench/sqlyog 中,您当然不会有 table 栏输出。