Foreign key question

From: Victor Spång Arthursson <victor(at)tosti(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: Foreign key question
Date: 2004-01-15 15:56:48
Message-ID: 6CEB98E4-4773-11D8-B913-00039344A3C4@tosti.dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

New to foreign keys and have some questions…

The first is, i have a language table with a primary key on the fields
lang and relid:

relid | lang | text
-------+------+-------------
11111 | uk | hello
11111 | dk | hej
11111 | de | guten tag
11112 | uk | world
11112 | dk | v&aelig;rld

In another table, texts, I have the following:

id | text
-------+------+
4 | 11112 |

What I want to do is to create a foreign key between the two tables.
But trying to do so, with the following syntax, I get the following
error message:

> ALTER TABLE varer ADD CONSTRAINT varenavn FOREIGN KEY (varenavn)
REFERENCES languages(relid) MATCH FULL ON DELETE CASCADE;

>ERROR: there is no unique constraint matching given keys for
referenced table "languages"
tostipippitest=#

Can't i create a Foreign key to a field that is not defined as UNIQUE?

And if so, are there any other approach to solve this problem?

Sincerely

Victor

PS If someone has a link to a good tutorial I would love to have it DS

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-01-15 15:57:04 Re: parse error in function
Previous Message Stephan Szabo 2004-01-15 15:56:28 Re: Returning large select results from stored procedures