Antek Drzewiecki
Antek Drzewiecki

Reputation: 544

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

Upvotes: 1

Views: 5596

Answers (2)

Denis de Bernardy
Denis de Bernardy

Reputation: 78463

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

Upvotes: 5

markpsmith
markpsmith

Reputation: 4918

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

Upvotes: 3

Related Questions