SQL

Relations

  1. Relation Schema
    • the column heads for the table which contains name of each field and the domain of each field
      • e.g. Students( sid: string, name: string, login: string, age: integer, gpa: real )
        | sid | name | login | age | gpa |
        | :—-: | :—-: | :—-: | :—-: | :—-: |
  1. Relation Instance

    • a table with rows and columns

      • e.g.
        | sid | name | login | age | gpa |
        | :—-: | :—-: | :—-: | :—-: | :—-: |
        | 50000 | Dave | dave@cs | 19 | 3.3 |
        | 53666 | Jones | jones@cs | 18 | 3.4 |
        | 53688 | Smith | smith#@ee | 18 | 3.2 |
        | 53650 | Smith | smith@math | 19 | 3.8 |
        | 53831 | Madayan | madayan@music | 11 | 1.8 |
        | 53832 | Guldu | guldu@music | 20 | 2.0 |
    • Reminder:

      1. no two rows are identical
      2. the order in the rows and fields are listed is not important
  1. Definitions:
    1. degree: the number of fields
    2. cardinality: the number of tuples (records)
    3. relational database: a collection of relations with distinct relation names
    4. relational database schema: the collection of schemas for the relations in the database
    5. instance of relational database: a collection of relation instances

Data Definition Language

  1. Create
    • create the Student relation
      1
      2
      3
      4
      5
      6
      7
      Create table Students(
      sid CHAR(20),
      name CHAR(30),
      login CHAR(20),
      age INTEGER,
      gpa REAL
      )
  1. Insert
    • insert a single tuple
      1
      2
      3
      Insert
      into Students (sid, name, login, age, gpa)
      Values (53688, 'Smith', 'smith@ee', 18, 3.2)
    • Noted: it is not necessary to list out the name of fields, but make sure to match the order
  1. Delete
    • delete tuples
      1
      2
      3
      DELETE
      From Students S
      WHERE S.name = 'Smith'
  1. Update
    • modify the column values in an existing row
      1
      2
      3
      UPDATE  Students S
      SET S.age = S.age + 1, S.gpa = S.gpa - 1
      WHERE S.sid = 53688
    • Noted: it would identify all satisfied records then update them
    • e.g.
      1
      2
      3
      UPDATE  Students S
      SET S.gpa = S.gpa - 0.1
      WHERE S.gpa >= 3.3
      | sid | name | login | age | gpa |
      | :—-: | :—-: | :—-: | :—-: | :—-: |
      | 50000 | Dave | dave@cs | 19 | 3.2 |
      | 53666 | Jones | jones@cs | 18 | 3.3 |
      | 53688 | Smith | smith#@ee | 18 | 3.2 |
      | 53650 | Smith | smith@math | 19 | 3.7 |
      | 53831 | Madayan | madayan@music | 11 | 1.8 |
      | 53832 | Guldu | guldu@music | 20 | 2.0 |

Key Constraint

  • certain minimal subset of the fields, which is a unique identifier for a tuple, declare by UNIQUE command
  • designer can also identify a primary key by PRIMARY KEY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Create table Students(
    sid CHAR(20),
    name CHAR(30),
    login CHAR(20),
    age INTEGER,
    gpa REAL,
    UNIQUE(name, age), -- a key: not allow two records have the same name and age
    CONSTRAINT StudentsKey PRIMARY KEY (sid) -- a primary key
    ) -- the constraint name StudentsKey is not necessary to be set

Foreign Key

  • a set of fields in one relation r that is used to refer to a tuple in another relation s

    • e.g.
      | cid | grade | sid |
      | :—-: | :—-: | :—-: |
      | CSC101 | D | 53831 |
      | ERG202 | B | 53832 |
      | IEE321 | A | 53650 |
      | PSY203 | B | 53666 |

    • In this case, sid would be the foreign key

      1
      2
      3
      4
      5
      6
      7
      CREATE TABLE Enrolled{
      sid CHAR(20),
      cid CHAR(20),
      grade CHAR(10),
      PRIMARY KEY (sid, cid),
      FOREIGN KEY (sid) REFERENCES Students
      }
    • Noted:
      1. Cannot insert <55555, ART104, A> because there is no tuple in Students with sid = 55555.
      2. Cannot delete <53666, Jones, ...> because there is a tuple from Enrolled with sid = 53666.

Foreign key violations

  1. Insert:

    • insert a row to Enrolled with a sid value that does not appear in Students
    • The INSERT is reject
  2. Delete & Update:

    • Delete or update a Students row, the row in Enrolled refer to the target can have following operations:
      1. Delete all rows in Enrolled that refer to the delete Students row
      2. Disallow the deletion
      3. Set the sid column in Enrolled row that refers to it to default
      4. Set the sid column in Enrolled row that refers to it to null
  • Operations:

    1. NO ACTION (default)

      • The action (DELETE or UPDATE) is to be rejected
    2. CASCADE

      • If Students row is deleted, all Enrolled rows that refer to it are deleted
    3. SET DEFAULT

      • Switch the Enrolled row to a DEFAULT student
    4. SET NULL

      • Allow the use of NULL as the DEFAULT value
      1
      2
      3
      4
      5
      6
      7
      8
      9
      CREATE TABLE Enrolled{
      sid CHAR(20),
      cid CHAR(20),
      grade CHAR(10),
      PRIMARY KEY (sid, cid),
      FOREIGN KEY (sid) REFERENCES Students
      ON DELETE CASCADE
      ON UPDATE NO ACTION
      }

