Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Intuitively, adding the option ON UPDATE CASCADE to a foreign key constraint will have the effect of updating all referencing columns with the updated value of the key. Would that really work?

Example lifted from manual:

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

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    shipping_address text,
    ...
);

CREATE TABLE order_items (
    product_no integer REFERENCES products ON DELETE RESTRICT,
    ## HERE'S THE LINE THAT I MODIFIED FROM THE MANUAL, IT USED TO SAY 'ON DELETE CASCADE'
    order_id integer REFERENCES orders ON UPDATE CASCADE,
    quantity integer,
    PRIMARY KEY (product_no, order_id)
);

I am using Postgres 9.1.

share|improve this question
2  
Have you tried to update a few rows? It should work - as advertised. –  ypercube Sep 20 '12 at 17:56
    
Strictly speaking it's not advertised to do that, nor have i found anything in the manual or google about it. Working on a test now –  ted.strauss Sep 20 '12 at 18:03
    
my example code may have been misleading, i've updated the comment –  ted.strauss Sep 20 '12 at 18:05
3  
And you don't have to remove the ON DELETE CASCADE. You can have a foreign key with both behaviours: order_id integer REFERENCES orders ON DELETE CASCADE ON UPDATE CASCADE –  ypercube Sep 20 '12 at 19:16
1  
Why did you look at the manual for 8.2 when you are using 9.1? –  a_horse_with_no_name Sep 20 '12 at 19:49

1 Answer 1

up vote 6 down vote accepted

Possible options for the ON DELETE and ON UPDATE clauses are explained in the CREATE TABLE page of the manual.

The default is NO ACTION, so if you have any foreign keys defined without explicitly specifying what action to be performed on updates of the referenced primary or unique key, it is the same as if ON UPDATE NO ACTION was specified:

NO ACTION

Produce an error indicating that the deletion or update would create a foreign key constraint violation. If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows. This is the default action.


If you have specified ON UPDATE CASCADE on a foreign key, the referencing rows will be updated accordingly when a referenced row is updated:

CASCADE

Delete any rows referencing the deleted row, or update the value of the referencing column to the new value of the referenced column, respectively.

Primary keys are usually not updated (and for good reasons) but there are situations where it can be useful. Plus, the foreign key may be referencing not a primary key but a (rarely updated) unique key.

share|improve this answer
    
For info on why I need to modify primary keys see here stackoverflow.com/a/12518468/241677 –  ted.strauss Sep 20 '12 at 19:12
    
Yes, I can see merging two or more tables/databases (with conflicting primary keys) as a situation where this can be useful. –  ypercube Sep 20 '12 at 19:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.