CSC 282 · Database Programming
Our notes
2 min readWhat SQL is
SQL (Structured Query Language) is the standard language for relational databases. It has parts:
- DDL (data definition): CREATE, ALTER, DROP.
- DML (data manipulation): SELECT, INSERT, UPDATE, DELETE.
- DCL (control): GRANT, REVOKE.
- TCL (transactions): COMMIT, ROLLBACK.
Keywords are not case sensitive; statements end with a semicolon. Each DBMS adds its own extras (types, functions), but the core is the same.
Common data types
INT, DECIMAL(p, s) for money and grades, VARCHAR(n) for text, CHAR(n) fixed text, DATE, DATETIME, BOOLEAN (or TINYINT 0/1 in MySQL).
Creating tables
CREATE TABLE department (
dept_code VARCHAR(10) PRIMARY KEY,
dept_name VARCHAR(80) NOT NULL UNIQUE
);
CREATE TABLE student (
matric_no VARCHAR(15) PRIMARY KEY,
surname VARCHAR(40) NOT NULL,
other_names VARCHAR(60),
dept_code VARCHAR(10) NOT NULL,
level INT CHECK (level BETWEEN 100 AND 600),
FOREIGN KEY (dept_code) REFERENCES department(dept_code)
);
CREATE TABLE result (
matric_no VARCHAR(15),
course_code VARCHAR(10),
session VARCHAR(9),
score INT CHECK (score BETWEEN 0 AND 100),
PRIMARY KEY (matric_no, course_code, session),
FOREIGN KEY (matric_no) REFERENCES student(matric_no)
);
Constraints: PRIMARY KEY, FOREIGN KEY … REFERENCES, NOT NULL, UNIQUE, CHECK, DEFAULT.
Changing and removing tables
ALTER TABLE student ADD COLUMN phone VARCHAR(14);
ALTER TABLE student DROP COLUMN phone;
DROP TABLE result;
DROP removes the table and all its data. Tables referenced by foreign keys must be dropped after the tables that refer to them (or the constraint removed first).
Indexes and views
CREATE INDEX idx_student_surname ON student(surname);
CREATE VIEW cs_students AS
SELECT matric_no, surname FROM student WHERE dept_code = 'CSC';
A view is a stored query that behaves like a table: a way to give each user only the columns and rows they need.
Permissions
GRANT SELECT ON cs_students TO exams_officer;
REVOKE SELECT ON cs_students FROM exams_officer;
Exams usually ask
- Write CREATE TABLE with a primary key, a foreign key and a CHECK.
- Explain the difference between DROP, DELETE and TRUNCATE (DROP removes the table; DELETE removes chosen rows; TRUNCATE empties the table quickly).
- Define a view and say why it is useful.
Common mistake
Creating the result table before the student table it references, or dropping in the wrong order. Create parents first, drop children first.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Data definition language
(opens en.wikipedia.org in a new tab)
Wikipedia, Data definition language, CC BY-SA 4.0 · licence
-
SQL
(opens en.wikipedia.org in a new tab)
Wikipedia, SQL, CC BY-SA 4.0 · licence
-
NOUN CIT 304: Data Management
NOUN course book (PDF, about 6.9 MB: best on Wi-Fi). Files, databases, the relational model and SQL.
(opens nou.edu.ng in a new tab)
Watch
-
SQL Create Table and Insert Data - A Complete Tutorial
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
Creating tables and adding data in Microsoft SQL Server - using GUI and T-SQL code
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
SQL Tutorial - 9: Create Table Statement
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
SQL Tutorial - Full Database Course for Beginners
4 h 21 min · ≈ 1.1 GB · about ₦660
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.