Database Management & Schema Design
Database Management & Schema Design
Command: Create
https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 1/10
11/30/24, 9:40 PM Database Management & Schema Design
-- 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
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';
• Example:
https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 3/10
11/30/24, 9:40 PM Database Management & Schema Design
• Example:
3. UNIQUE Constraint:
Ensures that the values in a column or a group of columns are unique across all
rows in the table.
• Example:
https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 4/10
11/30/24, 9:40 PM Database Management & Schema Design
• Example:
SQL Copy
5. CHECK Constraint:
Specifies a condition that must be true for each row in a table.
• Example:
6. DEFAULT Constraint:
Sets a default value for a column if no value is specified during INSERT operations.
• Example:
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
This index could improve performance for queries that involve filtering or sorting
based on both the departure and destination airports.
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
https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 7/10
11/30/24, 9:40 PM Database Management & Schema Design
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.
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.
https://almabetter.notion.site/Database-Management-Schema-Design-1201a19a561780318303d61ae994c45a 10/10