mirror of
https://github.com/supabase/supabase.git
synced 2026-05-23 19:13:13 +08:00
161 lines
2.7 KiB
Plaintext
161 lines
2.7 KiB
Plaintext
---
|
|
id: tables
|
|
title: "Tables"
|
|
slug: tables
|
|
custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/postgres.yml
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs';
|
|
import TabsPanel from '@theme/TabsPanel';
|
|
|
|
Tables are similar to excel spreadsheets. They contain columns & rows of data. There are a few key differences from a spreadsheet however:
|
|
|
|
- Every column is a strict type of data. When you set up a column, you must define what "data type" it is.
|
|
- Tables can be joined together through relationships. For example you can have a "users" table, which is joined to a "teams" table.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabsPanel id="sql" label="sql">
|
|
|
|
```sql
|
|
create table table_name (
|
|
id integer primary key,
|
|
data jsonb,
|
|
name text
|
|
);
|
|
|
|
# with schema
|
|
create table schema_name.table_name (
|
|
id integer primary key,
|
|
data jsonb,
|
|
name text
|
|
);
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
|
|
|
|
|
|
|
|
## Notes
|
|
|
|
- Tables contain columns, rows, triggers, comments,
|
|
- It is best practice to use lowercase and underscores when naming tables. For example: `table_name`, not `Table Name`.
|
|
- Tables belong to [schemas](/docs/reference/postgres/schemas). If you don't explicitly pass the schema, Postgres will assume that you want to create the table in the `public` schema.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
### Create table
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabsPanel id="sql" label="sql">
|
|
|
|
```sql
|
|
create table table_name (
|
|
id integer primary key,
|
|
data jsonb,
|
|
name text
|
|
);
|
|
|
|
# with schema
|
|
create table schema_name.table_name (
|
|
id integer primary key,
|
|
data jsonb,
|
|
name text
|
|
);
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Primary keys using multiple columns
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabsPanel id="sql" label="sql">
|
|
|
|
```sql
|
|
create table table_name (
|
|
column_1 data_type,
|
|
column_2 data_type
|
|
-- Constraints:
|
|
primary key (column_1, column_2)
|
|
);
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Multiple foreign keys to the same table
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabsPanel id="sql" label="sql">
|
|
|
|
```sql
|
|
alter table table_name
|
|
add constraint constraint_name_1 foreign key (column_1) references foreign_table(id),
|
|
add constraint constraint_name_2 foreign key (column_2) references foreign_table(id);
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Delete a table
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabsPanel id="sql" label="sql">
|
|
|
|
```sql
|
|
delete table if exists table_name;
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs> |