Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Suggestion on Foreign Key Constraint


Suggestion on Foreign Key Constraint

Author
Message
zubairmasoodi
zubairmasoodi
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 20
Hi

I have a very simple Question and i know experts here will give me an instant reply.

I have a table UserInfo withe the following columns

UserID P.K
UserName
Password
CountryID

And an another Table Country with the following columns

CountryID P.K
CountryName

Will it be advisable to add a foreign key constraint to UserInfo table On (CountryID) column which points to CountryID of Country table

If Yes , why?
If Not , Why not? and what is the alternate way, or there should be no relation between the two.

All replies are deeply appreciated

Thanks

New SQL Server Programmer
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
I've seen it work both ways.

Both have advantages that are no negligible. 1 garantees the data is good. 2 Improves db speed.

What would you do ?
zubairmasoodi
zubairmasoodi
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 20
Thanks for your quick reply

I quote you

"2 Improves db speed" and this is my primary motivation., but i wonder architecturally if its a good practice.

Limitation here i see is , will this constraint allow me to delete a record in UserInfo table ? or will it just say

Cannot truncate table 'UserInfo' because it is being referenced by a FOREIGN KEY constraint.


Thanks

Zubair
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38949
zubairmasoodi (2/8/2011)
Thanks for your quick reply

Cannot truncate table 'UserInfo' because it is being referenced by a FOREIGN KEY constraint.


No, the constraint will not let you delete the Country because it is referenced by the foreign key...it will not prevent the referencing record(the child data...userInfo) fromb eing deleted.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
zubairmasoodi (2/8/2011)
Thanks for your quick reply

I quote you

"2 Improves db speed" and this is my primary motivation., but i wonder architecturally if its a good practice.

Limitation here i see is , will this constraint allow me to delete a record in UserInfo table ? or will it just say

Cannot truncate table 'UserInfo' because it is being referenced by a FOREIGN KEY constraint.


Thanks

Zubair



Here's 1 of my favorite mantra. There's no point in having the wrong answer fast.

I'd strongly recommend to use then in dev and once you're 1 000 0000 0000 000 000 000 000 000 000 % sure that you application is coded correctly, then you can remove the fks.
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
And just to point out something else. You will be able to delete, but not TRUNCATE. The fk removes the ability to truncate, but that's all.
zubairmasoodi
zubairmasoodi
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 20
Thanks,

but Logically speaking , Dont you think its wrong that it will not allow me to delete a record in UserInfo Table


Country Table is Master Table, I am not going to delete record in my Master Table

I am just deleting a record in UserInfo table. which should be no problem


I Can delete any user from my DB and it should not affect my Country Table (which is master)

Thanks
@SQLFRNDZ
@SQLFRNDZ
Mr or Mrs. 500
Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)Mr or Mrs. 500 (523 reputation)

Group: General Forum Members
Points: 523 Visits: 1175
In first, I don't think you can reference country id in userinfo table as foriegn key as it is not a composite key or unique key or not not part of a primary in the user info table .


correct me if Im wrong.


--SQLFRNDZ

Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
It only has to be a key in the countries table, not the other way around.
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
zubairmasoodi (2/8/2011)
Thanks,

but Logically speaking , Dont you think its wrong that it will not allow me to delete a record in UserInfo Table


Country Table is Master Table, I am not going to delete record in my Master Table

I am just deleting a record in UserInfo table. which should be no problem


I Can delete any user from my DB and it should not affect my Country Table (which is master)

Thanks




No that you'll be allowed to do.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search