-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.sql
executable file
·73 lines (65 loc) · 2.1 KB
/
init_db.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
-- Create ship_status table
CREATE TABLE IF NOT EXISTS ship_status (
ship_voyage_number VARCHAR(10) PRIMARY KEY,
ship_name VARCHAR(100),
latest_event VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create ship_berth_order table
CREATE TABLE IF NOT EXISTS ship_berth_order (
berth_number VARCHAR(10),
berthing_time TIMESTAMP,
ship_status VARCHAR(10),
pilotage_time TIMESTAMP,
ship_name_chinese VARCHAR(50),
ship_name_english VARCHAR(50),
port_agent VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (berth_number, ship_name_chinese, ship_status)
);
-- Create ship_voyage table
CREATE TABLE IF NOT EXISTS ship_voyage (
ship_voyage_number VARCHAR(10) PRIMARY KEY,
pass_10_miles_time TIMESTAMP,
pass_5_miles_time TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create ship_events table
CREATE TABLE IF NOT EXISTS ship_events (
id SERIAL PRIMARY KEY,
ship_voyage_number VARCHAR(10),
event_source VARCHAR(50),
event_time TIMESTAMP,
event_name VARCHAR(100),
navigation_status VARCHAR(50),
pilot_order_number VARCHAR(20),
berth_number VARCHAR(10),
event_content_time TIMESTAMP,
UNIQUE (ship_voyage_number, event_time, event_name)
);
-- Create the trigger function
CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create the trigger for ship_status table
CREATE TRIGGER update_ship_status_timestamp
BEFORE UPDATE ON ship_status
FOR EACH ROW
EXECUTE FUNCTION update_timestamp();
-- Create the trigger for ship_voyage table
CREATE TRIGGER update_ship_voyage_timestamp
BEFORE UPDATE ON ship_voyage
FOR EACH ROW
EXECUTE FUNCTION update_timestamp();
-- Create the trigger for ship_berth_order table
CREATE TRIGGER update_ship_berth_order_timestamp
BEFORE UPDATE ON ship_berth_order
FOR EACH ROW
EXECUTE FUNCTION update_timestamp();