-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.sql
More file actions
92 lines (72 loc) · 2.26 KB
/
database.sql
File metadata and controls
92 lines (72 loc) · 2.26 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
CREATE DATABASE IF NOT EXISTS warehouse_db;
USE warehouse_db;
CREATE TABLE suppliers (
supplier_id INT AUTO_INCREMENT PRIMARY KEY,
supplier_name VARCHAR(100) NOT NULL,
email VARCHAR(100),
phone VARCHAR(20)
);
CREATE TABLE IF NOT EXISTS Items (
item_id INT AUTO_INCREMENT PRIMARY KEY,
rfid_tag_id VARCHAR(50) UNIQUE,
item_name VARCHAR(50) NOT NULL,
item_code VARCHAR(20) NOT NULL,
category VARCHAR(30),
minimum_level INT DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
supplier_id INT,
FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id)
);
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR (255),
role VARCHAR (30) NOT NULL
);
CREATE TABLE locations (
location_id INT AUTO_INCREMENT PRIMARY KEY,
location_code VARCHAR(50) NOT NULL,
description VARCHAR(150)
);
CREATE TABLE item_batches (
batch_id INT AUTO_INCREMENT,
item_id INT NOT NULL,
expiry_date DATE,
received_date DATE NOT NULL,
PRIMARY KEY (batch_id, item_id),
CONSTRAINT fk_batch_item
FOREIGN KEY (item_id) REFERENCES Items(item_id)
ON DELETE CASCADE
);
CREATE TABLE stock (
item_id INT NOT NULL,
batch_id INT NOT NULL,
location_id INT NOT NULL,
quantity INT NOT NULL DEFAULT 0,
PRIMARY KEY (item_id, batch_id, location_id),
FOREIGN KEY (item_id, batch_id)
REFERENCES item_batches(item_id, batch_id)
ON DELETE CASCADE,
FOREIGN KEY (location_id)
REFERENCES locations(location_id)
);
CREATE TABLE stock_transactions (
transaction_id INT AUTO_INCREMENT PRIMARY KEY,
item_id INT NOT NULL,
batch_id INT NOT NULL,
location_id INT NOT NULL,
from_location_id INT NULL,
user_id INT NOT NULL,
transaction_type ENUM('IN','OUT') NOT NULL,
quantity INT NOT NULL,
reference VARCHAR(100) DEFAULT 'RFID-AUTO',
transaction_time DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (item_id, batch_id)
REFERENCES item_batches(item_id, batch_id),
FOREIGN KEY (location_id)
REFERENCES locations(location_id),
FOREIGN KEY (from_location_id)
REFERENCES locations(location_id),
FOREIGN KEY (user_id)
REFERENCES users(user_id)
);