简体   繁体   中英

Relationship between database tables

I've got a question relating databases which i can't find the anwser for myself. Currently i got a situation where i have a database with two tables. The two tables are named items and items_sequences. What i want to do is make a relationship between the primary key of the item database and a field in the items_sequences table. Below an illustrated example.

| Items |          |  items_sequences |          
|-------|          |------------------|          
|  id   |          | sequence_id      |          
|  foo  |          | counter          |   

The point is creating the relationship isnt hard. But what i want to do if an Item is made an item sequence should be also made containing a sequence_id with the primary key of the Item itself. And if a Item is deleted the item_sequence should be deleted as well. By the way this should be a 1:1 relationship.

I forgot how this can be done, i would really appreciate if someone would point me to the right direction. Currently i am using a postgresql database if it matters.

Thanks,

Antek

If I get this right, you want to look into using foreign keys and triggers .

If it's a 1:1 relationship, could you not just combine the tables?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM