MySQL RDBMS Database Basics
RDBMS (Relational Database Management System) is a type of database system where data is stored in related tables using rows and columns. MySQL is a popular open-source RDBMS.
In this topic you’ll learn:
name, email).Although RDBMS concepts are logical, MySQL implements them using SQL commands such as CREATE DATABASE, CREATE TABLE, and ALTER TABLE.
SELECT and joins.Let’s create a simple school_db database with a students table.
-- Create a database and basic students table
CREATE DATABASE school_db;
USE school_db;
CREATE TABLE students (
student_id INT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
date_of_birth DATE
);
school_db is the database (container of tables).students is a table that will store student details.student_id is the Primary Key (no duplicates allowed).email is marked UNIQUE, so no two students can share the same email.RDBMS is powerful because you can connect tables instead of duplicating data everywhere.
One student can enroll in many courses. One course can have many students. We’ll start by modeling a One-to-Many relationship using students and enrollments.
-- Students table (already created earlier)
CREATE TABLE students (
student_id INT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL
);
-- Courses table with its own primary key
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL
);
-- Enrollments table uses foreign keys to link students and courses
CREATE TABLE enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
enrollment_date DATE,
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
students has student_id as Primary Key.courses has course_id as Primary Key.enrollments has Foreign Keys linking to both tables.enrollments → One-to-Many from students to enrollments.Constraints ensure data integrity (correctness and reliability of the data).
Here is an example of a payments table with different constraints applied.
-- Payments table with various constraints
CREATE TABLE payments (
payment_id INT PRIMARY KEY,
student_id INT NOT NULL,
amount DECIMAL(10,2) NOT NULL,
payment_date DATE DEFAULT (CURRENT_DATE),
status VARCHAR(20) DEFAULT 'PENDING',
FOREIGN KEY (student_id) REFERENCES students(student_id)
);
amount cannot be NULL → payment must have an amount.payment_date defaults to today if not provided.status defaults to 'PENDING'.student_id must exist in the students table due to the Foreign Key.Normalization is the process of structuring tables to reduce data duplication.
Advantages of using MySQL RDBMS:
To see which student enrolled in which course, we need to fetch data from multiple tables using JOIN.
-- List students with the courses they are enrolled in
SELECT
s.student_id,
s.full_name,
c.course_name,
e.enrollment_date
FROM enrollments e
JOIN students s ON e.student_id = s.student_id
JOIN courses c ON e.course_id = c.course_id
ORDER BY s.student_id;
1 | Alice Johnson | SQL Basics | 2024-06-011 | Alice Johnson | Web Development | 2024-06-052 | Bob Singh | SQL Basics | 2024-06-02Here, data from three different tables is combined to show a clear picture of who is enrolled in which course.
DATE, INT, DECIMAL).enrollment_date instead of date1).library_db.members (member_id PK, name, email)books (book_id PK, title, author)issues (issue_id PK, member_id FK, book_id FK, issue_date, return_date)SELECT query with JOIN to list which member has which book.Bonus: Add a status column in issues (e.g., 'ISSUED', 'RETURNED') with a DEFAULT value and see how it behaves.