⬅ Previous Topic
SQL DenormalizationNext Topic ⮕
Primary Key vs Unique Key⬅ Previous Topic
SQL DenormalizationNext Topic ⮕
Primary Key vs Unique KeyEvery successful database starts with a smart plan. Think of it like designing the blueprint of a school building — classrooms, staff rooms, corridors — everything must connect sensibly. In the world of data, that blueprint is called an ER Diagram (Entity Relationship Diagram), and its translation into tables is known as schema design.
An ER Diagram is a visual representation of entities (real-world objects like Student
or Teacher
), their attributes (like name
, marks
), and the relationships between them. It helps you understand how your data fits together — before you start coding tables.
Let’s design a simple school system with the following:
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10)
);
CREATE TABLE subjects (
subject_code VARCHAR(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE marks (
roll_no INT,
subject_code VARCHAR(10),
marks INT,
PRIMARY KEY (roll_no, subject_code),
FOREIGN KEY (roll_no) REFERENCES students(roll_no),
FOREIGN KEY (subject_code) REFERENCES subjects(subject_code)
);
Entity | Mapped Table | Relationship |
---|---|---|
Student | students | 1-to-many with marks |
Subject | subjects | 1-to-many with marks |
Marks | marks | Associative entity between student & subject |
You can expand this design by adding:
ER Diagrams are the heart of any well-designed database. They help you visualize the structure before you write a single line of SQL. Combined with normalization, they prevent confusion, enforce clarity, and pave the way for future scaling. Whether it's a small school app or a large university portal — solid design begins with a great schema.
Coming up: SQL Best Practices — real-world habits and tips to write cleaner, safer, and more efficient SQL code.
⬅ Previous Topic
SQL DenormalizationNext Topic ⮕
Primary Key vs Unique KeyYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.