-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB_structure.sql
More file actions
58 lines (53 loc) · 1.47 KB
/
DB_structure.sql
File metadata and controls
58 lines (53 loc) · 1.47 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
CREATE DATABASE IF NOT EXISTS AircraftDB;
USE AircraftDB;
-- Create the Country table
CREATE TABLE IF NOT EXISTS Country (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
-- Create the Type table
CREATE TABLE IF NOT EXISTS Type (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
-- Create the Aircraft table
CREATE TABLE IF NOT EXISTS Aircraft (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
model VARCHAR(255),
country_id INT,
type_id INT,
FOREIGN KEY (country_id) REFERENCES Country(id),
FOREIGN KEY (type_id) REFERENCES Type(id)
);
-- Create the Specifications table
CREATE TABLE IF NOT EXISTS Specifications (
id INT AUTO_INCREMENT PRIMARY KEY,
aircraft_id INT,
speed FLOAT,
_range FLOAT,
fuel_capacity FLOAT,
weight FLOAT,
FOREIGN KEY (aircraft_id) REFERENCES Aircraft(id)
);
-- Create the view to get all details of aircraft with specifications
CREATE VIEW AircraftDetails AS
SELECT
a.id AS aircraft_id,
a.name AS aircraft_name,
a.model AS aircraft_model,
c.name AS country_name,
t.name AS type_name,
s.speed AS aircraft_speed,
s._range AS aircraft_range,
s.fuel_capacity AS aircraft_fuel_capacity,
s.weight AS aircraft_weight
FROM
Aircraft a
JOIN
Country c ON a.country_id = c.id
JOIN
Type t ON a.type_id = t.id
LEFT JOIN
Specifications s ON a.id = s.aircraft_id;
select * from `AircraftDetails`;