CREATE TABLE customer(
id int( 11 ) NOT NULL AUTO_INCREMENT ,
name varchar( 255 ) ,
lastname varchar( 255 ) ,
age int( 11 ),
PRIMARY KEY (id)
) TYPE=INNODB;
CREATE TABLE book(
id int( 11 ) NOT NULL AUTO_INCREMENT ,
title varchar( 255 ) ,
author varchar( 255 ) ,
customer_fk int( 11 ),
available TINYINT NOT NULL,
PRIMARY KEY ( id ),--这里只是定义主索引
INDEX (customer_fk), --定义索引,且它一定是int或integer类型
CONSTRAINT book_customer FOREIGN KEY (customer_fk) REFERENCES customer(id) --外键必须是外表主键,这里是参照customer的id
)TYPE=INNODB;
以上就已经成功定义了外键,下面是级联删除!
--constraint约束
-- ALTER TABLE book ADD CONSTRAINT book_customer FOREIGN KEY ( customer_fk ) REFERENCES customer( id ) ON UPDATE RESTRICT ON DELETE RESTRICT ;--级联删除
posted on 2009-03-04 18:38
fl1429 阅读(1344)
评论(0) 编辑 收藏 所属分类:
Mysql/Sqlserver/Oracle