一般是多表一起插入数据的时候使用
例如,一个表记录用户信息,另一个表记录用户所对应的权限,两个表均使用用户表的ID来关联
你新添加用户的时候,肯定需要一次性插入用户表和权限表这两个表。你插入完用户表后,ID字段的值才是已知的,这个时候在存储过程中就可以写成
INSERT INTO 用户表() VALUES ()
DECLARE @ID AS INT
SELECT @ID = @@Identity
INSERT INTO 权限表 (UserID, ……) VALUES (@ID, ……)
执行完插入语句后,再select @@identity 获取新增ID值
别担心在你插入后别人再插入你提取的会是别人的值.
@@identity 只返回你的这第链接上升成的最新递增值,根别人没关系
CREATE DATABASE folder
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
use folder;
set NAMES gb2312;
//desc和数据库关键字有冲突
create table t_dep
(
id int auto_increment not null primary key,
name varchar(40) not null,
pass varchar(40) not null,
depdes varchar(40)
);
create table t_box
(
id int auto_increment not null primary key,
depid int not null,
boxname varchar(40) not null,
boxdesc varchar(40)
);
create table t_folder
(
id int auto_increment not null primary key,
boxid int not null,
foldername varchar(40) not null,
folderdesc varchar(40)
);
create table t_type
(
id int auto_increment not null primary key,
folderid int not null,
typename varchar(40) not null,
typedesc varchar(40)
);
create table t_form
(
id int auto_increment not null primary key,
typeid int not null,
formname varchar(40) not null,
formdesc varchar(40),
formimage varchar(100)
);
22.常用插入、修改、删除语句
插入记录:insert into teacher values('','glchengang','深圳一中','1976-10-10');
修改记录:update mytable set single=′y′ where name=′abccs′;
删除记录:delete from mytable where name=′abc′;
MySQL级联
首先,目前在产品环境可用的MySQL版本(指4.0.x和4.1.x)中,只有InnoDB引擎才允许使用外键,所以,我们的数据表必须使用InnoDB引擎。
下面,我们先创建以下测试用数据库表:
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的文档;
好,接着我们再来插入测试数据:
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');
我们先看一下当前数据表的状态:
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的相关子纪录是否会自动删除:
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中利用外键实现级联删除成功!
---------------------------------------------------------------------------------------------------------------
user表:
create table user
(
userid integer not null auto_increment primary key,
username varchar(12) not null
)
type=innodb;
password表:
create table password
(
userid integer not null,
password varchar(12) not null,
index (userid),
foreign key (userid) references user (userid)
on delete cascade
on update cascade
)
type=innodb;
1、MySQL支持外键约束,并提供与其它DB相同的功能,但表类型必须为 InnoDB
2、建外键的表的那个列要加上index.