· 8 years ago · Jul 29, 2018, 05:10 AM
1Composite key as foreign key (sql)
2CREATE TABLE IF NOT EXISTS `tutorial` (
3 `beggingTime` time NOT NULL,
4 `day` varchar(8) NOT NULL,
5 `tutorId` int(3) NOT NULL,
6 `maxMembers` int(2) NOT NULL,
7 `minMembers` int(1) NOT NULL,
8 PRIMARY KEY (`beggingTime`,`day`,`tutorId`),
9 KEY `tutorId` (`tutorId`)
10)
11
12
13CREATE TABLE IF NOT EXISTS `group` (
14 `groupId` tinyint(3) NOT NULL AUTO_INCREMENT,
15 `status` varchar(20) NOT NULL,
16 `groupName` varchar(50) NOT NULL,
17 PRIMARY KEY (`groupId`)
18)
19
20FOREIGN KEY (`beggingTime`,`day`,`tutorId`)
21 REFERENCES tutorial(`beggingTime`,`day`,`tutorId`)
22
23CREATE TABLE IF NOT EXISTS `tutorial` (
24 `beggingTime` time NOT NULL,
25 `day` varchar(8) NOT NULL,
26 `tutorId` int(3) NOT NULL,
27 `maxMembers` int(2) NOT NULL,
28 `minMembers` int(1) NOT NULL,
29 PRIMARY KEY mykey (`tutorId`,`beggingTime`,`day`)
30)