Welcome to our SQL tutorial! Today, we're going to create a Student Database from scratch. By the end of this tutorial, you'll understand how to design and manage a database using SQL, a powerful language for managing and manipulating databases. Let's dive in! š
SQL (Structured Query Language) is a language used to communicate with and manipulate databases. It allows us to perform tasks such as creating tables, inserting data, querying data, and updating data within a database.
Before we start, let's list the tables we need for our Student Database:
The Students table will store information about each student, such as their name, ID, and email.
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);š Note: We've used INT for the ID, VARCHAR for Name and Email. We've set the ID as the primary key, meaning each student has a unique ID.
The Courses table will store information about each course, such as its ID, name, and duration.
CREATE TABLE Courses (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Duration INT
);š Note: We've used INT for the ID and Duration, and VARCHAR for the Name.
The Enrollments table will store information about each student's enrollment in a course, such as the student's ID, the course's ID, and the enrollment date.
CREATE TABLE Enrollments (
ID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE
);š Note: We've used INT for the ID, StudentID, and CourseID, and DATE for the EnrollmentDate.
Now that our tables are created, let's insert some data.
-- Inserting data into Students table
INSERT INTO Students (ID, Name, Email)
VALUES (1, 'John Doe', 'john.doe@example.com'),
(2, 'Jane Smith', 'jane.smith@example.com');
-- Inserting data into Courses table
INSERT INTO Courses (ID, Name, Duration)
VALUES (1, 'Web Development', 12),
(2, 'Data Science', 18);š Note: We've used multiple INSERT statements to insert data into our tables.
Now that we have some data, let's perform some queries to retrieve information.
SELECT * FROM Students;SELECT * FROM Students WHERE ID = 1;SELECT * FROM Students WHERE Email = 'john.doe@example.com';SELECT * FROM Courses;INSERT INTO Enrollments (ID, StudentID, CourseID, EnrollmentDate)
VALUES (1, 1, 1, '2022-01-01');š Note: We've used the IDs 1 for both the student and the course, and a date for the enrollment.
You've now learned how to create a basic Student Database using SQL. You've seen how to create tables, insert data, and query data. With this foundation, you're well on your way to becoming an SQL expert!
What is the primary key in the Students table?