mysql什么叫删除定义_MySQL删除外键定义的方法

MySQL外键在定以后,如果我们不再需要这个外键,可以进行删除操作,下面就为您介绍MySQL删除外键定义的方法,供您参考。

不知道大家有没有发现,在定义外键的时候articles.member_id外键比articles.category_id子句多了一个CONSTRAINT fk_member ?

这个fk_member就是用来实现MySQL删除外键用的,如下所示:

mysql>ALTER TABLE articles DROP FOREIGN KEY fk_member;

Query OK, 1 row affected (0.25 sec)

Records: 1    Duplicates: 0    Warnings: 0

这样articles.member_id外键定义就被删除了,但是如果定义时没有指定CONSTRAINT fk_symbol (即外键符号)时该怎么实现MySQL删除外键呢?别急,没有指定时,MySQL会自己创建一个,可以通过以下命令查看:

mysql>SHOW CREATE TABLE articles;

+———-+————————————+

| Table      | Create Table                         |

+———-+————————————+

| articles | CREATE TABLE `articles` (

`article_id` int(11) unsigned NOT NULL auto_increment,

`category_id` tinyint(3) unsigned NOT NULL,

`member_id` int(11) unsigned NOT NULL,

`title` varchar(255) NOT NULL,

PRIMARY KEY    (`article_id`),

KEY `category_id` (`category_id`),

KEY `member_id` (`member_id`),

CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)

)ENGINE=InnoDBDEFAULTCHARSET=latin1|

+———-+————————————+

1 row in set (0.01 sec)

可以看出articles.category_id的外键符号为articles_ibfk_1,因为就可以执行以下命令实现MySQL删除外键定义:

mysql>ALTER TABLE articles DROP FOREIGN KEY articles_ibfk_1;  注意大小写

Query OK, 1 row affected (0.66 sec)

Records: 1    Duplicates: 0    Warnings: 0

mysql> ALTER TABLEarticles DROP column member_id;

--------------------------------------------------------------------------

删除索引

show index 发现有和外键同名的索引存在

mysql> show index from CORE_FUNCTION;

+---------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |

+---------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

| CORE_FUNCTION | 0 | PRIMARY | 1 | FUNCTION_ID | A | 0 | NULL | NULL | | BTREE | |

| CORE_FUNCTION | 1 | FKDB8410785D1C928B | 1 | PARENT | A | 0 | NULL | NULL | YES | BTREE | |

+---------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

2 rows in set (0.01 sec)

删除掉索引

mysql> alter table CORE_FUNCTION drop index FKDB8410785D1C928B;

Query OK, 0 rows affected (0.02 sec)

Records: 0 Duplicates: 0 Warnings: 0

再show keys看,外键不见了

mysql> show keys from CORE_FUNCTION;

+---------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |

+---------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

| CORE_FUNCTION | 0 | PRIMARY | 1 | FUNCTION_ID | A | 0 | NULL | NULL | | BTREE | |

+---------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

1 row in set (0.00 sec)

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);

alter table t2 add d timestamp;

alter table infos add ex tinyint not null default '0';

alter table t2 drop column c;

alter table t1 change a b integer;

alter table t1 change b b bigint not null;

alter table infos change list list tinyint not null default '0';

alter table t1 rename t2;

mysql> alter table tablename change depno depno int(5) not null;

mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);

mysql> alter table tablename add index emp_name (name);

mysql> alter table tablename add primary key(id);

加唯一限制条件的索引549830479

mysql> alter table tablename add unique emp_name2(cardnumber);

mysql>alter table tablename drop index emp_name;

mysql> ALTER TABLE table_name ADD field_name field_type;

修改原字段名称及类型:549830479

mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

mysql> ALTER TABLE table_name DROP field_name;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值