UNIHIVE

Study Hub UNIPORT CSC 282

CSC 282 · Database Programming

SQL: creating and changing tables

SQL's parts, data types, CREATE TABLE with constraints, ALTER, DROP and indexes.

Our notes

2 min read

What 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

Watch

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.

All CSC 282 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.