The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Adds a few additions to the tables docs
Copplefe06dc1fd1df
diff --git a/web/docs/guides/database/tables.mdx b/web/docs/guides/database/tables.mdxindex 90cddf01503..353a5fff843 100644--- a/web/docs/guides/database/tables.mdx+++ b/web/docs/guides/database/tables.mdx@@ -9,23 +9,30 @@ import TabItem from '@theme/TabItem';Tables are where you store your data.-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).+Tables are similar to excel spreadsheets. They contain columns & rows of data.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. |+| 1 | The Phantom Menace | Two Jedi escape a hostile blockade to find allies and come across a young boy who may bring balance to the Force. |+| 2 | Attack of the Clones | Ten years after the invasion of Naboo, the Galactic Republic is facing a Separatist movement. |+| 3 | Revenge of the Sith | As Obi-Wan pursues a new threat, Anakin acts as a double agent between the Jedi Council and Palpatine and is lured into a sinister plan to rule the galaxy. |+| 4 | Star Wars | Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee and two droids to save the galaxy from the Empire's world-destroying battle station. |+There are a few important differences from a spreadsheet, but it's a good starting point if you're new to Relational databases.## Creating Tables++A table is represented as a two dimensional grid of columns and rows. You must define the "data type" for a column when it is created.+++++It's best practice to add columns to a table at the same time that you create it. You can add and remove columns at any time after creating a table.+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 databaseand run the SQL queries yourself.@@ -57,22 +64,21 @@ values={[<TabItem value="SQL">```sql--- Create a table called "todos" with a column to store tasks.--create table todos (+create table movies (id bigint generated by default as identity primary key,- task text check (char_length(task) > 3)+ name text,+ description text);```</TabItem></Tabs>+It is best practice to use lowercase and underscores when naming tables. For example: `table_name`, not `Table Name`.-### Tips+## Columns-- 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.+You must define the "data type" when you create a column.### Data types@@ -133,3 +139,138 @@ if the default types don't fit your needs.</details>+You can "cast" columns from one type to another, however there can be some incompatibilities between types.+For example, if you cast a `timestamp` to a `date`, you will lose all the time information that was previously saved.++### Primary Keys++A table can have a "primary key" - a unique identifier for every row of data. A few tips for Primary Keys:+++- It's recommended to create a Primary Key for every table in your database.+- You can use any column as a primary key, as long as it is unique for every row.+- It's common to use a `uuid` type or a numbered `identity` column as your primary key.++```sql+create table movies (+ id bigint generated always as identity primary key+);+```++In the example above, we have:++1. created a column called `id`+1. assigned the data type `bigint`+1. instructed the database that this should be `generated always as identity`, which means that Postgres will automatically assign a unique number to this column.+1. Becuase it's unique, we can also use it as our `primary key`.++We could also use `generated by default as identity`, which would allow us to insert our own unique values.++```sql+create table movies (+ id bigint generated by default as identity primary key+);+```++## Joining tables with Foreign Keys++Tables can be "joined" together using Foreign Keys.++++This is where the "Relational" naming comes from, as data typically forms some sort of relationship.++In our "movies" example above, we might want to add a "category" for each movie (for example, "Action", or "Documentary").+Let's create a new table called `categories` and "link" our `movies` table.++```sql+create table categories (+ id bigint generated always as identity primary key,+ name text -- category name+);++alter table movies+ add column category_id bigint references categories;+```++You can also create "many-to-many" relationships by creating a "join" table.+For example if you had the following situations:++- You have a list of `movies`.+- A movie can have several `actors`.+- An `actor` can perfom in several movies.++<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'UI', value: 'UI'},+]}>+<TabItem value="SQL">++```sql+create table movies (+ id bigint generated by default as identity primary key,+ name text,+ description text+);++create table actors (+ id bigint generated by default as identity primary key,+ name text+);++create table performances (+ id bigint generated by default as identity primary key,+ movie_id bigint not null references movies,+ actor_id bigint not null references actors+);+```++</TabItem>+<TabItem value="UI">++<iframe+className="w-full video-with-border"+width="640"+height="420"+src="https://www.youtube-nocookie.com/embed/TKwF3IGij5c"+frameBorder="1"+allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"+allowFullScreen+></iframe>++</TabItem>+</Tabs>+++## Schemas++Tables belong to `schemas`. Schemas are a way of organizing your tables, often for security reasons.++++If you don't explicitly pass a schema when creating a table, Postgres will assume that you want to create the table in the `public` schema.++We can create schemas for organizing tables. For example, we might want a private schema which is hidden from our API:+++```sql+create schmea private;+```++Now we can create tables inside the `private` schema:++```sql+create table salaries (+ id bigint generated by default as identity primary key,+ salary bigint not null,+ actor_id bigint not null references public.actors+);+```++## Resources++- [Official Docs: Create table](https://www.postgresql.org/docs/current/sql-createtable.html)+- [PostgreSQL Tutorial: Create tables](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-create-table/)+- [PostgreSQL Tutorial: Add column](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-add-column/)\ No newline at end of filediff --git a/web/static/img/guides/database/foreign-keys.png b/web/static/img/guides/database/foreign-keys.pngnew file mode 100644index 00000000000..1025e6e5ee2Binary files /dev/null and b/web/static/img/guides/database/foreign-keys.png differdiff --git a/web/static/img/guides/database/schema-tables.png b/web/static/img/guides/database/schema-tables.pngnew file mode 100644index 00000000000..6bb2316aa97Binary files /dev/null and b/web/static/img/guides/database/schema-tables.png differdiff --git a/web/static/img/guides/database/tables-columns.png b/web/static/img/guides/database/tables-columns.pngnew file mode 100644index 00000000000..d4d3ab03d3dBinary files /dev/null and b/web/static/img/guides/database/tables-columns.png differ