473,408 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Foreign key question

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æ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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #1
6 7805
From the HTML docs:
"The referenced columns must be the columns of a unique or primary key
constraint in the referenced table."

I personally don't know of a work around. Maybe some of the others do.

On Thursday 15 January 2004 10:56 am, Victor Spång Arthursson wrote:
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æ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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


--
"The most important reason that people save is the hope of providing a
better life for their children. A society that punishes that impulse
with taxes is foolish. It is draining energy from the single most power-
ful engine of capital accumulation. If the super-rich don't want their
kids to get their money, fine. Donate every penny of it to someone else.
But they are wrong to block others from exercising a free choice."

--Lew Rockwell

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: te***@esc1.com

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #2

On Thu, 15 Jan 2004, [ISO-8859-1] Victor Spång Arthursson wrote:
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æ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"

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


You are not allowed to do so by the SQL spec, no.
And if so, are there any other approach to solve this problem?


I think you could keep the ids for text blocks in a separate table with
all tables that have such an id keeping a reference to it (thus there's a
separate list of valid ids). Unfortunately depending on the behavior you
want, you may have to write triggers to keep the values straight (for
example if you want the id to go away if all the references in a
particular table go away).

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #3
Terry Lee Tucker <te***@esc1.com> writes:
From the HTML docs:
"The referenced columns must be the columns of a unique or primary key
constraint in the referenced table." I personally don't know of a work around. Maybe some of the others do.


There is no workaround, because foreign keys don't make any sense if
there isn't a uniquely identifiable referenced row.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 22 '05 #4
Mensaje citado por Tom Lane <tg*@sss.pgh.pa.us>:
Terry Lee Tucker <te***@esc1.com> writes:
From the HTML docs:
"The referenced columns must be the columns of a unique or primary key
constraint in the referenced table."

I personally don't know of a work around. Maybe some of the others do.


There is no workaround, because foreign keys don't make any sense if
there isn't a uniquely identifiable referenced row.


A better way to understand it is:

Foreign keys are many to one assignments.

Try to think of it as a function (mathematicaly speeking): You can't have an element
from the domain end up on two different elementos of the co-domain.

In simbols:

If f(x) = y and f(x) = z => y = z

Those this bring insight?

P.D.: Aparently you are having problems with your database model.

--
select 'mmarques' || '@' || 'unl.edu.ar' AS email;
---------------------------------------------------------
Martín Marqués | Programador, DBA
Centro de Telemática | Administrador
Universidad Nacional
del Litoral
---------------------------------------------------------

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #5

On Thursday 15 January 2004 01:34 pm, Martin Marques wrote:

A better way to understand it is:

Foreign keys are many to one assignments.


Many to One is what clicks with me. Thanks...
--

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: te***@esc1.com

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #6
Victor Spång Arthursson wrote:

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 |
Given what you have shown, there is no way foreign keys can work here.
Foreign keys are links between the key values of two tables in a sort
of master/slave relationship.
What I want to do is to create a foreign key between the two tables.


What problem does the concept of a foreign key solve for you?
--
jimoe at sohnen-moe dot com

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards ...
4
by: inline_four | last post by:
If I have a table with multiple foreign keys to various other tables, what's the best way to index them: one index that contains all of those columns, or multiple indexes containing one foreign key...
1
by: Michael D | last post by:
What are the pros and cons of the following two design methods ? (1) Using foreign keys to form a composite primary key of a child tables -- as in Example. POOR MAN'S ERD PROVIDED FOR SUMMARY...
1
by: Krist | last post by:
Hi All, There is some additional info I forget on this same topic I just posted. I have a database design question, pls give me some help.. I want to define tables for salesman's sales target...
4
by: Simon Bond | last post by:
Hi there, I should point out I am very new to MySql, im trying to learn what I can. Basically im using Navicat to work with my databases. When making a foreign key, i am asked to enter the...
1
by: James E | last post by:
I have a question about best practices of how to deal with lookup data from my C# apps. On a couple of occasions I have come across a problem where I have to automate inserting a record into a...
2
by: Ed Havelaar | last post by:
Hi, Hope someone can help. Here's the scenario: I have two MSAccess tables MainTab and SubTab. MainTab has an autonumber 'id' column as primary key. Subtab has this id column as a foreign...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
12
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
6
semanticnotion
by: semanticnotion | last post by:
Hi sir i want to transform the data of one table into another through foreign key but the following error come to my browser Here is my code and data base structure. CREATE TABLE IF NOT...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.