-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmySQLref.sql
30 lines (25 loc) · 1.14 KB
/
mySQLref.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
kCREATE TABLE IF NOT EXISTS objects (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS relationships (
parent_id INT,
child_id INT,
PRIMARY KEY (parent_id, child_id),
FOREIGN KEY (parent_id) REFERENCES objects(id) ON DELETE CASCADE,
FOREIGN KEY (child_id) REFERENCES objects(id) ON DELETE CASCADE
);
-- Add root object
INSERT INTO objects (serial, name) VALUES ('ROOT001', 'Root Item');
-- Insert new objects
INSERT INTO objects (serial, name) VALUES
('ITEM002', 'Item A'),
('ITEM003', 'Item B'),
('ITEM004', 'Item C'),
('ITEM005', 'Item D');
-- Establish parent-child relationships
INSERT INTO relationships (parent_id, child_id) VALUES
((SELECT id FROM objects WHERE serial = 'ROOT001'), (SELECT id FROM objects WHERE serial = 'ITEM002')),
((SELECT id FROM objects WHERE serial = 'ROOT001'), (SELECT id FROM objects WHERE serial = 'ITEM003')),
((SELECT id FROM objects WHERE serial = 'ITEM002'), (SELECT id FROM objects WHERE serial = 'ITEM004')),
((SELECT id FROM objects WHERE serial = 'ITEM003'), (SELECT id FROM objects WHERE serial = 'ITEM005'));