Designing Tables Lesson 19 of 21
SQL CREATE TABLE — Data Types and Constraints
Design your own tables with CREATE TABLE — choose data types, add PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, CHECK and FOREIGN KEY constraints.
So far you've used tables that already existed. Now you'll design your own. A well-designed table protects your data: it refuses negative prices, empty names and orders for customers who don't exist.
CREATE TABLE syntax
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
A first table
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author TEXT,
year INTEGER,
copies INTEGER DEFAULT 1
);
INSERT INTO books (title, author, year) VALUES ('Noli Me Tángere', 'Jose Rizal', 1887);
INSERT INTO books (title, author, year, copies) VALUES ('El Filibusterismo', 'Jose Rizal', 1891, 3);
SELECT * FROM books;
Common data types
| Kind of data | MySQL | SQLite |
|---|---|---|
| Whole numbers | INT | INTEGER |
| Money, exact decimals | DECIMAL(10,2) | REAL or NUMERIC |
| Short text | VARCHAR(100) | TEXT |
| Long text | TEXT | TEXT |
| Date | DATE | TEXT as 'YYYY-MM-DD' |
| Date and time | DATETIME | TEXT as 'YYYY-MM-DD HH:MM' |
| True/false | BOOLEAN (stored as 0/1) | INTEGER 0/1 |
SQLite is relaxed about types; MySQL and others are strict. VARCHAR(100) means text up to 100 characters. DECIMAL(10,2) means up to 10 digits, 2 after the decimal point, which is ideal for pesos.
Constraints: rules for the data
| Constraint | Rule |
|---|---|
PRIMARY KEY | Unique id for each row; can't be NULL |
NOT NULL | Must have a value |
UNIQUE | No two rows can have the same value |
DEFAULT x | Value used when none is given |
CHECK (condition) | Value must pass a test |
FOREIGN KEY ... REFERENCES | Must match a row in another table |
CREATE TABLE members (
id INTEGER PRIMARY KEY,
lrn TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
grade INTEGER CHECK (grade BETWEEN 7 AND 12),
joined TEXT DEFAULT '2026-06-15'
);
INSERT INTO members (lrn, name, grade) VALUES ('136728140001', 'Ana Reyes', 9);
SELECT * FROM members;
-- Try each of these one at a time (remove the -- to run):
-- INSERT INTO members (lrn, name, grade) VALUES ('136728140001', 'Ben', 8); -- duplicate LRN
-- INSERT INTO members (lrn, name, grade) VALUES ('136728140002', NULL, 8); -- no name
-- INSERT INTO members (lrn, name, grade) VALUES ('136728140003', 'Cy', 15); -- grade 15
Remove the -- in front of one of the last lines and run again. The database refuses the bad data with an error message. That's exactly what you want: bad data never gets in.
Foreign keys
A foreign key makes sure a value points to a row that actually exists:
PRAGMA foreign_keys = ON; -- SQLite needs this; MySQL checks automatically
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL
);
CREATE TABLE loans (
id INTEGER PRIMARY KEY,
book_id INTEGER NOT NULL,
borrower TEXT NOT NULL,
due TEXT,
FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE
);
INSERT INTO books (title) VALUES ('Florante at Laura');
INSERT INTO loans (book_id, borrower, due) VALUES (1, 'Carla', '2026-12-10');
SELECT * FROM loans;
-- INSERT INTO loans (book_id, borrower) VALUES (99, 'Dan'); -- no book 99!
ON DELETE CASCADE means: if a book is deleted, delete its loans too, so no loan points to a missing book.
CREATE TABLE IF NOT EXISTS
Creating a table that already exists is an error. Add IF NOT EXISTS to skip it safely:
CREATE TABLE IF NOT EXISTS books (...);
Good table design tips
- Every table gets an
idprimary key. - Use lowercase names with underscores:
order_items,due_date. - Store one fact in one place. Don't copy the customer's name into every order; store
customer_id. - Add
NOT NULLto every column that must always have a value. - Use the right type: numbers as numbers, dates as dates. A phone number is text, though, because it starts with 0 and you never do math with it.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04