Objective
Create a system for storing and managing surveillance video footage from security cameras with support for multiple locations, retention policies, and efficient retrieval.
Step 1: Create Camera Locations Table
Define camera installation sites.
CREATE TABLE camera_locations (
id INTEGER PRIMARY KEY,
location_code VARCHAR(50) NOT NULL UNIQUE,
location_name VARCHAR(200),
building VARCHAR(100),
floor VARCHAR(20),
zone VARCHAR(100),
area_type VARCHAR(50),
gps_latitude DECIMAL(10, 8),
gps_longitude DECIMAL(11, 8),
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Create Security Cameras Table
Store camera details.
CREATE TABLE security_cameras (
id INTEGER PRIMARY KEY,
camera_id VARCHAR(50) NOT NULL UNIQUE,
camera_name VARCHAR(200),
location_id INTEGER NOT NULL,
camera_type VARCHAR(50),
manufacturer VARCHAR(100),
model VARCHAR(100),
resolution VARCHAR(20),
frame_rate INTEGER DEFAULT 30,
has_audio BOOLEAN DEFAULT FALSE,
has_ptz BOOLEAN DEFAULT FALSE,
has_night_vision BOOLEAN DEFAULT TRUE,
ip_address VARCHAR(50),
status VARCHAR(50) DEFAULT 'online',
install_date DATE,
last_maintenance DATE,
FOREIGN KEY (location_id) REFERENCES camera_locations(id)
);
Step 3: Create Video Recordings Table
Store recorded footage.
CREATE TABLE video_recordings (
id INTEGER PRIMARY KEY,
camera_id INTEGER NOT NULL,
recording_uid VARCHAR(100) UNIQUE,
video_file VIDEO(MP4),
thumbnail IMAGE(JPEG),
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP NOT NULL,
duration_seconds INTEGER,
file_size_mb DECIMAL(10, 2),
resolution VARCHAR(20),
frame_rate INTEGER,
codec VARCHAR(50),
has_motion BOOLEAN DEFAULT FALSE,
motion_events INTEGER DEFAULT 0,
quality_score DECIMAL(3, 2),
is_archived BOOLEAN DEFAULT FALSE,
retention_days INTEGER DEFAULT 30,
expires_at TIMESTAMP,
FOREIGN KEY (camera_id) REFERENCES security_cameras(id)
);
Step 4: Create Video Segments Table
Store chunked recordings for streaming.
CREATE TABLE video_segments (
id INTEGER PRIMARY KEY,
recording_id INTEGER NOT NULL,
segment_number INTEGER,
segment_file VIDEO(MP4),
start_offset_sec INTEGER,
duration_sec INTEGER,
file_size_mb DECIMAL(8, 2),
is_keyframe BOOLEAN DEFAULT FALSE,
FOREIGN KEY (recording_id) REFERENCES video_recordings(id)
);
Step 5: Create Recording Tags Table
Label recordings with metadata.
CREATE TABLE recording_tags (
id INTEGER PRIMARY KEY,
recording_id INTEGER NOT NULL,
tag_name VARCHAR(100),
tag_value VARCHAR(255),
tagged_by VARCHAR(100),
tagged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (recording_id) REFERENCES video_recordings(id)
);
Step 6: Insert Camera Locations
Add surveillance zones.
INSERT INTO camera_locations (id, location_code, location_name, building, floor, zone, area_type, gps_latitude, gps_longitude) VALUES
(1, 'LOC-ENT-MAIN', 'Main Entrance', 'Building A', 'Ground', 'Entrance Zone', 'entrance', 37.78825000, -122.40730000),
(2, 'LOC-LOBBY-A', 'Main Lobby', 'Building A', 'Ground', 'Lobby Zone', 'lobby', 37.78830000, -122.40735000),
(3, 'LOC-PARK-A', 'Parking Garage Level 1', 'Parking A', 'P1', 'Parking Zone', 'parking', 37.78815000, -122.40745000),
(4, 'LOC-PARK-B', 'Parking Garage Level 2', 'Parking A', 'P2', 'Parking Zone', 'parking', 37.78815000, -122.40745000),
(5, 'LOC-SERV-A', 'Server Room', 'Building A', '2', 'Secure Zone', 'restricted', 37.78840000, -122.40720000),
(6, 'LOC-ELEV-A1', 'Elevator Bank A', 'Building A', 'Ground', 'Common Area', 'elevator', 37.78832000, -122.40732000),
(7, 'LOC-STAIR-A1', 'Stairwell A North', 'Building A', 'All', 'Emergency Exit', 'stairwell', 37.78835000, -122.40728000),
(8, 'LOC-LOAD-A', 'Loading Dock', 'Building A', 'Ground', 'Service Zone', 'loading', 37.78820000, -122.40750000);
Step 7: Insert Security Cameras
Add camera inventory.
INSERT INTO security_cameras (id, camera_id, camera_name, location_id, camera_type, manufacturer, model, resolution, frame_rate, has_audio, has_ptz, has_night_vision, ip_address, status, install_date) VALUES
(1, 'CAM-ENT-001', 'Main Entrance Camera 1', 1, 'dome', 'Axis', 'P3245-V', '1080p', 30, TRUE, TRUE, TRUE, '192.168.1.101', 'online', '2023-01-15'),
(2, 'CAM-ENT-002', 'Main Entrance Camera 2', 1, 'bullet', 'Hikvision', 'DS-2CD2085', '4K', 25, FALSE, FALSE, TRUE, '192.168.1.102', 'online', '2023-01-15'),
(3, 'CAM-LOBBY-001', 'Lobby Overview', 2, 'dome', 'Axis', 'P3245-V', '1080p', 30, TRUE, TRUE, TRUE, '192.168.1.103', 'online', '2023-01-15'),
(4, 'CAM-PARK-P1-001', 'Parking P1 Entrance', 3, 'bullet', 'Hikvision', 'DS-2CD2085', '4K', 25, FALSE, FALSE, TRUE, '192.168.1.104', 'online', '2023-02-01'),
(5, 'CAM-PARK-P1-002', 'Parking P1 Interior', 3, 'dome', 'Axis', 'M3057-PLVE', '1080p', 15, FALSE, FALSE, TRUE, '192.168.1.105', 'online', '2023-02-01'),
(6, 'CAM-SERVER-001', 'Server Room Entry', 5, 'dome', 'Axis', 'P3245-V', '4K', 30, TRUE, FALSE, TRUE, '192.168.1.106', 'online', '2023-01-20'),
(7, 'CAM-SERVER-002', 'Server Room Interior', 5, 'fisheye', 'Axis', 'M3057-PLVE', '1080p', 15, TRUE, FALSE, TRUE, '192.168.1.107', 'online', '2023-01-20'),
(8, 'CAM-LOAD-001', 'Loading Dock', 8, 'bullet', 'Hikvision', 'DS-2CD2685', '4K', 25, TRUE, TRUE, TRUE, '192.168.1.108', 'online', '2023-03-01');