mysql外键索引

来源:互联网 发布:抓取访客手机号源码 编辑:程序博客网 时间:2024/06/17 21:38

mysql在创建外键的时候会自动添加索引,oracle就不会,这样避免了死锁的产生,提高查询效率

mysql> create table f(id int primary key);
Query OK, 0 rows affected (0.05 sec)


mysql> create table c(id int , foreign key(id) references f(id));
Query OK, 0 rows affected (0.02 sec)


mysql> show index from c;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+--------
-------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_c
omment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+--------
-------+
| c     |          1 | id       |            1 | id          | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |
       |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+--------
-------+
1 row in set (0.00 sec)


mysql> drop index id on c;
ERROR 1553 (HY000): Cannot drop index 'id': needed in a foreign key constraint
mysql>

0 0