What is a Database?
A database is an organized collection of structured data stored electronically. A Relational Database Management System (RDBMS) manages relational databases.
š” Think of a database as a highly organized filing system. Each "file cabinet" is a database, each "folder" is a table, and each "document" is a row.
Core Concepts
Tables
A table is a collection of related data organized in rows and columns.
-- Creating a simple table
CREATE TABLE students (
student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
enrollment_date DATE
);
Columns (Attributes)
Each column defines a specific attribute with a data type:
| Data Type | Description | Example |
|---|---|---|
INT | Whole numbers | 42 |
VARCHAR(n) | Variable-length text | 'Hello' |
DECIMAL(p,s) | Exact decimal numbers | 19.99 |
DATE | Calendar date | '2024-01-15' |
BOOLEAN | True/false values | TRUE |
TEXT | Unlimited text | Long paragraphs |
Rows (Records)
Each row represents a single record ā an instance of the entity described by the table.
-- Inserting rows
INSERT INTO students VALUES (1, 'Alice', 'Johnson', '2024-01-15');
INSERT INTO students VALUES (2, 'Bob', 'Smith', '2024-01-16');
Keys
Primary Key
A primary key uniquely identifies each row in a table. It must be unique and cannot be NULL.
CREATE TABLE orders (
order_id INT PRIMARY KEY, -- Primary key
customer_id INT,
order_date DATE,
total_amount DECIMAL(10,2)
);
Foreign Key
A foreign key creates a link between two tables by referencing the primary key of another table.
CREATE TABLE order_items (
item_id INT PRIMARY KEY,
order_id INT, -- Foreign key referencing orders
product_id INT, -- Foreign key referencing products
quantity INT,
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
Relationships
One-to-One (1:1)
Each record in Table A relates to exactly one record in Table B.
users (1) āāāāāāāāāāā (1) user_profiles
One-to-Many (1:N)
Each record in Table A can relate to many records in Table B.
customers (1) āāāāāāāāāāā (N) orders
Many-to-Many (M:N)
Records in Table A can relate to many records in Table B, and vice versa. Requires a junction table.
students (M) āāāā enrollment (junction) āāāā (N) courses
-- Junction table for many-to-many
CREATE TABLE enrollments (
student_id INT,
course_id INT,
enrollment_date DATE,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
Schema
A schema is the blueprint of your database ā it defines the structure, relationships, and constraints.
Database: university
āāā Schema: students
ā āāā Table: students
ā āāā Table: enrollments
ā āāā Table: grades
āāā Schema: courses
ā āāā Table: courses
ā āāā Table: instructors
āāā Schema: finance
āāā Table: payments
āāā Table: scholarships
Normalization
Normalization organizes tables to reduce data redundancy and improve data integrity.
| Normal Form | Rule |
|---|---|
| 1NF | Each cell contains a single value; each record is unique |
| 2NF | Meets 1NF + all non-key columns depend on the entire primary key |
| 3NF | Meets 2NF + no transitive dependencies |
ā ļø While normalization reduces redundancy, over-normalizing can hurt performance due to excessive JOINs. Find the right balance for your use case.
āļø Exercise: Create a table called 'courses' with columns: course_id (PRIMARY KEY), course_name, credits, department.
See Solution
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100),
credits INT,
department VARCHAR(50)
);
ā Key Takeaways
- Tables store data in rows and columns with defined data types
- Primary keys uniquely identify each row; foreign keys create table relationships
- Three relationship types exist: one-to-one, one-to-many, and many-to-many
- Schema defines the database structure and constraints
- Normalization reduces redundancy but balance it with performance needs