- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial.sql
More file actions
Latest commit
90 lines (82 loc) · 6.76 KB
/
Copy pathinitial.sql
File metadata and controls
90 lines (82 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
DROPTRIGGER IF EXISTS VerifyEmployeeInsert;
DROPTRIGGER IF EXISTS VerifyEmployeeUpdate;
DROPTABLE IF EXISTS employee_role_join;
DROPTABLE IF EXISTS employee;
DROPTABLE IF EXISTS employeerole;
CREATETABLE `employee` (
`id`int(11) NOT NULL UNIQUE AUTO_INCREMENT,
`bStatus`int(11) NOT NULL,
`dateOfBirth` datetime NOT NULL,
`dateOfEmployment` datetime DEFAULT NULL,
`firstName`varchar(255) NOT NULL,
`lastName`varchar(255) NOT NULL,
`middleInitial`char(1) DEFAULT NULL,
`username`varchar(255) NOT NULL,
`email`varchar(255) NOT NULL,
`password`varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
);
DELIMITER $$
CREATETRIGGERVerifyEmployeeInsert BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF (SELECTCOUNT(1) FROM employee WHEREemployee.username=NEW.usernameANDemployee.bStatus=0) >0 THEN
SIGNAL SQLSTATE '45001'SET MESSAGE_TEXT ='Unable to insert duplicate username';
END IF;
END;
$$
CREATETRIGGERVerifyEmployeeUpdate BEFORE UPDATEON employee
FOR EACH ROW
BEGIN
IF NEW.bStatus=0 THEN
BEGIN
IF (SELECTCOUNT(1) FROM employee WHERENEW.username=employee.usernameANDemployee.id<>NEW.idandemployee.bStatus=0) >0 THEN
SIGNAL SQLSTATE '45001'SET MESSAGE_TEXT ='Unable to insert duplicate username';
END IF;
END;
END IF;
END;
$$
DELIMITER ;
CREATETABLE `employeerole` (
`id`int(11) NOT NULL UNIQUE AUTO_INCREMENT,
`role`varchar(255) NOT NULL UNIQUE,
PRIMARY KEY (`id`)
);
CREATETABLE `employee_role_join` (
`employee_id`int(11) NOT NULL,
`role_id`int(11) NOT NULL,
PRIMARY KEY (`employee_id`,`role_id`),
FOREIGN KEY ( employee_id ) REFERENCES employee ( id ) ON DELETE CASCADE,
FOREIGN KEY ( role_id ) REFERENCES employeerole ( id ) ON DELETE CASCADE
);
insert into employeerole (role) values ('ROLE_ADD_EMP');
insert into employeerole (role) values ('ROLE_UPDATE_EMP');
insert into employeerole (role) values ('ROLE_DELETE_EMP');
insert into employeerole (role) values ('ROLE_SET_PASSWORD');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-11-26', '2001-01-01', 'Kenzan', 'Test', 'A', 'kenzan', 'kenzan@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-11-27', '2002-02-02', 'Kenzan', 'Test A', 'B', 'kenzana', 'kenzana@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-11-28', '2003-03-03', 'Kenzan', 'Test AD', 'C', 'kenzanad', 'kenzanad@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-11-29', '2004-04-04', 'Kenzan', 'Test AU', 'D', 'kenzanau', 'kenzanau@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-11-30', '2005-05-05', 'Kenzan', 'Test ADU', 'E', 'kenzanadu', 'kenzanadu@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-12-01', '2006-06-06', 'Kenzan', 'Test D', 'F', 'kenzand', 'kenzand@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-12-02', '2007-07-07', 'Kenzan', 'Test DU', 'G', 'kenzandu', 'kenzandu@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-12-03', '2008-08-08', 'Kenzan', 'Test U', 'H', 'kenzanu', 'kenzanu@kenzan.com');
insert into employee (bStatus, dateOfBirth, dateOfEmployment, firstName, lastName, middleInitial, username, email) values (0, '1968-12-04', '2008-08-09', 'Kenzan', 'Test P', 'H', 'kenzanp', 'kenzanp@kenzan.com');
update employee set password='$2a$10$i51OFodMCqcVjvDyQUt8IeYhtuMH7J6JqUXKwWWPCP00DcgHnIscG'; /* password='kenzan' */
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzana'), (select id from employeerole where role='ROLE_ADD_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanad'), (select id from employeerole where role='ROLE_ADD_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanau'), (select id from employeerole where role='ROLE_ADD_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanadu'), (select id from employeerole where role='ROLE_ADD_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzand'), (select id from employeerole where role='ROLE_DELETE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzandu'), (select id from employeerole where role='ROLE_DELETE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanad'), (select id from employeerole where role='ROLE_DELETE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanadu'), (select id from employeerole where role='ROLE_DELETE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanadu'), (select id from employeerole where role='ROLE_UPDATE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzandu'), (select id from employeerole where role='ROLE_UPDATE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanau'), (select id from employeerole where role='ROLE_UPDATE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanu'), (select id from employeerole where role='ROLE_UPDATE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanp'), (select id from employeerole where role='ROLE_ADD_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanp'), (select id from employeerole where role='ROLE_UPDATE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanp'), (select id from employeerole where role='ROLE_DELETE_EMP'));
insert into employee_role_join (employee_id, role_id) values ((select id from employee where username='kenzanp'), (select id from employeerole where role='ROLE_SET_PASSWORD'));