Editing
Foreign key
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
Aanmaken van foreign keys, zonder actions, en later het wijzigen van de foreign key om wΓ©l een action in the stellen: CREATE TABLE foo(id_foo INT PRIMARY KEY, content TEXT); CREATE TABLE bar(id_bar INT PRIMARY KEY, content TEXT); CREATE TABLE foobar( id_foo INT , id_bar INT , PRIMARY KEY (id_foo,id_bar) , CONSTRAINT fk_id_foo FOREIGN KEY (id_foo) REFERENCES foo(id_foo) -- no action defined, falls back to DEFAULT , CONSTRAINT fk_id_bar FOREIGN KEY (id_bar) REFERENCES bar(id_bar) -- no action defined, falls back to DEFAULT ); -- some data in all tables: INSERT INTO foo(id_foo, content) VALUES(1, 'foo content'); INSERT INTO bar(id_bar, content) VALUES(1, 'bar content'); INSERT INTO foobar(id_foo, id_bar) VALUES(1,1); -- now try to delete some data from "foo": DELETE FROM foo WHERE id_foo = 1; -- fails! -- And this is why: SELECT * FROM foobar WHERE id_foo = 1; -- this record is linking to "foo" BEGIN; -- always within a single transaction to avoid data corruption ALTER TABLE foobar DROP CONSTRAINT fk_id_foo; ALTER TABLE foobar -- what table? ADD CONSTRAINT fk_id_foo -- what constraint name? FOREIGN KEY (id_foo) -- what column for this table? REFERENCES foo(id_foo) -- referencing what table? (in what schema?) ON DELETE CASCADE -- what to do when a delete happens? ON UPDATE CASCADE; -- what to do when an update happens? COMMIT; DELETE FROM foo WHERE id_foo = 1; -- works! SELECT * FROM foobar WHERE id_foo = 1; -- that one is gone as well <hr/> Terug naar: [[Standaard opzetjes]]
Summary:
Please note that all contributions to Webhuis wiki are considered to be released under the GNU Free Documentation License 1.3 or later (see
Project:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Voorpagina
Cobol and PostgreSQL
PostgreSQL
CFEngine
Proxmox
Webhuis Kennisbank
Basale infra
Webhuis bouwstenen
Webhuis configuratie
Webhuis Infra
Webhuis Support
Webhuis Raspberry
Opzet Applicaties
Business Applicaties
Community portal
Current events
Recent changes
Random page
Help
sitesupport
Tools
What links here
Related changes
Special pages
Page information