Primary Key
Primary Key
A primary key is a unique identifier for each record in a table. It ensures that
no two rows have the same value in that column. Key characteristics:
Unique: Every value must be distinct.
Non-null: Cannot have empty values.
Stable: Should not change frequently.
Example:
A Students table might use StudentID as the primary key: | StudentID | Name |
Age | |-----------|----------|-----| | 101 | Alice | 21 | | 102 | Bob | 22 |
Each student has a unique StudentID, making it the primary key.
Foreign Key
A foreign key establishes a link between two tables by referencing the primary
key of another table. It ensures referential integrity, meaning data remains
consistent across tables.
Example:
Consider an Enrollment table where StudentID (foreign key) connects to the
Students table: | EnrollmentID | StudentID | Course |
|-------------|----------|--------| | 1 | 101 | Math | | 2 | 102 | Science |
Data Integrity
Data integrity ensures that data is accurate, reliable, and consistent
throughout its lifecycle, preventing corruption or unauthorized
modifications.
Types of Data Integrity:
1. Entity Integrity – Ensures that each table has a unique primary key.
2. Referential Integrity – Maintains correct relationships between tables
using foreign keys.
3. Domain Integrity – Ensures data follows predefined rules, such as valid
formats and constraints.
4. User-Defined Integrity – Custom rules enforced by businesses to meet
specific needs.
Example:
If a banking system has a constraint preventing negative balances:
CREATE TABLE Accounts (
AccountID INT PRIMARY KEY,
Balance DECIMAL(10,2) CHECK (Balance >= 0)
);
This prevents invalid transactions and keeps data meaningful.
Maintaining both data redundancy and data integrity in balance is crucial
for database efficiency. Want to see how normalization can help with these
issues? 🚀
Database Normalization
Database normalization is a process used to organize data efficiently by
eliminating redundancy and ensuring consistency. It involves dividing a
database into smaller, well-structured tables while maintaining proper
relationships between them.
Why Normalize a Database?
Reduces Data Redundancy – Prevents duplication, saving storage space.
Improves Data Integrity – Ensures consistency and accuracy across
tables.
Enhances Query Performance – Optimizes retrieval by eliminating
unnecessary complexities.
Prevents Anomalies – Avoids update, insertion, and deletion errors.
Normalization Forms
There are different levels of normalization, each improving data structure
further:
1. First Normal Form (1NF) – Ensures atomicity (each column contains only
indivisible values) and uniqueness (each row is distinct).
2. Second Normal Form (2NF) – Removes partial dependencies (every
column must fully depend on the primary key).
Third Normal Form (3NF) – Eliminates transitive dependencies (columns
should depend only on the primary key).
1. Boyce-Codd Normal Form (BCNF) – A stricter version of 3NF that
ensures all determinants are candidate keys.
2. Fourth Normal Form (4NF) – Removes multi-valued dependencies.
3. Fifth Normal Form (5NF) – Ensures tables are split correctly to avoid data
redundancy.
Example: Moving from Unnormalized Data to 3NF
Imagine a university Courses table before normalization: | CourseID |
CourseName | StudentName | Instructor |
|----------|-----------|-------------|------------| | 1 | Math | Alice | Dr. Smith | | 2
| Science | Bob | Dr. Lee | | 1 | Math | Bob | Dr. Smith |
This structure leads to redundancy because "Math" repeats for multiple
students. Applying normalization, we divide the table:
1NF - Breaking Repeating Groups
CourseID CourseName
1 Math
2 Science
StudentID StudentName
101 Alice
102 Bob
InstructorID InstructorName
201 Dr. Smith
202 Dr. Lee
1NF - Breaking Repeating Groups
CourseID CourseName
1 Math
2 Science
StudentID StudentName
101 Alice
102 Bob
InstructorID InstructorName
201 Dr. Smith
202 Dr. Lee
Database Normalization
StudentID StudentName
101 Alice
102 Bob
InstructorID InstructorName
201 Dr. Smith
202 Dr. Lee
2NF - Creating Relationships
CourseID InstructorID
1 201
2 202
EnrollmentID StudentID CourseID
1 101 1
2 102 2
3 102 1
3NF - Ensuring Full Dependency
By now, every table follows strict dependency rules—each piece of data
belongs in the right place, ensuring efficiency, integrity, and consistency.
Would you like an SQL example showing normalization techniques? 🚀
A database is an organized collection of data that is stored and managed
efficiently. It allows users to store, retrieve, update, and manage
information in a structured way.
Types of Databases
1. Relational Databases (RDBMS) – Store data in tables with rows and
columns. Examples: MySQL, PostgreSQL, SQL Server.
2. NoSQL Databases – Designed for flexible structures like documents or
key-value pairs. Examples: MongoDB, Cassandra.
3. Cloud Databases – Hosted on cloud platforms for scalability and
accessibility. Examples: AWS RDS, Azure SQL Database.
4. Graph Databases – Store relationships between data entities in graph
structures. Examples: Neo4j, ArangoDB.
Key Components
Tables (or Collections) – Structure for storing related data.
Primary & Foreign Keys – Maintain relationships between tables.
Indexes – Improve query performance.
Constraints – Ensure data integrity (e.g., NOT NULL, UNIQUE).
Queries (SQL, NoSQL) – Retrieve and manipulate data.
Example of a Simple Database Table (Students)
StudentID Name Age Major
101 Alice 20 Math
102 Bob 22 Science