mysql字段索引

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值