Day 65 | SQL Basics: Schema, Tables, Primary & Foreign Keys Explained! 
In the world of SQL, structuring your data efficiently is the key to building scalable and organized databases. Whether you’re just starting out or brushing up on your skills, understanding schemas, tables, primary keys, and foreign keys is crucial! Let’s dive into the essentials! 
Schema
A schema is like a container for your database objects - think of it as an organizational unit! It holds tables, views, and other objects, allowing for better structure and access control.
Imagine you’re managing a company database. You might have a schema named CompanyDB to hold all related tables, such as employees, departments, and projects.
Syntax:
sql
CREATE SCHEMA CompanyDB;
Tables
Tables are where your data lives! They consist of rows and columns, each column having a specific data type (like INTEGER, VARCHAR, or DATE).
For example, in our company database, we might have:
Employee table for personal details
Dependent table for employee dependents
Department table for different departments
Project table for ongoing projects
WorksOn table to track which employee works on which project
Syntax:
sql
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
dept_id INT
);
Primary Key
A primary key uniquely identifies each row in a table. No two rows can have the same primary key, ensuring data integrity.
In the Employee table, the emp_id column is the primary key, making each employee uniquely identifiable.
Syntax:
sql
PRIMARY KEY (emp_id)
Foreign Key
A foreign key creates a link between two tables. It enforces relationships and maintains referential integrity between data. For instance, the WorksOn table needs to link employees to projects.
Syntax:
sql
CREATE TABLE WorksOn (
emp_id INT,
proj_id INT,
hours_worked DECIMAL(5,2),
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id),
FOREIGN KEY (proj_id) REFERENCES Project(proj_id)
);
Example: Database Design
Here’s how the design fits together:
Employee table holds employee data.
Dependent table stores employee-dependent details.
Department table keeps department info.
DepLocation table tracks where departments are located.
Project table holds project details.
WorksOn table records which employees are assigned to which projects.
These tables are linked through primary and foreign keys, creating a robust relational database that minimizes redundancy! 

Why It Matters
Efficient database design helps maintain data consistency and integrity. By leveraging schemas and relational keys, you’re building a scalable structure that can grow with your data needs. 
Whether you’re a beginner or a pro, mastering these SQL fundamentals will set you up for success. Happy coding! 
100daysofcode lebanon-mug