Files
supabase/apps/temp-docs/docs/guides/database/tables.mdx
T
2022-06-27 16:26:42 -02:30

132 lines
9.0 KiB
Plaintext

---
id: tables
title: Tables
description: Creating and using Postgres tables.
---
import Tabs from '@theme/Tabs';
import TabsPanel from '@theme/TabsPanel';
In Postgres, data is stored in "tables".
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 the "data type".
- Tables can be "joined" together. For example you can have a `users` table, which is joined to a `teams` table (because users belong to teams).
For example, this table has 3 "columns" (`id`, `name`, `description`) and 4 "rows" of data:
| `id` | `name` | `description` |
| ---- | ---- | ----------- |
| 1 | Toy Story | When a new toy called "Forky" joins Woody and the gang, a road trip alongside old and new friends reveals how big the world can be for a toy. |
| 2 | Monsters, Inc. | In order to power the city, monsters have to scare children so that they scream. However, the children are toxic to monsters. |
| 3 | Finding Nemo | After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets out on a journey to bring him home. |
| 4 | WALL-E | In the distant future, a small waste-collecting robot inadvertently embarks on a space journey that will ultimately decide the fate of mankind. |
## Creating Tables
Supabase provides several options for creating tables. You can use the Table Editor or create them directly using SQL.
We provide a SQL editor within the Dashboard, or you can [connect](/docs/guides/database/connecting/connecting-to-postgres) to your database
and run the SQL queries yourself.
<Tabs
defaultActiveId="UI"
values={[
{label: 'UI', value: 'UI'},
{label: 'SQL', value: 'SQL'},
]}>
<TabsPanel id="UI" label="UI">
```sh
1. Go to the "Table editor" section.
2. Click "New Table".
3. Enter the table name "todos".
4. Click "Save".
5. Click "New Column".
6. Enter the column name "task" and make the type "text".
7. Click "Save".
```
<video width="99%" muted playsInline controls="true">
<source src="/docs/videos/api/api-create-table-sm.mp4" type="video/mp4" muted playsInline />
</video>
</TabsPanel>
<TabsPanel id="SQL" label="SQL">
```sql
-- Create a table called "todos" with a column to store tasks.
create table todos (
id bigint generated by default as identity primary key,
task text check (char_length(task) > 3)
);
```
</TabsPanel>
</Tabs>
### Tips
- It is best practice to use lowercase and underscores when naming tables. For example: `table_name`, not `Table Name`.
- Tables belong to `schemas`. If you don't explicitly pass the schema, Postgres will assume that you want to create the table in the `public` schema.
### Data types
Every column is a predefined type. PostgreSQL provides many [default types](https://www.postgresql.org/docs/current/datatype.html), and you can even design your own (or use extensions) if the default types don't fit your needs. Here's a list of all of the default data types provided by Postgres:
<details>
<summary>Show/Hide default data types</summary>
| `Name` | `Aliases` | `Description` |
| ------------------------------------------- | -------------------- | ----------------------------------------------------------------- |
| bigint | int8 | Signed eight-byte integer. For numbers ranging from -9223372036854775808 to +9223372036854775807. |
| bigserial | serial8 | Autoincrementing eight-byte integer. For numbers ranging from 1 to 9223372036854775807. |
| bit | | Fixed-length bit string. |
| bit varying | varbit | Variable-length bit string. |
| boolean | bool | Logical Boolean (true/false). |
| box | | Rectangular box on a plane. |
| bytea | | Binary data (“byte array”). |
| character | char | Fixed-length character string. |
| character varying | varchar | Variable-length character string. |
| cidr | | IPv4 or IPv6 network address. |
| circle | | Circle on a plane. |
| date | | Calendar date (year, month, day). |
| double precision | float8 | Double precision floating-point number (8 bytes). 15 decimal digits of precision. |
| inet | | IPv4 or IPv6 host address. |
| integer | int, int4 | Signed four-byte integer. -2147483648 to +2147483647. |
| interval \[ fields \] | | Time span. |
| json | | Textual JSON data. |
| jsonb | | Binary JSON data, decomposed. If you are doing a lot of JSON manipulation in Postgres you should consider using JSONB for efficiency. Otherwise, if you are just doing typical data storing and fetching, JSON should be fine. |
| line | | Infinite line on a plane. |
| lseg | | Line segment on a plane. |
| macaddr | | MAC (Media Access Control) address. |
| macaddr8 | | MAC (Media Access Control) address (EUI-64 format). |
| money | | currency. |
| numeric | decimal | Exact numeric of selectable precision. Up to 131072 digits before the decimal point; up to 16383 digits after. For storing number that require very high precision. Use the numeric type when you need more precision than float4 or float8 offer. |
| path | | Geometric path on a plane. |
| pg\_lsn | | PostgreSQL Log Sequence Number. |
| pg\_snapshot | | User-level transaction ID snapshot. |
| point | | Geometric point on a plane. |
| polygon | | Closed geometric path on a plane. |
| real | float4 | Single precision floating-point number (4 bytes). 6 decimal digits precision. |
| smallint | int2 | Signed two-byte integer. For numbers ranging from -32768 to +32767. |
| smallserial | serial2 | Autoincrementing two-byte integer. For numbers ranging from 1 to 32767. |
| serial | serial4 | Autoincrementing four-byte integer. For numbers ranging from 1 to 2147483647. |
| text | | Variable-length character string. This is the general purpose data type for textual content. |
| time \[ without time zone \] | | Time of day (no time zone). |
| time with time zone | timetz | Time of day, including time zone. |
| timestamp \[ without time zone \] | | Date and time (no time zone). |
| timestamp with time zone | timestamptz | Date and time, including time zone. |
| tsquery | | Text search query. |
| tsvector | | Text search document. |
| txid\_snapshot | | User-level transaction ID snapshot (deprecated; see pg\_snapshot). |
| uuid | | Universally unique identifier. |
| xml | | XML data. |
</details>