简体   繁体   中英

Postgres constraint references

When defining a field, what is the difference between:

,cadastre           integer NOT NULL

,CONSTRAINT fkey_affaire_vl_cadastre FOREIGN KEY(cadastre)
    REFERENCES public.vl_cadastre (obj_id) MATCH SIMPLE
    ON UPDATE RESTRICT ON DELETE RESTRICT

AND directly

,cadastre           integer NOT NULL REFERENCES public.cadastre (obj_id)

Both constructs do create a foreign key. The short expression is called an inline foreign key.

The main difference is that the long expression allows you to choose the name of the constraint, while the first one doesn't. This is handy if you need to later on manipulate the constraint (say, drop it), since you know its name beforehand.

In your code, the first example uses options on update restrict and on delete restrict . This is also supported in the inline form of a foreign key declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM