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
 General SQL Server Forums
 New to SQL Server Programming
 Key problem

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-06-26 : 02:52:29
While Iam trying to execute the below query

alter table UDS_Expense_account_Group
add constraint fk_exp_grp foreign key(Expense_group)
references UDS_Expense_Group(Expense_group)

Error faced:
There are no primary or candidate keys in the referenced table 'UDS_Expense_Group' that match the referencing column list in the foreign key 'fk_exp_grp'.

keys for the table:
UDS_Expense_Group primary key
pk_exp(sno, Expense_group)

I know that reference table value should be unique.But i need the
sno and expense_group column to be unique

how to solve can anyone help????




visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 02:57:59
but how can it guarantee that Expense_group alone will be unique in UDS_Expense_Group?
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-06-26 : 02:58:12
I was under the impression that FK must point to a PK.

You could try creating a separate unique index on just UDS_Expense_Group(Expense_group) and see if it lets you create the FK then.


elsasoft.org
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 02:59:37
quote:
Originally posted by jezemine

I was under the impression that FK must point to a PK.

You could try creating a separate unique index on just UDS_Expense_Group(Expense_group) and see if it lets you create the FK then.


elsasoft.org


before that OP needs to make sure he have only unique values of Expense_group appearing in the table.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-06-26 : 03:05:24
well, creating that index will certainly tell them the answer to that question. :)


elsasoft.org
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-06-26 : 03:55:25
txs for ur reply visakh16.
yes ur right since it is a composite key(sno,expense_group) it cannot be guarantee that expense_group alone be unique.

but i need sno should be unique and expense_group should also be unique.
so can i create unique key on sno and primary key on expense_group???

quote:
Originally posted by visakh16

but how can it guarantee that Expense_group alone will be unique in UDS_Expense_Group?

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 03:56:55
quote:
Originally posted by sent_sara

txs for ur reply visakh16.
yes ur right since it is a composite key(sno,expense_group) it cannot be guarantee that expense_group alone be unique.

but i need sno should be unique and expense_group should also be unique.
so can i create unique key on sno and primary key on expense_group???

quote:
Originally posted by visakh16

but how can it guarantee that Expense_group alone will be unique in UDS_Expense_Group?




Yup you can do that. create pk on expense_no and unique on sno
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-06-26 : 04:32:06
Txs for everyone
quote:
Originally posted by visakh16

quote:
Originally posted by sent_sara

txs for ur reply visakh16.
yes ur right since it is a composite key(sno,expense_group) it cannot be guarantee that expense_group alone be unique.

but i need sno should be unique and expense_group should also be unique.
so can i create unique key on sno and primary key on expense_group???

quote:
Originally posted by visakh16

but how can it guarantee that Expense_group alone will be unique in UDS_Expense_Group?




Yup you can do that. create pk on expense_no and unique on sno

Go to Top of Page
   

- Advertisement -