PostgreSQL/Constraints/FOREIGN KEY — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 13:45, 26 мая 2010

Adding a constraint to an existing table: add foreign key to table

postgres=# CREATE TABLE "books" (
postgres(#      "id"           integer NOT NULL,
postgres(#      "title"        text NOT NULL,
postgres(#      "author_id"    integer,
postgres(#      "subject_id"   integer,
postgres(#      Constraint "books_id_pkey" Primary Key ("id")
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "books_id_pkey" for table "books"
CREATE TABLE
postgres=# -- Adding a constraint to an existing table
postgres=#
postgres=# ALTER TABLE books ADD CONSTRAINT legal_subjects
postgres-#                    FOREIGN KEY (subject_id)
postgres-#                    REFERENCES subjects (id);
ALTER TABLE
postgres=#
postgres=# drop table books;
DROP TABLE
postgres=#



Adding constraints (FOREIGN KEY) to a table

postgres=#
postgres=#
postgres=# -- Creating the books table
postgres=#
postgres=# CREATE TABLE books (
postgres(#               id integer UNIQUE,
postgres(#               title text NOT NULL,
postgres(#               author_id  integer,
postgres(#               subject_id integer,
postgres(#               CONSTRAINT books_id_pkey PRIMARY KEY (id));
ERROR:  relation "books" already exists
postgres=#
postgres=# create table editions (
postgres(#              edition_id integer,
postgres(#              book_id    integer
postgres(#              );
CREATE TABLE
postgres=#
postgres=#
postgres=# -- Adding constraints to a table
postgres=#
postgres=# ALTER TABLE editions
postgres-#                  ADD CONSTRAINT foreign_book
postgres-#                  FOREIGN KEY (book_id) REFERENCES books (id);
ALTER TABLE
postgres=#
postgres=# drop table books cascade;
NOTICE:  drop cascades to constraint foreign_book on table editions
DROP TABLE
postgres=#
postgres=# drop table editions cascade;
DROP TABLE
postgres=#



ALTER TABLE employee ADD FOREIGN KEY (group_id) REFERENCES product_groups

postgres=#
postgres=# CREATE TABLE employee (
postgres(#     ID         int,
postgres(#     name       varchar(10),
postgres(#     salary     real,
postgres(#     start_date date,
postgres(#     city       varchar(10),
postgres(#     region     char(1)
postgres(# );
CREATE TABLE
postgres=#
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (1,  "Jason", 40420,  "02/01/94", "New York", "W");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (2,  "Robert",14420,  "01/02/95", "Vancouver","N");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (3,  "Celia", 24020,  "12/03/96", "Toronto",  "W");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (4,  "Linda", 40620,  "11/04/97", "New York", "N");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (5,  "David", 80026,  "10/05/98", "Vancouver","W");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (6,  "James", 70060,  "09/06/99", "Toronto",  "N");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (7,  "Alison",90620,  "08/07/00", "New York", "W");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (8,  "Chris", 26020,  "07/08/01", "Vancouver","N");
INSERT 0 1
postgres=# insert into employee (ID, name,    salary, start_date, city,       region)
postgres-#               values (9,  "Mary",  60020,  "06/09/02", "Toronto",  "W");
INSERT 0 1
postgres=#
postgres=# select * from employee;
 id |  name  | salary | start_date |   city    | region
----+--------+--------+------------+-----------+--------
  1 | Jason  |  40420 | 1994-02-01 | New York  | W
  2 | Robert |  14420 | 1995-01-02 | Vancouver | N
  3 | Celia  |  24020 | 1996-12-03 | Toronto   | W
  4 | Linda  |  40620 | 1997-11-04 | New York  | N
  5 | David  |  80026 | 1998-10-05 | Vancouver | W
  6 | James  |  70060 | 1999-09-06 | Toronto   | N
  7 | Alison |  90620 | 2000-08-07 | New York  | W
  8 | Chris  |  26020 | 2001-07-08 | Vancouver | N
  9 | Mary   |  60020 | 2002-06-09 | Toronto   | W
(9 rows)
postgres=#
postgres=#
postgres=# ALTER TABLE employee ADD FOREIGN KEY (group_id) REFERENCES product_groups;
ERROR:  relation "product_groups" does not exist
postgres=#
postgres=# \d employee;
            Table "public.employee"
   Column   |         Type          | Modifiers
------------+-----------------------+-----------
 id         | integer               |
 name       | character varying(10) |
 salary     | real                  |
 start_date | date                  |
 city       | character varying(10) |
 region     | character(1)          |
postgres=#
postgres=# drop table employee;
DROP TABLE
postgres=#
postgres=#



Create FOREIGN KEY

postgres=#
postgres=# CREATE TABLE "books" (
postgres(#      "id"           integer NOT NULL,
postgres(#      "title"        text NOT NULL,
postgres(#      "author_id"    integer,
postgres(#      "subject_id"   integer,
postgres(#      Constraint "books_id_pkey" Primary Key ("id")
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "books_id_pkey" for table "books"
CREATE TABLE
postgres=#
postgres=# CREATE TABLE editions
postgres-#               (isbn text,
postgres(#               book_id integer,
postgres(#               edition integer,
postgres(#               publisher_id integer,
postgres(#               publication date,
postgres(#               type char,
postgres(#               CONSTRAINT pkey PRIMARY KEY (isbn),
postgres(#               CONSTRAINT integrity CHECK (book_id IS NOT NULL
postgres(#                                           AND edition IS NOT NULL),
postgres(#               CONSTRAINT book_exists FOREIGN KEY (book_id)
postgres(#                          REFERENCES books (id)
postgres(#                          ON DELETE CASCADE
postgres(#                          ON UPDATE CASCADE);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pkey" for table "editions"
CREATE TABLE
postgres=#
postgres=# drop table books cascade;
NOTICE:  drop cascades to constraint book_exists on table editions
DROP TABLE
postgres=# drop table editions cascade;
DROP TABLE
postgres=#



Define foreign key for a table

postgres=#
postgres=# -- Foreign Keys
postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer PRIMARY KEY,
postgres(#    name text,
postgres(#    price numeric
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "products_pkey" for table "products"
CREATE TABLE
postgres=#
postgres=# CREATE TABLE orders (
postgres(#    order_id integer PRIMARY KEY,
postgres(#    product_no integer REFERENCES products (product_no),
postgres(#    quantity integer
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "orders_pkey" for table "orders"
CREATE TABLE
postgres=#
postgres=# drop table products cascade;
NOTICE:  drop cascades to constraint orders_product_no_fkey on table orders
DROP TABLE
postgres=# drop table orders cascade;
DROP TABLE
postgres=#
postgres=#



Reference two two columns as foreign key column

postgres=#
postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer PRIMARY KEY,
postgres(#    name text,
postgres(#    price numeric
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "products_pkey" for table "products"
CREATE TABLE
postgres=#
postgres=# CREATE TABLE orders (
postgres(#    order_id integer PRIMARY KEY,
postgres(#    shipping_address text
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "orders_pkey" for table "orders"
CREATE TABLE
postgres=#
postgres=# CREATE TABLE order_items (
postgres(#    product_no integer REFERENCES products,
postgres(#    order_id integer REFERENCES orders,
postgres(#    quantity integer,
postgres(#    PRIMARY KEY (product_no, order_id)
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "order_items_pkey" for table "order_items"
CREATE TABLE
postgres=#
postgres=#
postgres=# drop table products cascade;
NOTICE:  drop cascades to constraint order_items_product_no_fkey on table order_items
DROP TABLE
postgres=# drop table orders cascade;
NOTICE:  drop cascades to constraint order_items_order_id_fkey on table order_items
DROP TABLE
postgres=# drop table order_items cascade;
DROP TABLE
postgres=#
postgres=#



Using foreign keys

postgres=#
postgres=# -- Foreign Keys
postgres=#
postgres=# CREATE TABLE cities (
postgres(#        city     varchar(80) primary key,
postgres(#        id       int
postgres(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "cities_pkey" for table "cities"
CREATE TABLE
postgres=#
postgres=# CREATE TABLE weather (
postgres(#        city      varchar(80) references cities(city),
postgres(#        temp_lo   int,
postgres(#        temp_hi   int,
postgres(#        prcp      real,
postgres(#        date      date
postgres(# );
CREATE TABLE
postgres=#
postgres=# insert into cities values ("Berkeley", 0);
INSERT 0 1
postgres=#
postgres=# INSERT INTO weather VALUES ("Berkeley", 45, 53, 0.0, "1994-11-28");
INSERT 0 1
postgres=#
postgres=# drop table weather;
DROP TABLE
postgres=# drop table cities;
DROP TABLE
postgres=#
postgres=#