0% found this document useful (0 votes)
15 views10 pages

Database Management & Schema Design

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Database Management & Schema Design

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

11/30/24, 9:40 PM Database Management & Schema Design

Database Management &


Schema Design

Database Management & Schema Design

Data Definition Language (DDL) : Database & Tables


Dealing with CRUD Operation of Database and Table.

Command: Create

-- Create a database for SkyConnect Airways CREATE DATABASE


skyconnect_airways; -- Create tables for flights, passengers, and crew
CREATE TABLE flights ( flight_id SERIAL PRIMARY KEY, flight_number
VARCHAR(10) NOT NULL, departure_airport VARCHAR(50) NOT NULL,
destination_airport VARCHAR(50) NOT NULL, departure_time TIMESTAMP NOT
NULL, arrival_time TIMESTAMP NOT NULL, aircraft_type VARCHAR(50) NOT NULL
); CREATE TABLE passengers ( passenger_id SERIAL PRIMARY KEY, flight_id
INT REFERENCES flights(flight_id), passenger_name VARCHAR(100) NOT NULL,
seat_number VARCHAR(10) NOT NULL ); CREATE TABLE crew ( crew_id SERIAL
PRIMARY KEY, crew_type VARCHAR(50) NOT NULL, member_name VARCHAR(100) NOT
NULL, assigned_flight INT REFERENCES flights(flight_id) );

Command: ALTER Statement (Update)

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 1/10
11/30/24, 9:40 PM Database Management & Schema Design

-- Add a new column named "delay_minutes" to the "flights" table -- Add


multiple columns to the "flights" table ALTER TABLE flights ADD COLUMN
delay_minutes INTEGER NOT NULL, ADD COLUMN departure_gate VARCHAR(10),
ADD COLUMN arrival_gate VARCHAR(10); -- Delete a column named
"delay_minutes" to the "flights" table ALTER TABLE flights DROP COLUMN
delay_minutes, DROP COLUMN departure_gate, DROP COLUMN arrival_gate;

Command: DROP Statement (DELETE)


An RI (Referential Integrity) Constraint Trigger is a type of database trigger that
helps maintain referential integrity in a relational database. Referential integrity
ensures that relationships between tables remain consistent. For example, if one
table (the child) references another table (the parent) through a foreign key, the RI
constraint ensures that you cannot insert a value in the child table that doesn’t exist
in the parent table.

-- Delete the "crew" table from the database DROP TABLE crew; -- Deletes
the table --> del DROP TABLE flights; -- Can't be dropped until you drop
referenced tables -- i.e., passanger & Crew DROP TABLE passengers;
TRUNCATE TABLE student; -- Deletes the records --> .clear() --- Drop
database skyconnect_airways; --

Command: rename (In Postgres use alter table to rename the table name)

ALTER TABLE crew RENAME TO crews; -- Postgres Alter table flights rename
column flight_number to fl_num --Rename Column --Rename table
old_table_name to new_table_name; --> MySQL

Data Manipulation Language (DML) : Records

Command: INSERT

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 2/10
11/30/24, 9:40 PM Database Management & Schema Design

