-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase_schema_lab.sql
More file actions
95 lines (85 loc) · 3.45 KB
/
database_schema_lab.sql
File metadata and controls
95 lines (85 loc) · 3.45 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
91
92
93
94
-- Create little schema to benchmark test
-- Reglas de mi schema:
-- 0- The branch_offices table is the father.
-- 1- The sections owns to a sucursal.
-- 2- The employees belong to a branch.
-- 3- The employees must belong to a section at least.
-- 4- The employees musn't be deleted, they have a status flag.
-- 5- The pieces belong to a branch.
-- 6- The pieces belong to a sections
-- 7- The pieces belong to a employee, or is procesed by.
-- Table sucursals
CREATE TABLE `branch_offices` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`manager_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX brchName_idx (`name`)
) ENGINE=Innodb DEFAULT CHARSET=utf8;
-- Table sections
CREATE TABLE `sections` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`branch_id` INT(11) UNSIGNED NOT NULL,
`supervisor_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX brchId_idx (`branch_id`),
FOREIGN KEY (`branch_id`) REFERENCES `branch_offices`(`id`) ON DELETE RESTRICT
) ENGINE=Innodb DEFAULT CHARSET=utf8;
-- Table employees
CREATE TABLE employees (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`surname` VARCHAR(16) NOT NULL,
`contract_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`section_id` INT UNSIGNED NOT NULL,
`branch_id` INT UNSIGNED NOT NULL,
`supervisor` TINYINT(1) UNSIGNED DEFAULT 0,
`manager` TINYINT(1) UNSIGNED DEFAULT 0,
`is_active` TINYINT(1) UNSIGNED DEFAULT 0,
`decoupling` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX brchId_idx (`branch_id`),
INDEX secId_idx (`section_id`),
FOREIGN KEY (`branch_id`) REFERENCES `branch_offices`(`id`) ON DELETE RESTRICT,
FOREIGN KEY (`section_id`) REFERENCES `sections`(`id`) ON DELETE RESTRICT
) ENGINE=Innodb DEFAULT CHARSET=utf8;
-- Table inventary pieces
CREATE TABLE inventary (
`piece_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`stock` INT UNSIGNED DEFAULT 0 NOT NULL,
`section_id` INT UNSIGNED NOT NULL,
`branch_id` INT UNSIGNED NOT NULL,
`modified` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (`piece_id`),
FOREIGN KEY (`section_id`) REFERENCES `sections`(`id`) ON DELETE RESTRICT,
FOREIGN KEY (`branch_id`) REFERENCES `branch_offices`(`id`) ON DELETE RESTRICT
) ENGINE=Innodb DEFAULT CHARSET=utf8;
-- Table destinity of pieces
CREATE TABLE destinnations (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`Postal_code` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=Innodb DEFAULT CHARSET=utf8;
-- Table pieces
CREATE TABLE pieces (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`piece_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(32) NOT NULL,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`section_id` INT UNSIGNED NOT NULL,
`employee_id` INT UNSIGNED NOT NULL,
`inventary_id` INT NOT NULL,
`destination_id` INT UNSIGNED NOT NULL,
`branch_id` INT UNSIGNED NOT NULL,
`status` VARCHAR(12) NOT NULL,
PRIMARY KEY (`id`, `piece_id`),
FOREIGN KEY (`branch_id`) REFERENCES `branch_offices`(`id`) ON DELETE RESTRICT,
FOREIGN KEY (`section_id`) REFERENCES `sections`(`id`) ON DELETE RESTRICT,
FOREIGN KEY (`employee_id`) REFERENCES `employees`(`id`) ON DELETE RESTRICT,
FOREIGN KEY (`piece_id`) REFERENCES `inventary`(`piece_id`) ON DELETE RESTRICT,
FOREIGN KEY (`destination_id`) REFERENCES `destinnations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=Innodb DEFAULT CHARSET=utf8;
-- End