936d9254c0
Restructure the docs into five top-level sections, splitting the application you drive from the database language you build with. - New "Using the playground" section: command-line options; the assistive editor (completion, highlighting, [ERR]/[WRN] indicator, hints, in-line editing); the output pane (scrolling); projects (save/load/new/rebuild); undo/redo & history; export & import; clipboard; getting help. Grounded in the in-app help/usage and ADR-0003/0022/0027. - Reference: seed the remaining topic pages (Columns, Relationships, Indexes, Constraints, Inserting & editing data, Querying & inspecting) with real syntax synopses; worked examples to follow. - Surface the assistive editor on the landing page and in Getting started; restore cross-links now that targets exist. Plan + STYLE updated to the five-section structure. 24 pages, build green, links resolve, content clean; planned features carry "planned" callouts.
102 lines
2.1 KiB
Markdown
102 lines
2.1 KiB
Markdown
---
|
|
title: Tables
|
|
description: Create and drop tables in simple mode and in advanced-mode SQL, including compound primary keys.
|
|
sidebar:
|
|
order: 2
|
|
---
|
|
|
|
A table is the core building block. This page covers creating and removing
|
|
tables; changing a table's columns after it exists is covered in
|
|
[Columns](/reference/columns/).
|
|
|
|
## Create a table (simple mode)
|
|
|
|
The simplest way to create a table is `with pk` on its own — it gives the
|
|
table a ready-made primary-key column named `id` that the database fills in
|
|
for you:
|
|
|
|
```rdbms
|
|
create table authors with pk
|
|
```
|
|
|
|
Prefer to name the key, or give it a specific type? Name it in the `with pk`
|
|
clause:
|
|
|
|
```rdbms
|
|
create table authors with pk author_id(serial)
|
|
```
|
|
|
|
Either way, the rest of the columns are added afterwards with `add column`:
|
|
|
|
```rdbms
|
|
add column to authors: name (text)
|
|
add column to authors: birth_year (int)
|
|
```
|
|
|
|
### Compound primary keys
|
|
|
|
To make the primary key span more than one column, list them, comma
|
|
separated:
|
|
|
|
```rdbms
|
|
create table loans with pk book_id(int), member_id(int)
|
|
```
|
|
|
|
**Syntax**
|
|
|
|
```rdbms-syntax
|
|
create table <Name> with pk [<col>(<type>)[, ...]]
|
|
```
|
|
|
|
## Create a table (advanced mode)
|
|
|
|
In advanced mode you write a full `CREATE TABLE` with every column and
|
|
constraint inline:
|
|
|
|
```sql
|
|
create table authors (
|
|
author_id serial primary key,
|
|
name text not null,
|
|
birth_year int
|
|
)
|
|
```
|
|
|
|
The advanced form also accepts table-level constraints and inline foreign
|
|
keys — see [Constraints](/reference/constraints/) and
|
|
[Relationships](/reference/relationships/).
|
|
|
|
**Syntax**
|
|
|
|
```rdbms-syntax
|
|
create table [if not exists] <Name> (<col> <type> [constraints], ...)
|
|
```
|
|
|
|
## Drop a table
|
|
|
|
```rdbms
|
|
drop table authors
|
|
```
|
|
|
|
In advanced mode you may add `if exists` so removing a table that is not
|
|
there succeeds quietly:
|
|
|
|
```sql
|
|
drop table if exists authors
|
|
```
|
|
|
|
:::caution
|
|
Dropping a table removes its rows as well. Use `undo` if you drop one by
|
|
mistake.
|
|
:::
|
|
|
|
## Renaming a table
|
|
|
|
Renaming a table is available in advanced mode:
|
|
|
|
```sql
|
|
alter table authors rename to writers
|
|
```
|
|
|
|
There is no simple-mode rename verb — switch to advanced mode (or use the
|
|
one-line `:` escape) to rename a table.
|