DATABASE ESSENTIALS
Lecture 8
[email protected]
SQL
• CREATE DATABASE college4;
or
• CREATE DATABASE IF NOT EXISTS college4;
SQL…
Show databases
• SHOW databases;
Switch to the 'college4' database
• USE college4;
SQL…
Create a table for students
CREATE TABLE students (
student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
address VARCHAR(100)
);
SQL…
Create a table for courses
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50),
department VARCHAR(50),
credits INT
);
SQL…
Create a table for enrollment
CREATE TABLE enrollment (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
enrollment_date DATE
);
SQL…
Add some sample data
INSERT INTO students VALUES (1, ‘Juma’, ‘Kulwa', '1990-01-01', ‘Kawe');
INSERT INTO students VALUES (2, ‘Halima', ‘Salehe', '1995-05-15',
‘Temeke');
SQL…
Add some sample data
INSERT INTO courses VALUES (101, 'Introduction to Programming',
'Computer Science', 3);
INSERT INTO courses VALUES (102, 'Database Management', 'Computer
Science', 4);
SQL…
Add some sample data
INSERT INTO enrollment VALUES (1, 1, 101, '2024-01-01');
INSERT INTO enrollment VALUES (2, 1, 102, '2024-01-15');
INSERT INTO enrollment VALUES (3, 2, 101, '2024-02-01');
SQL…
Select some data
Select all columns for all students
SELECT * FROM students;
Select specific columns
SELECT student_id, first_name, last_name FROM students;
Adding conditions to filter the results. Example, select a student with a
specific student_id:
Select all columns for a specific student (e.g., student with id 1)
SELECT * FROM students WHERE student_id = 1;
SQL…
Quiz
Selecting from two or more tables
Update
Delete
“If you are working on
something that you really care
about, you don’t have to be
pushed. The vision pulls you.”
– Steve Jobs