diff --git a/content/references/sqlreference/statements/altertable.md b/content/references/sqlreference/statements/altertable.md index 87ab7d3..858e41b 100644 --- a/content/references/sqlreference/statements/altertable.md +++ b/content/references/sqlreference/statements/altertable.md @@ -12,6 +12,32 @@ See [PostgreSQL: ALTER TABLE](https://www.postgresql.org/docs/current/sql-altert ```sql ALTER TABLE IF EXISTS movies RENAME COLUMN runlength TO duration; ``` -Rename column of table with `table_name` and `current_column_name` to the `new_column_name`. +Rename column of table with `table_name` and `current_column_name` to the `new_column_name`. If exists checks if the table exists and only tries to rename in the case of existence. +## Constraint statements + +#### `ADD CONSTRAINT` + +Adds a constraint to an existing table. +CedarDB supports named `primary key`, `foreign key`, and `unique` constraints: + +```sql +ALTER TABLE orders ADD CONSTRAINT orders_pk primary key (id); +ALTER TABLE orders ADD CONSTRAINT orders_customer_fk foreign key (customer) references customers (id); +ALTER TABLE orders ADD CONSTRAINT orders_unique unique (customer, item); +``` + +The `CONSTRAINT ` part can be omitted entirely, in which case CedarDB automatically assigns a default name using the same conventions as PostgreSQL: +- Primary key: `tablename_pkey` +- Unique: `tablename_colname_key` +- Foreign key: `tablename_colname_fkey` + +#### `DROP CONSTRAINT` + +Removes a constraint by name, using either an explicit name or the default name: + +```sql +ALTER TABLE orders DROP CONSTRAINT orders_unique; +ALTER TABLE orders DROP CONSTRAINT orders_pkey; +``` diff --git a/content/references/sqlreference/statements/createtable.md b/content/references/sqlreference/statements/createtable.md index fffefb9..29c0f12 100644 --- a/content/references/sqlreference/statements/createtable.md +++ b/content/references/sqlreference/statements/createtable.md @@ -33,6 +33,28 @@ multiple columns: * `foreign key(...) references other_table(...)` * `not null` +The `unique`, `primary key`, and `foreign key` constraints can optionally be given a name using the `constraint` keyword: + +```sql +create table orders ( + id int, + customer int, + item int, + constraint orders_pk primary key (id), + constraint orders_customer_fk foreign key (customer) references customers (id), + constraint orders_item_fk foreign key (item) references items (id), + constraint orders_unique unique (customer, item) +); +``` + +The `constraint ` part can be omitted entirely, in which case CedarDB automatically assigns a default name using the same conventions as PostgreSQL: +- Primary key: `tablename_pkey` +- Unique: `tablename_colname_key` +- Foreign key: `tablename_colname_fkey` + +These default names can be used just like explicit names, e.g., to drop a constraint with `alter table ... drop constraint `. +Naming is not supported for `not null`. + ## Options Create a temporary table that will be dropped when the current client disconnects: