Does the foreign keys automatically get updated as primary table is updated?

☆樱花仙子☆ 提交于 2019-12-04 13:30:04

问题


Above is my simple database design, just wanted to gain information about how things happen as I'm really new at database. Following are my questions:

  1. as I update wall_id in walls table, does the wall_id in wall_categories table also get updated? as the wall_id in wall_categories table references to wall_id in walls table.

  2. same with desktop_id in walls table since it is a foreign key referencing to desktop_id in desktop_wall table, so when I update desktop_id in walls table does the desktop_id in deskotp_wall also gets updated?

  3. if it does not update by default, how can this be done?

Thanks a lot!


回答1:


This feature is called cascading referential integrity. It's optional when you define a foreign key constraint. The syntax to enable it is described here (Micorosoft SQL but the syntax is standard and most DBMSs support it):

http://technet.microsoft.com/en-us/library/ms186973(v=sql.105).aspx




回答2:


In postgresql I write like this

ALTER TABLE a_table
ADD CONSTRAINT fk_a_b FOREIGN KEY (a_id)
REFERENCES b_table (b_some_id) 
ON DELETE CASCADE ON UPDATE CASCADE;

and get that fileds are updated and deleted with cascaded way.




回答3:


No the foreign key is not updated automatically. You need to update the foreign key in the tables in which it is referenced by yourself else it would result in referential integrity exception.

For updating the foreign key automatically you may use TRIGGERS.

EDIT:-

As suggested by sqlvogel in comments if your DBMS supports cascading updates the you dont need to create TRIGGERS for that.

In that case it would be better if you have Cascading Referential Integrity Constraints

By using cascading referential integrity constraints, you can define the actions that the SQL Server takes when a user tries to delete or update a key to which existing foreign keys point.




回答4:


  1. As I update wall_id in walls table, does the wall_id in wall_categories table also get updated? as the wall_id in wall_categories table references to wall_id in walls table.

  2. same with desktop_id in walls table since it is a foreign key referencing to desktop_id in desktop_wall table, so when I update desktop_id in walls table does the desktop_id in deskotp_wall also gets updated?

No, the updates won't happen automatically. In fact, you would get a SQLException because you're trying to break referential integrity and hence your updates would fail. This assuming you have valid Constraints applied to the tables.


There's an ON UPDATE CASCADE too but it's not supported on all databases. While, a cascading update should obviously be preferred; if your database doesn't support it you would need to implement an after-row Trigger.

For example, in Oracle you would implement this as:

CREATE OR REPLACE TRIGGER walls_wall_upd_trg
 AFTER UPDATE OF wall_id ON walls FOR EACH ROW
BEGIN
    UPDATE wall_categories
       SET wall_id = :new.wall_id
     WHERE wall_id = :old.wall_id;

    UPDATE wall_comments
       SET wall_id = :new.wall_id
     WHERE wall_id = :old.wall_id;
END;

CREATE OR REPLACE TRIGGER walls_desk_upd_trg
 AFTER UPDATE OF desktop_id ON walls FOR EACH ROW
BEGIN
    UPDATE desktop_wall
       SET desktop_id = :new.desktop_id
     WHERE desktop_id = :old.desktop_id;
END;



回答5:


No. It is not update automatically. What is the database? What you what?



来源:https://stackoverflow.com/questions/19609649/does-the-foreign-keys-automatically-get-updated-as-primary-table-is-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!