mysql联合索引的用法_MySQL联合索引用法示例

本文实例讲述了mysql联合索引。分享给大家供大家参考,具体如下:

员工表 userid

部门表 deptid

员工部门表

条件:一个员工可以对应多个部门

问题:怎么样设置数据库,让其不能重复添加 userid 和deptid?

uuid userid deptid

111

212

311(这个就不能让其添加)

761d8b95d40be3dd23a91b5103412514.png

drop table if exists `dept`;

create table `dept` (

`id` int(11) not null auto_increment,

`deptname` char(32) not null,

primary key (`id`)

) engine=innodb auto_increment=3 default charset=utf8;

-- ----------------------------

-- records of dept

-- ----------------------------

insert into `dept` values ('1', '1');

insert into `dept` values ('2', '2');

drop table if exists `employee`;

create table `employee` (

`id` int(11) not null auto_increment,

`name` varchar(32) not null,

primary key (`id`)

) engine=innodb auto_increment=3 default charset=utf8;

-- ----------------------------

-- records of employee

-- ----------------------------

insert into `employee` values ('1', '11');

drop table if exists `employee_dept`;

create table `employee_dept` (

`id` int(11) not null,

`employeeid` int(11) not null,

`deptid` int(11) not null,

primary key (`id`),

key `bb` (`deptid`),

key `myindex` (`employeeid`,`deptid`),

constraint `aa` foreign key (`employeeid`) references `employee` (`id`),

constraint `bb` foreign key (`deptid`) references `dept` (`id`)

) engine=innodb default charset=utf8;

-- ----------------------------

-- records of employee_dept

-- ----------------------------

insert into `employee_dept` values ('1', '1', '1');

insert into `employee_dept` values ('2', '1', '2');

备注:创建联合索引create index myindex on employee_dept (employeeid,deptid);

希望本文所述对大家mysql数据库计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值