DBMSCSL303 / MAL505
Module 2: Core SQL

Lecture 05: SQL DDL & Schemas

SQL Sublanguages, SQLite vs PostgreSQL Data Types, Table Creation, Constraints, and Alteration

Lecture Source Slide Deck

Original Slides: Lecture 05 (22 slides) · Amit Kumar Dhar (IIT Bhilai)
Download lecture05.pdf

1. The Four SQL Sublanguages

SQL is divided into four distinct sublanguages based on operational scope:

+--------------------------------------------------------------------------+
|                                    SQL                                   |
+-------------------+-------------------+------------------+---------------+
|        DDL        |        DML        |       DCL        |      TCL      |
|  Data Definition  | Data Manipulation |   Data Control   |  Transaction  |
+-------------------+-------------------+------------------+---------------+
| CREATE            | SELECT            | GRANT            | COMMIT        |
| ALTER             | INSERT            | REVOKE           | ROLLBACK      |
| DROP              | UPDATE            |                  | SAVEPOINT     |
| TRUNCATE          | DELETE            |                  |               |
+-------------------+-------------------+------------------+---------------+

2. Data Types: SQLite vs. PostgreSQL

Logical TypeSQLite AffinityPostgreSQL Native TypePractical Notes
IntegerINTEGERINT, BIGINT, SMALLINTSQLite uses variable-length 1 to 8-byte ints.
Floating PointREALREAL, DOUBLE PRECISIONIEEE 754 floating point numbers.
Exact DecimalNUMERICNUMERIC(precision, scale)Crucial for currency/financial data (no float rounding errors).
Variable StringTEXTVARCHAR(n), TEXTIn SQLite, VARCHAR(50) is accepted but length is not enforced.
BooleanINTEGER (0 / 1)BOOLEAN (TRUE / FALSE)SQLite stores Booleans internally as 0 and 1.
Date & TimeTEXT (ISO 8601)DATE, TIMESTAMP WITH TIME ZONESQLite stores as 'YYYY-MM-DD HH:MM:SS'.

3. Schema Constraints & Integrity Rules

CREATE TABLE department (
    dept_name   VARCHAR(20) PRIMARY KEY,
    building    VARCHAR(15) NOT NULL,
    budget      NUMERIC(12, 2) CHECK (budget > 0)
);

CREATE TABLE instructor (
    id          CHAR(5) PRIMARY KEY,
    name        VARCHAR(20) NOT NULL,
    dept_name   VARCHAR(20),
    salary      NUMERIC(8, 2) DEFAULT 30000.00 CHECK (salary >= 20000),
    FOREIGN KEY (dept_name) REFERENCES department(dept_name)
        ON DELETE SET NULL
        ON UPDATE CASCADE
);

The 6 Fundamental Constraint Types

  1. PRIMARY KEY:
    • Enforces both UNIQUE and NOT NULL automatically.
    • Creates an automatic primary index (usually B+ Tree).
    • Only one primary key per table (can be composite across multiple columns).
  2. FOREIGN KEY:
    • Enforces referential integrity with a referenced table's primary key or unique column.
    • SQLite note: Foreign key enforcement must be activated per session via:
      PRAGMA foreign_keys = ON;
  3. NOT NULL:
    • Forbids inserting NULL into the attribute.
  4. UNIQUE:
    • Guarantees no two rows share the same non-null value. Multiple NULL values are permitted in standard SQL.
  5. CHECK (condition):
    • Validates that every inserted or modified tuple satisfies a boolean expression (e.g., CHECK (salary > 0 AND salary < 1000000)).
  6. DEFAULT value:
    • Automatically populates the attribute if omitted in an INSERT statement.

4. Modifying and Dropping Tables

-- Add a new column
ALTER TABLE instructor ADD COLUMN email VARCHAR(50);

-- Rename a table
ALTER TABLE instructor RENAME TO faculty;

-- Drop an existing table
DROP TABLE IF EXISTS faculty;

5. Topic-Specific SQL Practice Questions

On this page