MySQL创建外键
CREATE TABLE Members (
Member_ID INT NOT NULL,
PRIMARY KEY AUTO_INCREMENT,
First_Name VARCHAR(30) NOT NULL,
Last_Name VARCHAR(30) NOT NULL,
Bday DATE NOT NULL,
Gender ENUM('M', 'F') DEFAULT 'F'
);
CREATE TABLE Email (
Email VARCHAR(50),
Member_ID INT NOT NULL,
PRIMARY KEY (Email, Member_ID),
CONSTRAINT FOREIGN KEY (Member_ID) REFERENCES Members(Member_ID)
ON DELETE RESTRICT ON UPDATE CASCADE
);
在 MySQL 中只有 InnoDB 类型的表才支持外键与存储过程
所有要建立外键的字段必须建立索引
首先,目前在产品环境可用的MySQL 版本(指4.0.x 和4.1.x )中,只有InnoDB 引擎才允许使用外键,所以,我们的数据表必须使用InnoDB 引擎。
下面,我们先创建以下测试用数据库表:
CODE:[Copy to clipboard]CREATE TABLE `roottb` (
`id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL,
`data` VARCHAR(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `subtb` (
`id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL,
`rootid` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`data` VARCHAR(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
INDEX (`rootid`),
FOREIGN KEY (`rootid`) REFERENCES roottb(`id`) ON DELETE CASCADE
) TYPE=InnoDB;
注意:
1 、必须使用InnoDB 引擎;
2 、外键必须建立索引(INDEX );
3 、外键绑定关系这里使用了“ ON DELETE CASCADE” ,意思是如果外键对应数据被删除,将关联数据完全删除,更多信息请参考MySQL 手册中关于InnoDB 的文档;
好,接着我们再来插入测试数据:
CODE:[Copy to clipboard]INSERT INTO `roottb` (`id`,`data`)
VALUES ('1', 'test root line 1'),
('2', 'test root line 2'),
('3', 'test root line 3');
INSERT INTO `subtb` (`id`,`rootid`,`data`)
VALUES ('1', '1', 'test sub line 1 for root 1'),
('2', '1', 'test sub line 2 for root 1'),
('3', '1', 'test sub line 3 for root 1'),
('4', '2', 'test sub line 1 for root 2'),
('5', '2', 'test sub line 2 for root 2'),
('6', '2', 'test sub line 3 for root 2'),
('7', '3', 'test sub line 1 for root 3'),
('8', '3', 'test sub line 2 for root 3'),
('9', '3', 'test sub line 3 for root 3');
我们先看一下当前数据表的状态:
CODE:[Copy to clipboard]mysql>; show tables;
+----------------+
| Tables_in_test |
+----------------+
| roottb |
| subtb |
+----------------+
2 rows in set (0.00 sec)
mysql>; select * from `roottb`;
+----+------------------+
| id | data |
+----+------------------+
| 1 | test root line 1 |
| 2 | test root line 2 |
| 3 | test root line 3 |
+----+------------------+
3 rows in set (0.05 sec)
mysql>; select * from `subtb`;
+----+--------+----------------------------+
| id | rootid | data |
+----+--------+----------------------------+
| 1 | 1 | test sub line 1 for root 1 |
| 2 | 1 | test sub line 2 for root 1 |
| 3 | 1 | test sub line 3 for root 1 |
| 4 | 2 | test sub line 1 for root 2 |
| 5 | 2 | test sub line 2 for root 2 |
| 6 | 2 | test sub line 3 for root 2 |
| 7 | 3 | test sub line 1 for root 3 |
| 8 | 3 | test sub line 2 for root 3 |
| 9 | 3 | test sub line 3 for root 3 |
+----+--------+----------------------------+
9 rows in set (0.01 sec)
嗯,一切都正常,好,下面我们要试验我们的级联删除功能了。
我们将只删除roottb 表中id 为2 的数据记录,看看subtb 表中rootid 为2 的相关子纪录是否会自动删除:
CODE:[Copy to clipboard]mysql>; delete from `roottb` where `id`='2';
Query OK, 1 row affected (0.03 sec)
mysql>; select * from `roottb`;
+----+------------------+
| id | data |
+----+------------------+
| 1 | test root line 1 |
| 3 | test root line 3 |
+----+------------------+
2 rows in set (0.00 sec)
mysql>; select * from `subtb`;
+----+--------+----------------------------+
| id | rootid | data |
+----+--------+----------------------------+
| 1 | 1 | test sub line 1 for root 1 |
| 2 | 1 | test sub line 2 for root 1 |
| 3 | 1 | test sub line 3 for root 1 |
| 7 | 3 | test sub line 1 for root 3 |
| 8 | 3 | test sub line 2 for root 3 |
| 9 | 3 | test sub line 3 for root 3 |
+----+--------+----------------------------+
6 rows in set (0.01 sec)
嗯,看subtb 表中对应数据确实自动删除了,测试成功。
结论:在MySQL 中利用外键实现级联删除成功!
mysql 创建外键 sql 语句
2008-4-8 10:52
提问者: cly_apple | 浏览次数: 3048 次
create table UserInfo(
User_nID
int
not null,
-- identity
User_sNo
varchar(50)
null,
--
编号
User_sName
varchar(50)
null,
--
名称
User_sEmail
varchar(200)
null,
--
邮件
primary key (User_nID) -- 设置 主键为 User_nID
)
create table OrderInfo(
Order_nID
int
not null,
-- identity
Order_UsernID
int
null,
-- user_nID FK
Order_sNo
varchar(50)
null,
--
编号
Order_sPName
varchar(50)
null,
--
项目名称
primary key (Order_nID) -- 设置 主键为 User_nID
)
俩表联系 怎么给 OrderInfo
创建外键???
输入内容已经达到长度限制
还能输入 9999 字
插入图片 删除图片 插入地图 删除地图 插入视频 视频地图
参考资料: 提交回答 取消
2008-4-8 10:56
满意回答
alter table OrderInfo
add constraint FK3C0469F99FC6869
foreign key (userID)
references UserInfo;
FK3C0469F99FC6869
这个是外键名称,自己起
或者
ALTER TABLE bbsReply ADD CONSTRAINT FK_bbsReply_bbsSection FOREIGN KEY
(RSID)
REFERENCES bbsSection (SID),