我有一张桌子
CREATE TABLE IF NOT EXISTS `hrms_employee_leave` (
`employee_leave_id` int(11) NOT NULL,
`employee_id` int(11) NOT NULL,
`leave_category_id` int(11) NOT NULL,
`days` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
它会显示员工有多少请假,这是此表中的数据存储。
Employee_id = 25,
leave_category_id = 2,
days = 14
Employee_id = 25,
leave_category_id = 3,
days = 14
当我内部加入表格时,输出就像
Employee Name = Foo,
Leave Category = Annual Leave,
days = 14
Foo has 14 days of Annual Leave.
Employee Name = Foo,
Leave Category = Medical Leave,
days = 14
Foo has 14 days of Medical Leave.
如何合并 employee_id 和 leave_category_id 成为无法复制的唯一键,以防止用户插入多次
Employee_id = 25,
leave_category_id = 2,
days = 14
Employee_id = 25,
leave_category_id = 3,
days = 14
答案 0 :(得分:0)
添加多列唯一索引有多种方法。例如。你可以在create table语句中执行它:
CREATE TABLE IF NOT EXISTS `hrms_employee_leave` (
`employee_leave_id` int(11) NOT NULL,
`employee_id` int(11) NOT NULL,
`leave_category_id` int(11) NOT NULL,
`days` int(11) NOT NULL,
CONSTRAINT `uniqueindex_1` UNIQUE (`employee_leave_id`, `leave_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
这样,emloyee_leave_id
和leave_category_id
组合不会重复。而且,这些都不是NULL。如果这些列同时可以是外键,则两者都可以。
请参阅示例,其中这两列既是外键又是复合唯一索引:http://www.sqlfiddle.com/#!9/a4dfc