MySQL删除外键定义的方法

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

不知道大家有没有发现,在定义外键的时候articles.member_id外键比articles.category_id子句多了一个CONSTRAINT fk_member ?
这个fk_member就是用来实现MySQL删除外键用的,如下所示:

 
  
  1. mysql> ALTER TABLE articles DROP FOREIGN KEY fk_member;  
  2. Query OK, 1 row affected (0.25 sec)  
  3. Records: 1    Duplicates: 0    Warnings: 0 

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

 
  
  1. mysql> SHOW CREATE TABLE articles;  
  2. +———-+————————————+  
  3. | Table      | Create Table                         |  
  4. +———-+————————————+  
  5. | articles | CREATE TABLE `articles` (  
  6.     `article_id` int(11) unsigned NOT NULL auto_increment,  
  7.     `category_id` tinyint(3) unsigned NOT NULL,  
  8.     `member_id` int(11) unsigned NOT NULL,  
  9.     `title` varchar(255) NOT NULL,  
  10.     PRIMARY KEY    (`article_id`),  
  11.     KEY `category_id` (`category_id`),  
  12.     KEY `member_id` (`member_id`),  
  13.     CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)  
  14. ENGINE=InnoDB DEFAULT CHARSET=latin1            |  
  15. +———-+————————————+  
  16. 1 row in set (0.01 sec)  
  17.  

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

  1. mysql> ALTER TABLE articles DROP FOREIGN KEY articles_ibfk_1;  注意大小写
  2. Query OK, 1 row affected (0.66 sec)  
  3. Records: 1    Duplicates: 0    Warnings: 0 
  4. mysql> ALTER TABLEarticles DROP column member_id;
  5. --------------------------------------------------------------------------
  6. 删除索引

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)
mysql alter 语句用法,添加、修改、删除字段等


 

//主键549830479

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

//增加一个新列549830479

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

//删除列549830479

   alter table t2 drop column c;

//重命名列549830479

   alter table t1 change a b integer;


//改变列的类型549830479

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

//重命名表549830479

   alter table t1 rename t2;

加索引549830479

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

加主关键字的索引549830479

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

加唯一限制条件的索引549830479

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

删除某个索引549830479

   mysql>alter table tablename drop index emp_name;

修改表:549830479

增加字段:549830479

   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;

删除字段:549830479

   mysql> ALTER TABLE table_name DROP field_name; 

 

 

你可能感兴趣的:(Mysql)