Jed Rembold
Monday, September 29, 2025
Given the table (named employees) to the
right and the query immediately below, what would the output table look
like?
SELECT p1.name, p2.name
FROM employees as p1
LEFT JOIN employees as p2
ON p1.superior id > p2.id
ORDER BY p1.name LIMIT 2;
| id | name | superior_id |
|---|---|---|
| 1 | Bob | NULL |
| 2 | Frank | 1 |
| 3 | Kelly | 1 |
| 4 | Anne | 3 |
| 5 | Tiffany | 2 |
| 6 | Henry | 4 |
| name | name |
|---|---|
| Anne | Kelly |
| Bob | NULL |
| name | name |
|---|---|
| Anne | Kelly |
| Frank | Bob |
| name | name |
|---|---|
| Frank | Bob |
| Kelly | Bob |
| name | name |
|---|---|
| Anne | Bob |
| Anne | Frank |
YYYY-MM-DD to
the table name to make it easier to find later what your latest version
is.CONSTRAINT |||constraint name||| |||CONSTRAINT_TYPE||| |||CONSTRAINT_CONDITIONS|||
CHECK constraint is perhaps the most
straightforward (and general), in that it just checks to see if a
certain condition is trueCREATE TABLE |||example||| (
|||col₁||| INT CHECK (|||col₁||| > 0),
|||col₂||| INT,
|||col₃||| INT,
CONSTRAINT |||second_constraint_name||| CHECK (|||col₂||| > |||col₃|||)
);
NOT NULL constraint is only applied
to columns, not to the entire table!CREATE TABLE |||example||| (
|||col₁||| INT NOT NULL,
|||col₂||| INT
);
CREATE TABLE |||example||| (
|||col₁||| INT UNIQUE,
|||col₂||| INT,
|||col₃||| INT,
CONSTRAINT |||uniq_pair_name||| UNIQUE (|||col₂|||, |||col₃|||)
);
Given the table created as seen below, which insertion command would complete successfully?
CREATE TABLE uc (
id_num INT UNIQUE NOT NULL,
prod_name TEXT UNIQUE,
price NUMERIC(5,2)
wholesale NUMERIC(5,2),
CHECK (price > wholesale),
CHECK (price >= 0)
);
INSERT INTO uc VALUES
(1, 'Steak', 3.22, 5.00),
(2, 'Beans', 4.12, 2.50));
INSERT INTO uc VALUES
(1, 'Steak', 3.22, 1.23),
(2, NULL, 2.65, 1.26));
INSERT INTO uc VALUES
(1, 'Steak', 3.22, 2.78),
(NULL, 'Beans', 4.12, 2.50));
INSERT INTO uc VALUES
(1, 'Steak', -3.22, -5.00),
(2, 'Steak', 4.12, 2.50));
UNIQUE and
NOT NULL is extremely useful in having a
column that gives an unambiguous way to selection a specific row from a
tableSERIAL data types are great for this,
or some people prefer to use UUIDsCREATE TABLE |||example||| (
|||col₁||| TEXT PRIMARY KEY,
|||col₂||| INT
);
CREATE TABLE |||example||| (
|||col₁||| TEXT,
|||col₂||| INT,
|||col₃||| INT,
CONSTRAINT |||comp_key_name||| PRIMARY KEY (|||col₁|||, |||col₂|||)
);
REFERENCESCREATE TABLE |||new_example||| (
|||col₁||| TEXT PRIMARY KEY,
|||col₂||| INT REFERENCES |||example||| (|||col₁|||)
);
CREATE TABLE |||new_example||| (
|||col₁||| TEXT PRIMARY KEY,
|||col₂||| INT,
|||col₃||| INT,
CONSTRAINT |||fkey_pair_name||| FOREIGN KEY (|||col₂|||, |||col₃|||)
REFERENCES |||example||| (|||col₁|||, |||col₂|||)
);
UNIQUE constraint to your foreign key. Does
really show up as often in everyday usage
NULL or the column default...
|||col₂||| INT REFERENCES |||example||| ON DELETE CASCADE
|||col₃||| INT REFERENCES |||example||| ON DELETE SET NULL
...
Suppose you wanted to track your Spotify playlist information in your own database. Questions you may want to be able to answer might include:
In groups of two or three, sketch out table diagrams similar to what we just saw in the previous example (commonly called ERD or Entity Relationship Diagrams), that would allow all these questions to be answered. Your tables should include primary keys and foreign keys where appropriate.
Online sketching resources include: drawsql, ChartDB, dbdiagram, DatabaseDiagram