-- Insert a new flight into the flights table INSERT INTO flights
(flight_number, departure_airport, destination_airport, departure_time,
arrival_time, aircraft_type) VALUES ('SCA303', 'DFW', 'SFO', '2024-05-10
10:00:00', '2024-05-10 12:30:00', 'Airbus A321'); INSERT INTO passengers
(flight_id, passenger_name, seat_number) VALUES (1, 'Alice Johnson',
'A1'), (1, 'Bob Smith', 'B2');

Command: Update

-- Update the departure time for flight SCA101 UPDATE flights SET
departure_time = '2024-05-01 08:30:00' WHERE flight_number = 'SCA303'; --
Update multiple columns in the "flights" table UPDATE flights SET
departure_time = '2024-05-01 08:30:00', arrival_time = '2024-05-01
11:00:00', aircraft_type = 'Boeing 777' WHERE flight_number = 'SCA303';

Command: Delete

-- Remove passenger Bob Smith from flight SCA303 DELETE FROM passengers
WHERE flight_id = 1 AND passenger_name = 'Bob Smith';

Constraints & Indexes


Constraints
1. PRIMARY KEY Constraint:
Ensures that each row in a table is uniquely identified.

• Example:

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 3/10
11/30/24, 9:40 PM Database Management & Schema Design

CREATE TABLE flights ( flight_id INT PRIMARY KEY, flight_number VARCHAR(1


0) UNIQUE, departure_airport VARCHAR(50) NOT NULL, destination_airport VA
RCHAR(50) NOT NULL, departure_time TIMESTAMP NOT NULL, arrival_time TIMES
TAMP NOT NULL, aircraft_type VARCHAR(50) NOT NULL ); INSERT INTO flights
(flight_id,flight_number, departure_airport, destination_airport, departu
re_time, arrival_time, aircraft_type) VALUES (1,'SCA303', 'DFW', 'SFO',
'2024-05-10 10:00:00', '2024-05-10 12:30:00', 'Airbus A321');

2. FOREIGN KEY Constraint:


Establishes a relationship between two tables by referencing the primary key column
of one table in another table.

• Example:

CREATE TABLE passengers ( passenger_id SERIAL PRIMARY KEY, flight_id I


NT REFERENCES flights(flight_id), passenger_name VARCHAR(100) NOT NUL
L, seat_number VARCHAR(10) NOT NULL );

3. UNIQUE Constraint:
Ensures that the values in a column or a group of columns are unique across all
rows in the table.

• Example:

-- Single Column Unique CREATE TABLE flights ( flight_id SERIAL PRIMARY K


EY, flight_number VARCHAR(10) UNIQUE, departure_airport VARCHAR(50) NOT N
ULL, destination_airport VARCHAR(50) NOT NULL, departure_time TIMESTAMP N
OT NULL, arrival_time TIMESTAMP NOT NULL, aircraft_type VARCHAR(50) NOT N
ULL ); -- Combination Column Unique CREATE TABLE flight_routes ( route_id
SERIAL PRIMARY KEY, departure_airport VARCHAR(50) NOT NULL, destination_a
irport VARCHAR(50) NOT NULL, UNIQUE (departure_airport, destination_airpo
rt) );

4. NOT NULL Constraint:

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 4/10
11/30/24, 9:40 PM Database Management & Schema Design

Requires that a column must not contain NULL values.

• Example:

SQL Copy

CREATE TABLE crew ( crew_id SERIAL PRIMARY KEY, crew_type VARCHAR(50)


NOT NULL, member_name VARCHAR(100) NOT NULL, assigned_flight INT REFER
ENCES flights(flight_id), duty_start_time TIMESTAMP NOT NULL, duty_end
_time TIMESTAMP NOT NULL );

5. CHECK Constraint:
Specifies a condition that must be true for each row in a table.

• Example:

CREATE TABLE aircrafts ( aircraft_id SERIAL PRIMARY KEY, aircraft_type


VARCHAR(50) NOT NULL, seating_capacity INT NOT NULL CHECK (seating_cap
acity > 0), max_speed INT NOT NULL CHECK (max_speed > 0) );

6. DEFAULT Constraint:
Sets a default value for a column if no value is specified during INSERT operations.

• Example:

CREATE TABLE flights ( flight_id SERIAL PRIMARY KEY, flight_number VAR


CHAR(10) UNIQUE, departure_airport VARCHAR(50) NOT NULL, destination_a
irport VARCHAR(50) NOT NULL, departure_time TIMESTAMP NOT NULL CHECK
(departure_time > '2024-01-01'), arrival_time TIMESTAMP NOT NULL CHECK
(arrival_time>=CURRENT_TIMESTAMP), aircraft_type VARCHAR(50) NOT NULL,
status VARCHAR(20) DEFAULT 'Scheduled' );

Indexes
Indexes in SQL are special database objects that improve the speed of data retrieval
operations on a database table. They work like an index in a book, allowing the
database to find rows more quickly. Here's a breakdown of how indexes work along
with examples.

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 5/10
11/30/24, 9:40 PM Database Management & Schema Design

Heading 3

--creating a composite index on multiple columns: CREATE INDEX


idx_flight_number ON flights (flight_number); CREATE INDEX
idx_departure_destination ON flights (departure_airport,
destination_airport);

This index could improve performance for queries that involve filtering or sorting
based on both the departure and destination airports.

Indexes automatically enhance the performance of SELECT queries. For


example: -- Query to retrieve information about a flight using its flight
number SELECT * FROM flights WHERE flight_number = 'SCA101'; -- With an
index on flight_number, -- the database can quickly locate all
flight_numbers with that filter. -- Query to retrieve information about
flights based on departure and destination airports SELECT * FROM flights
WHERE departure_airport = 'JFK' AND destination_airport = 'LAX';

ERD Diagram
An Entity-Relationship Diagram (ERD) is a visual representation of the data model
that illustrates the entities, attributes, relationships, and constraints within a
database system. ERDs are commonly used during the database design phase to
communicate and document the structure of a database in a clear and concise
manner.

PGADMIN

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 6/10
11/30/24, 9:40 PM Database Management & Schema Design

It automatically creates the ERD Diagram based on the reference


Note: For students who are facing issues with PGADMIN use
https://dbdiagram.io/d

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 7/10
11/30/24, 9:40 PM Database Management & Schema Design

TABLE flights { flight_id INT [primary key] flight_number VARCHAR(10)


departure_airport VARCHAR(50) destination_airport VARCHAR(50)
departure_time TIMESTAMP arrival_time TIMESTAMP aircraft_type
VARCHAR(50)} TABLE passengers { passenger_id INT [primary key] flight_id
INT passenger_name VARCHAR(100) seat_number VARCHAR(10) crew_id INT}
TABLE crew { crew_id INT [primary key] crew_type VARCHAR(50) member_name
VARCHAR(100) assigned_flight INT } Ref: flights.flight_id >
passengers.flight_id Ref: passengers.crew_id > crew.crew_id

Follow the sample as mentioned.

Normalisation and Denormalisation


Normalisation:
Normalisation is the process of organising data in a database to reduce redundancy
and dependency. It involves breaking down tables into smaller, related tables and
defining relationships between them using foreign keys.
Normalization helps prevent data anomalies and ensures data integrity but can
sometimes lead to more complex queries due to the need for joins.

Denormalisation:
denormalisation involves adding redundant data or pre-joining tables to improve
query performance by reducing the need for joins.
While denormalisation can improve query performance, it can also lead to data
redundancy and potential inconsistency, so it should be used judiciously.

Example :
1. Denormalised Schema:

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 8/10
11/30/24, 9:40 PM Database Management & Schema Design

--------------------------------------------------------- | product_id |
product_name | category_id | category_name | category_desc --------------
------------------------------------------- | 1 | Laptop | 1 |
Electronics | Electronics Product | 2 | Smartphone | 1 | Electronics |
Electronics Product | 3 | Shirt | 2 | Clothing | Mens' clothing ---------
------------------------------------------------

2. Normalised Schema:
We break down the table into two separate tables: products and categories ,
and establish a relationship between them using foreign keys.

products ------------------------------------- | product_id | product_


name | category_id | ------------------------------------- | 1 | Lapto
p | 1 | | 2 | Smartphone | 1 | | 3 | Shirt | 2 | ---------------------
---------------- categories ------------------------- | category_id |
name | desc ------------------------- | 1 | Electronics | Electronics
Product | 2 | Clothing | Mens' clothing -------------------------

Case Study: ERD Diagram for Instagram

Users ---------- UserID (PK) Username Full_Name Email Bio ... Posts -----
----- PostID (PK) UserID (FK) Caption Image_URL Likes_Count
Comments_Count Post_Date ... Comments ---------- CommentID (PK) PostID
(FK) UserID (FK) Content Comment_Date ... Follows ---------- Sl_id (PK)
FollowerID (FK) FollowingID (FK) Follow_Date ... block ----- UserID(FK)
userID_blocked(FK) Share ------ share_id(PK) user_id(FK) post_id(FK)
platform share_date

Explanation:

• Users Table: Stores user information. UserID is the primary key. Username,
Full_Name, Email, Bio, etc., are attributes of a user.

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 9/10
11/30/24, 9:40 PM Database Management & Schema Design

• Posts Table: Contains information about user posts. PostID is the primary key.
UserID is a foreign key referencing the User table. Caption, Image_URL,
Likes_Count, Comments_Count, and Post_Date are attributes of a post.

• Comments Table: Stores comments made by users on posts. CommentID is the


primary key. PostID and UserID are foreign keys referencing the Post and User
tables, respectively. Content and Comment_Date are attributes of a comment.

https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 10/10

You might also like