drop table if exists `auth_assignment`;
drop table if exists `auth_item_child`;
drop table if exists `auth_item`;
drop table if exists `auth_rule`;
create table `auth_rule`
(
`name` varchar(64) not null,
`data` text,
`created_at` integer,
`updated_at` integer,
primary key (`name`)
) engine InnoDB;
create table `auth_item`
(
`name` varchar(64) not null,
`type` integer not null,
`description` text,
`rule_name` varchar(64),
`data` text,
`created_at` integer,
`updated_at` integer,
primary key (`name`),
foreign key (`rule_name`) references `auth_rule` (`name`) on delete set null on update cascade,
key `type` (`type`)
) engine InnoDB;
create table `auth_item_child`
(
`parent` varchar(64) not null,
`child` varchar(64) not null,
primary key (`parent`, `child`),
foreign key (`parent`) references `auth_item` (`name`) on delete cascade on update cascade,
foreign key (`child`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;
create table `auth_assignment`
(
`item_name` varchar(64) not null,
`user_id` varchar(64) not null,
`created_at` integer,
primary key (`item_name`, `user_id`),
foreign key (`item_name`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;
auth_rule表没做任何关联,可直接插入
INSERT INTO auth_rule VALUE('rule_name1','rule_data',0,0);
INSERT INTO auth_rule VALUE('rule_name2','rule_data',0,0);
INSERT INTO auth_rule VALUE('rule_name3','rule_data',0,0);
插入auth_item表
INSERT INTO auth_item VALUE('item_name1',1,'item_description','rule_name1','data',0,0);
INSERT INTO auth_item VALUE('item_name2',1,'item_description','rule_name2','data',0,0);
INSERT INTO auth_item VALUE('item_name3',1,'item_description','rule_name3','data',0,0);
INSERT INTO auth_item VALUE('item_name4',1,'item_description','rule_name4','data',0,0);
INSERT INTO auth_item VALUE('item_name5',1,'item_description','rule_name5','data',0,0);
但只插成功了3条,因为auth_item表中rule_name4和rule_name5没有在auth_rule中存在,相关语句
foreign key (`rule_name`) references `auth_rule` (`name`)
插入auth_item_child表
INSERT INTO auth_item_child VALUE('item_name1','item_name2');
INSERT INTO auth_item_child VALUE('item_name3','item_name4');
会提示第二条插入不成功
当我们修改auth_rule表时
UPDATE auth_rule SET NAME='rule_name11' WHERE NAME='rule_name1'
相关表auth_item已会发生变化
相关链接:http://my.oschina.net/sallency/blog/465079