-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherp_progIITest.sql
91 lines (81 loc) · 2.84 KB
/
erp_progIITest.sql
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
drop schema erp_progIITest;
CREATE DATABASE erp_progIITest;
USE erp_progIITest;
CREATE TABLE `Users`(
`full_name` varchar(255),
`user_name` varchar(255) PRIMARY KEY,
`password` varbinary(255),
`role` ENUM ('admin', 'simpleuser')
);
CREATE TABLE `Suppliers` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`full_name` varchar(255),
`address` varchar(255),
`phonenumber` bigint,
`email` varchar(255),
`is_deleted` boolean default false
);
CREATE TABLE `Customers` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`full_name` varchar(255),
`address` varchar(255),
`phonenumber` bigint,
`email` varchar(255),
`is_deleted` boolean default false
);
CREATE TABLE `Raw_Materials` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`name` varchar(255),
`supplier_id` int NOT NULL,
`quantity` int,
`price` double,
`is_deleted` boolean default false,
CONSTRAINT raw_mat_sup FOREIGN KEY (`supplier_id`) REFERENCES `Suppliers` (`id`) on delete cascade
);
CREATE TABLE `Products` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`name` varchar(255),
`quantity` int,
`price` double,
`is_deleted` boolean default false
);
CREATE TABLE `S_Orders` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`supplier_id` int NOT NULL,
`status` ENUM ('delivered', 'pending'),
`created_at` datetime DEFAULT now(),
CONSTRAINT s_orsre_sup FOREIGN KEY (`supplier_id`) REFERENCES `Suppliers` (`id`) on delete cascade
);
CREATE TABLE `C_Orders` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`customer_id` int NOT NULL,
`status` ENUM ('preparing', 'ready', 'delivered'),
`created_at` datetime DEFAULT now(),
`user_name` varchar(255),
CONSTRAINT c_order_cust FOREIGN KEY (`customer_id`) REFERENCES `Customers` (`id`) on delete cascade,
CONSTRAINT c_order_u FOREIGN KEY (`user_name`) REFERENCES `Users` (`user_name`) on delete set null
);
CREATE TABLE `C_order_items` (
`c_order_id` int,
`product_id` int,
`quantity` int DEFAULT 1,
primary key(c_order_id, product_id),
CONSTRAINT c_order_i_c_ord FOREIGN KEY (`c_order_id`) REFERENCES `C_Orders` (`id`) on delete cascade,
CONSTRAINT c_order_pr FOREIGN KEY (`product_id`) REFERENCES `Products` (`id`) on delete cascade
);
CREATE TABLE `S_order_items` (
`s_order_id` int,
`raw_material_id` int,
`quantity` int DEFAULT 1,
primary key(s_order_id, raw_material_id),
CONSTRAINT s_order_items_s_order FOREIGN KEY (`s_order_id`) REFERENCES `S_Orders` (`id`) on delete cascade,
CONSTRAINT s_order_items_r_mat FOREIGN KEY (`raw_material_id`) REFERENCES `Raw_Materials` (`id`) on delete cascade
);
CREATE TABLE `P_Materials` (
`product_id` int,
`raw_material_id` int,
`quantity_of_raw_material` int,
primary key(product_id, raw_material_id),
CONSTRAINT p_mat_pr FOREIGN KEY (`product_id`) REFERENCES `Products` (`id`) on delete cascade,
CONSTRAINT p_mat_r_mat FOREIGN KEY (`raw_material_id`) REFERENCES `Raw_Materials` (`id`) on delete cascade
);