Search code examples
sqlcascadecascading-deletesrestrict

SQL Confused over ON UPDATE and ON DELETE


I need to create a database using sql for my reviews site. I tried to use the on delete and on update constraints but I'm not sure if they are right. I created three tables a 'cars'(my review items) 'reviews'(holds the reviews) and a 'users' one. I linked cars_id and users_id with reviews table. I used on update cascade and on delete cascade for both. I'm not sure if that's right. Can someone help me understand them more?


Solution

  • It is a matter of data modelling.

    Ask yourself the question:

    "If I delete a car, do I want to delete the relational records of other tables too?"

    If YES put it to CASCADE etc.

    Or:

    "If I delete a user, do I want to delete his cars too?"

    "If I delete a car, do I want do delete the corresponding user too?"

    Maybe a good idea to read first here:

    http://en.wikipedia.org/wiki/Relational_database

    and then here: http://dev.mysql.com/doc/refman/5.1-olh/de/innodb-foreign-key-constraints.html