Logical Database Design

Entity Sets

  1. Attributes:

    • all attributes of that entity
  2. Keys:

    • the primary key and condidate keys (if exist)
  3. Entity Set:

    • Employees: id, name, lot

      1
      2
      3
      4
      5
      6
      CREATE TABLE Employees(
      id CHAR(11),
      name CHAR(30),
      lot INTEGER,
      PRIMARY KEY(id) -- primary key {id}
      )

Relationship Sets

  1. Attributes:

    • the primary key attributes of each participating entity set
    • its own attribute
    • the descriptive attribute
  2. Keys:

    • primary key: the group of primary keys of each participating entity
    • the foreign keys
  3. Entity Table:

    • Employees: id, name, lot
    • Departments: did, dname, budget
    • Locations: address, capacity
  4. Relationship Table:

    • Work_In2: id, did, address, since (primary key: {id, did, address})

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      CREATE TABLE Work_In2(
      -- primary key of each participating entity set
      id CHAR(11),
      did INTEGER,
      address CHAR(20),
      -- its own attribute
      since DATE,
      -- primary key (cannot be null)
      PRIMARY KEY (id, did, address), -- the group of primary keys of each entity
      -- foreign keys
      FOREIGN KEY (id) REFERENCES Employees,
      FOREIGN KEY (did) REFERENCES Departments,
      FOREIGN KEY (address) REFERENCES Locations
      )
  • demonstrating on descriptive attributes

  1. Entity Table:

    • Employees: id, name, lot
  2. Relationship Table:

    • Reoports_To: supervisor_id, subordinate_id (primary key: {supervisor_id, subordinate_id})

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      CREATE TABLE Reoports_To(
      -- the descriptive attributes
      supervisor_id CHAR(11),
      subordinate_id CHAR(11),
      -- primary key
      PRIMARY KEY (supervisor_id, subordinate_id), -- the group of attributes
      -- foreign keys (relate the descriptive attributes to the associated attribute)
      FOREIGN KEY (supervisor_id) REFERENCES Employees(id),
      FOREIGN KEY (subordinate_id) REFERENCES Employees(id),
      )

Relationship Sets (Key Constraints)

  • each department has at most one manager (i.e., no two tuples can have same did but can have same id in Managers table)
  • did is the primary / candidate key but {id, did} is not as it is not minimal
  1. Attributes:

    • all attributes of the entity set with the candidate key
    • the primary key attributes of each participating entity set
    • its own attributes
  2. Keys:

    • primary key: the only required primary key
    • the foreign keys
  3. Entity Table:

    • Employees: id, name, lot
    • Departments: did, dname, budget
  4. Relationship Table:

    • Managers: did, dname, budget, id, since (primary key: {did})
  • Cons: if a lot of departments have no manager, it is wasting space

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    CREATE TABLE Managers(
    -- all attributes of the entity set with the candidate key
    did INTEGER,
    dname CHAR(20),
    budget REAL,
    -- primary key of each entity set
    id CHAR(11),
    -- its own attributes
    since DATE,
    -- primary key (not the set of primary keys in entity sets)
    PRIMARY KEY (did),
    FOREIGN KEY (id) REFERENCES Employees
    )

Relationship Sets (Participation Constraints)

  • each department has exactly one one manager (i.e., id cannot set to be DELETE)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    CREATE TABLE Managers(
    did INTEGER,
    dname CHAR(20),
    budget REAL,
    id CHAR(11),
    since DATE,
    PRIMARY KEY (did),
    -- must have one manager (turn on cannot DELETE)
    FOREIGN KEY (id) REFERENCES Employees ON DELETE NO ACTION
    )

Week Entity Sets

  • The weak entity set always

    • participates in a one-to-many binary relationship
    • has a key constraint
    • has total participation
  • Primary key: {id, pname} as Dependents is a weak entity

  1. Attributes:

    • all attributes of the entity set with the candidate key
    • the primary key attributes of each participating entity set
    • its own attributes
  2. Keys:

    • primary key: the group of primary key in participating entity
    • the foreign keys
  3. Entity Table:

    • Employees: id, name, lot
    • Departments: did, dname, budget
  4. Relationship Table:

    • Managers: did, dname, budget, id, since (primary key: {id, pname})

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      CREATE TABLE Policy(
      -- all attributes of the entity set
      pname CHAR(20),
      age INTEGER,
      -- the primary key of each participating entity
      id CHAR(11),
      -- its own attributes
      cost REAL,
      -- primary key
      PRIMARY KEY (pname,id),
      -- ensure that information about employee's policy and dependents is deleted
      FOREIGN KEY (id) REFERENCES Employees ON DELETE CASCADE
      )

Class Hierarchies

  1. Entity Table:
    • Employees: id, name, lot
    • Hourly-Emps: id, hour_worked, hour_wages
    • Contract-Emps: id, contract_id

Aggregation

  1. Entity Table:

    • Employees: id, name, lot
    • Departments: did, dname, budget
    • Projects: pid, started-on, pbudget
  2. Relationship Table:

    • Monitors: id, pid, did, until (primary key: {id, pid, did})
    • Sponsors: pid, did, since (primary key: {pid, did})