Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Composing primary key

Author  Topic 

goranp
Starting Member

17 Posts

Posted - 2008-02-29 : 20:33:23
Hi! Probably answer on this question exists already on this forum but i can't find it.

I have table with two foreign keys (ID1 and ID2) and date. Date can't be primary key because it wouldn't be unique so i must create PK from those three things.

PK = ID1 + ID2 + datetime

and simply can't get it, always some error.

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-02-29 : 20:38:22
I would also presume ID1+ID2 together aren't unique enough

What is the error you get?




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

goranp
Starting Member

17 Posts

Posted - 2008-02-29 : 20:44:39
NO, ID's aren't unique, although they can show up only once on the specific date.

"" There are no primary or candidate keys in the referenced table 'clubs' that match the referencing column list in the foreign key 'FK__tekme_nog__ID_do__0BC6C43E'. ""

but there are, table 'clubs' is created and ID is primary key there. I must seek table clubs for ID1 and ID2 put them in new table, connect them with date and create PK.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-02-29 : 20:51:59
You are going to have to post the DDL for the tables or some more information.

the referenced table clubs must have a primary key in order for that primary key to be a foreign key in another table. It sounds like you clubs.[ID] column is not referenced by the specificed FK definition ..



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

goranp
Starting Member

17 Posts

Posted - 2008-02-29 : 20:55:07

CREATE TABLE klubovi(
ID_kluba int identity(1,1),
ID_lige int foreign key references liga(ID_lige),
naziv_kluba varchar(100)
)

CREATE TABLE tekme_nogomet(
ID_domacina int foreign key references klubovi(ID_kluba),
ID_gosta int foreign key references klubovi(ID_kluba) ,
datum datetime,
gol_domaci int,
gol_gosti int,
udarci_domaci int,
udarci_gosti int,
na_gol_domaci int,
na_gol_gosti int,
zuti_domaci int,
zuti_gosti int,
crveni_domaci int,
crveni_gosti int,
posjed_domaci int,
posjed_gosti int,
opis varchar(1000)

primary key(ID_domacina,ID_gosta,datum) // this should be PK
)

Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-02-29 : 20:56:39
so ID_Domacina AND ID_gosta BOTH foreign key reference to klubovi(ID_kluba)?





Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

goranp
Starting Member

17 Posts

Posted - 2008-02-29 : 21:02:36
yes, klubovi are football teams and i am trying to create a game. (if you know what i mean) so i'm taking them from the same table.
Go to Top of Page

goranp
Starting Member

17 Posts

Posted - 2008-03-01 : 07:41:35
hm, i am looking in this for a while now and have no idea. Creating constraint that will not allow user to enter 1 klub (team) twice on the same date must exists definitely. And foreign key can be part of primary key, right? This should be so simple.... I'll try change date with gameday (int)....because i'm becoming desperate
Go to Top of Page

goranp
Starting Member

17 Posts

Posted - 2008-03-01 : 07:44:50
don't do SQL late at night. i forgot to define PK in table klubovi. Idiot.
tnx
Go to Top of Page
   

- Advertisement -