You can make it a bit more efficient and add more rules depending on your requirements. Trailer=# insert into combine values (1,'') ĮRROR: insert or update on table "donkey" violates foreign keyĭETAIL: Key (others)=(3) is not present in table "harvester". (delete from donkey where donkey.id=new.id insert into donkey select Trailer=# create rule combine_insert as on insert to combine do instead Trailer=# create view combine as select id, array_agg(others) as others Trailer=# create unique index donkey_ears on donkey (id, others) Trailer=# create table donkey (id int, others int references Trailer=# insert into harvester values (1,'grain'), (2,'cricket') PostgreSQL FOREIGN KEY example Let us consider two tables vendors and to illustrate the FOREIGN KEY in PostgreSQL. Here is how: trailer=# create table harvester (id int unique, label text) A foreign key constraint, also known as Referential integrity Constraint, specifies that the values of the foreign key correspond to actual values of the primary key in the other table. I then made it possible to insert into the view by adding an appropriate rule. Mostly, broad strokes, it’s the same as with SQL Server. The user table was then a view of the hidden table with all the roles assembled into an array. Manipulating data within PostgreSQL is one of the easiest things I’ve learned so far. Essentially I made a hidden table that contained one row for each (user,role) pair with suitable constraints. I just made something similar for a colleague.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |