Welcome to the world of database design! Databases are essential for storing, managing, and retrieving data efficiently. This section will cover the fundamentals of database design, including key concepts, normalization, and practical examples.
A database is an organized collection of structured information that can be easily accessed, managed, and updated. Databases are crucial for applications that require persistent storage and efficient data retrieval.
Let's dive into some practical examples to understand database design better.
Imagine you're designing a database for a library. The library has books, authors, and members. Here’s how you might structure the database:
Books
book_id (Primary Key)titleauthor_id (Foreign Key)Authors
author_id (Primary Key)nameMembers
member_id (Primary Key)nameemailCREATE TABLE Authors (
author_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE Books (
book_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT,
FOREIGN KEY (author_id) REFERENCES Authors(author_id)
);
CREATE TABLE Members (
member_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE
);
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. Let's normalize a simple example.
CREATE TABLE Students (
student_id INT PRIMARY KEY,
name VARCHAR(100),
course VARCHAR(100),
professor_name VARCHAR(100)
);
This table has redundancy because the same professor’s name is repeated for multiple students in the same course.
CREATE TABLE Professors (
professor_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
professor_id INT,
FOREIGN KEY (professor_id) REFERENCES Professors(professor_id)
);
CREATE TABLE Students (
student_id INT PRIMARY KEY,
name VARCHAR(100),
course_id INT,
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);