Mysql刪除所有外來鍵約束

2021-10-18 05:52:16 字數 677 閱讀 8294

由於外來鍵約束的存在,使得後期的分庫分表非常麻煩,對於沒有用到外來鍵約束限制操作,並且有外來鍵結構的資料庫,刪除所有外來鍵約束很有必要,使資料庫可移植性強,可拆解性強

執行查詢所有外來鍵命令,並且拼接刪除外來鍵的sql

複製所有sql,並執行

select concat(

'alter table '

,table_schema,

'.',table_name,

' drop foreign key '

,constraint_name,

' ;'

)from information_schema.table_constraints c

where c.table_schema=

'庫名'

and c.constraint_type=

'foreign key'

;# 查詢的結果複製到下方繼續執行

# 例如:

複製如上命令執行即可

mysql約束與外來鍵 MySQL 外來鍵與約束

外來鍵的建立 建表如下 create table parent id int not null,primary key id type innodb create table child id int,parent id int,foreign key parent id references pa...

MySQL外來鍵約束建立及刪除

建立外來鍵定義 create table categories category id tinyint 3 unsigned not null auto increment,name varchar 30 not null,primary key category id engine innodb ...

MySQL 外來鍵約束

建立測試主表.id 是主鍵.create table test main id int,value varchar 10 primary key id 建立測試子表.create table test sub id int,main id int,value varchar 10 primary k...