runtime
59 / 88

lesson 59 Database engineering · advanced

DDL: create, alter & inspect a table

Create and alter a table, then inspect its structure.

the idea

DDL (data definition language) defines database objects. CREATE TABLE names columns, types and keys; ALTER TABLE adds a column to an existing table. After running DDL, inspect the schema itself (sqlite_schema, pragma_table_info) as well as the rows you insert.

Dataset: Developer Platform

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

pull_requests10 columns · 13,242 rows
pull_request_idnumber · unique idrepository_idnumber · links to repositoriesauthor_idnumber · links to developerstitletextcreated_atdate & timemerged_atdate & timeclosed_atdate & timestatetextadditionsnumberdeletionsnumber
1124Fix rate limiter2024-01-27 05:50:25NULL2024-02-16 05:29:35closed4710
2158Fix dashboard query2024-02-10 17:45:142024-02-11 12:07:272024-02-11 12:07:27merged165
319Fix cache invalidation2024-02-12 12:03:262024-02-12 18:28:292024-02-12 18:28:29merged763365
4124Migrate retry logic2024-02-14 09:27:382024-02-16 01:04:572024-02-16 01:04:57merged16153
519Remove login flow2024-03-10 14:32:052024-03-11 04:00:502024-03-11 04:00:50merged396129
6158Migrate cache invalidation2024-03-12 10:11:262024-03-12 17:06:252024-03-12 17:06:25merged7151
pull_request_idnumber · unique id
1
repository_idnumber · links to repositories
1
author_idnumber · links to developers
24
titletext
Fix rate limiter
created_atdate & time
2024-01-27 05:50:25
merged_atdate & time
NULL
closed_atdate & time
2024-02-16 05:29:35
statetext
closed
additionsnumber
47
deletionsnumber
10
pull_request_idnumber · unique id
2
repository_idnumber · links to repositories
1
author_idnumber · links to developers
58
titletext
Fix dashboard query
created_atdate & time
2024-02-10 17:45:14
merged_atdate & time
2024-02-11 12:07:27
closed_atdate & time
2024-02-11 12:07:27
statetext
merged
additionsnumber
16
deletionsnumber
5
pull_request_idnumber · unique id
3
repository_idnumber · links to repositories
1
author_idnumber · links to developers
9
titletext
Fix cache invalidation
created_atdate & time
2024-02-12 12:03:26
merged_atdate & time
2024-02-12 18:28:29
closed_atdate & time
2024-02-12 18:28:29
statetext
merged
additionsnumber
763
deletionsnumber
365
pull_request_idnumber · unique id
4
repository_idnumber · links to repositories
1
author_idnumber · links to developers
24
titletext
Migrate retry logic
created_atdate & time
2024-02-14 09:27:38
merged_atdate & time
2024-02-16 01:04:57
closed_atdate & time
2024-02-16 01:04:57
statetext
merged
additionsnumber
161
deletionsnumber
53
pull_request_idnumber · unique id
5
repository_idnumber · links to repositories
1
author_idnumber · links to developers
9
titletext
Remove login flow
created_atdate & time
2024-03-10 14:32:05
merged_atdate & time
2024-03-11 04:00:50
closed_atdate & time
2024-03-11 04:00:50
statetext
merged
additionsnumber
396
deletionsnumber
129
pull_request_idnumber · unique id
6
repository_idnumber · links to repositories
1
author_idnumber · links to developers
58
titletext
Migrate cache invalidation
created_atdate & time
2024-03-12 10:11:26
merged_atdate & time
2024-03-12 17:06:25
closed_atdate & time
2024-03-12 17:06:25
statetext
merged
additionsnumber
71
deletionsnumber
51

first 6 of 13,242 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Read a stored CREATE statement. Run this. SQLite keeps the DDL for pull_requests in sqlite_schema—this is how you inspect how an existing table was defined.

    step-1.sql
    SELECT sql
    FROM sqlite_schema
    WHERE name = 'pull_requests';
  2. Create a table and insert rows. This lab uses a disposable copy of the database. You define release_notes, insert a row, then SELECT it back to confirm DDL and DML worked together.the pattern

    example.sql
    CREATE TABLE release_notes (note_id INTEGER PRIMARY KEY, repository_id INTEGER NOT NULL REFERENCES repositories(repository_id), body TEXT NOT NULL);
    INSERT INTO release_notes VALUES (1, 1, 'First release');
    SELECT *
    FROM release_notes;

common mistake

A SELECT that returns the expected constants does not create the table; the check inspects the schema.

challenge 1

Create review_checklist with item_id INTEGER PRIMARY KEY, pull_request_id INTEGER NOT NULL referencing pull_requests(pull_request_id), and item TEXT NOT NULL.

Insert (1, 1, 'Tests pass') and (2, 1, 'Docs updated'), then return item_id, pull_request_id and item.

what to return

Columns
item_idpull_request_iditem in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
DDL: create, alter & inspect a table — Database engineering · Runtime