Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion content/references/sqlreference/statements/altertable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>` 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;
```
22 changes: 22 additions & 0 deletions content/references/sqlreference/statements/createtable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>` 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 <name>`.
Naming is not supported for `not null`.

## Options

Create a temporary table that will be dropped when the current client disconnects:
Expand Down