Create Table documentation, version 10 - Examples

Started by PG Bug reporting formover 7 years ago1 messagesdocs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/10/sql-createtable.html
Description:

I feel there should be an example of creating a table with a foreign key,
since it is pretty common.
Here is a suggestion which follows the established "theme" in the present
documentation :

Define a foreign key constraint on the `did` column of the `distributors`
table, deletion of a record in the `distributors` table will fail if a
record in the `films` table is still referring it.

```SQL
CREATE TABLE films (
code char(5) CONSTRAINT firstkey PRIMARY KEY,
title varchar(40) NOT NULL,
did integer NOT NULL REFERENCES distributors (did) ON DELETE
RESTRICT,
date_prod date,
kind varchar(10),
len interval hour to minute
);

CREATE TABLE distributors (
did integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name varchar(40) NOT NULL CHECK (name <> '')
);
```