Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PostgreSQL does a foreign key constraint only require REFERENCES?

Tags:

sql

postgresql

I was reading the docs on PostgreSQL constraints because I wanted to see how to define foreign keys. In their examples

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer REFERENCES products (product_no),
    quantity integer
);

I don't see FOREIGN KEY anywhere; however, in several other stack overflow questions (How to add "on delete cascade" constraints? for example) I have seen the FOREIGN KEY written. Is it necessary to write FOREIGN KEY or is it only necessary to use REFERENCES?

like image 826
m0meni Avatar asked May 28 '15 02:05

m0meni


People also ask

Can foreign key only reference primary key?

A foreign key can refer to either a unique or a primary key of the parent table. If the foreign key refers to a non-primary unique key, you must specify the column names of the key explicitly.

What is foreign key constraint in PostgreSQL?

A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the referential integrity between two related tables.

Is a foreign key a referential integrity constraint?

Foreign key constraints (also known as referential constraints or referential integrity constraints) enable you to define required relationships between and within tables.

What is the difference between foreign key and reference key?

The Reference Key is the primary key that is referenced in the other table. On the other hand, Foreign Key is how you link the second table to the primary tables Primary Key (or Reference Key).


1 Answers

This is a good question.

You will notice FOREIGN KEY constraint in examples in doc related to DDL-constraints. I prefer to use FOREIGN KEY constraint as noted in Example 3 below.

You could do foreign key/references in different manners:

Parent table

CREATE TABLE products (
    product_no integer PRIMARY KEY,
    name text,
    price numeric
);

Child table - Ex1

Inline foreign key constraint without mentioning FOREIGN KEY

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer REFERENCES products (product_no),
    quantity integer
);

OR

Child table - Ex2

Notice that parent and child table should have the same column name to use this concise notation.

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer REFERENCES products,
    quantity integer
);

OR

Child table - Ex3

Notice that we are explicitly using FOREIGN KEY keyword here.

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer,
    quantity integer,
    FOREIGN KEY (product_no) REFERENCES products (product_no),
);

In case when more than one fields need to be constrainted, FOREIGN KEY constraint can be written like this also:

CREATE TABLE t1 (
  a integer PRIMARY KEY,
  b integer,
  c integer,
  FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)
);

These examples are taken from the docs.

SQL Fiddle Example: http://sqlfiddle.com/#!15/dd2d6

like image 178
zedfoxus Avatar answered Oct 20 '22 01:10

zedfoxus