- Creates a new table with specified columns and data types.
- Syntax:
create table <table> (<col> <type> [primary key|unique|not null], ..., [primary key(<col,...>)], [unique(<col,...>)], [foreign key(<col,...>) references <table>(<col,...>) [on delete restrict|cascade|set null|no action] [on update restrict|cascade|set null|no action]]) - Examples:
create table users (id int primary key, name text not null, age int)create table sessions (user_id int, device text, token text, primary key(user_id,device), unique(token))create table orders (id int, user_id int, foreign key(user_id) references users(id))create table order_items (id int, order_id int, foreign key(order_id) references orders(id) on delete cascade on update cascade)create table sessions (id int, user_id int, foreign key(user_id) references users(id) on delete set null on update no action)
- Alters constraints on an existing table.
- Syntax:
alter table <table> add unique(<col,...>)alter table <table> drop unique(<col,...>)alter table <table> add foreign key(<col,...>) references <table>(<col,...>) [on delete restrict|cascade|set null|no action] [on update restrict|cascade|set null|no action]alter table <table> drop foreign key(<col,...>) references <table>(<col,...>)alter table <table> alter column <col> set not nullalter table <table> alter column <col> drop not null
- Notes:
create/alter tableare auto-commit operations and are rejected inside active transactions.add unique(...)andadd foreign key(...)validate existing table rows.set not nullvalidates existing rows and fails if any row hasnullin that column.
- Creates/drops a secondary (non-unique) index.
- Syntax:
create index on <table> (<col,...>)drop index on <table> (<col,...>)
- Notes:
- Current planner uses single-column equality indexes for
select,update, anddeletewhere possible. - Index entries skip rows where indexed column values are
null.
- Current planner uses single-column equality indexes for
- Inserts one row into a table.
- Syntax:
insert into <table> values (<val>, <val>, ...) - Example:
insert into users values (1, "Alice", 30)
- Syntax:
begincommitrollback
- Notes:
insert,update,deletecan be grouped in one transaction.create table,alter table,create index,drop indexare auto-commit operations and are not allowed inside an active transaction.commitvalidates deferredforeign key ... no actionconstraints before finalizing.- If that validation fails, commit is rejected and the transaction state is rolled back.
- Updates one or more columns for rows matching a WHERE condition.
- Syntax:
update <table> set <col> = <value> [, <col> = <value> ...] where <column> <operator> <value> - Examples:
update users set name = "Ravi" where id = 1update users set name = "Ravi", age = 25 where id eq 1
- Deletes rows matching a WHERE condition.
- Syntax:
delete from <table> where <column> <operator> <value> - Examples:
delete from users where id = 1delete from users where name like "r?m"
- Retrieves all or selected columns.
- Syntax:
select <col1,col2|*> from <table> [where <column> <operator> <value>] [order by <column> [asc|desc]] [limit <n>] - Examples:
select * from usersselect id,name from usersselect name from users where age gte 18select id,name from users order by age desc limit 10
- Equality (int/text):
=oreq - Numeric only:
>orgt,<orlt,>=orgte,<=orlte - Text pattern matching only:
like
*matches zero or more characters?matches exactly one character- Examples:
- Starts with:
"ra*" - Ends with:
"*ir" - Contains:
"*av*" - Exact:
"ram" - Single-char:
"r?m","??vi"
- Starts with:
- Column constraints:
primary key(impliesnot null)uniquenot null
- Table constraints (composite):
primary key(col1,col2,...)unique(col1,col2,...)foreign key(col1,col2,...) references parent_table(parent_col1,parent_col2,...) [on delete restrict|cascade|set null|no action] [on update restrict|cascade|set null|no action]
- Rules:
- Only one primary key constraint is allowed per table.
- Composite primary key must be declared as table-level
primary key(...). - Foreign key referenced columns must be a parent
primary keyoruniqueconstraint. on deletedefaults torestrictwhen omitted.on updatedefaults torestrictwhen omitted.on delete cascadedeletes matching child rows when parent rows are deleted.on update cascaderewrites matching child key values when parent key values are updated.on delete set nullsets child FK columns tonullwhen parent rows are deleted.on update set nullsets child FK columns tonullwhen referenced parent keys are updated.no actionis deferred tocommitinside a transaction (can be temporarily violated within tx and fixed before commit).- outside explicit transactions,
no actionbehaves like immediate validation for that statement. - If any FK child column is
null, referential check is skipped for that row.
- WAL records are statement-based (
BEGIN,OP,COMMIT,ROLLBACK). - On startup recovery, only committed transactions are replayed.
- Replayed committed transactions are applied atomically: if replay of a tx fails or violates deferred
no actionchecks, that tx is skipped and prior state is restored.