Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Suggestion on Foreign Key Constraint Expand / Collapse
Author
Message
Posted Tuesday, February 8, 2011 11:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 8, 2012 11:10 AM
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
Post #1060452
Posted Tuesday, February 8, 2011 11:07 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Monday, September 22, 2014 6:13 AM
Points: 20,578, Visits: 9,618
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 ?
Post #1060458
Posted Tuesday, February 8, 2011 11:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 8, 2012 11:10 AM
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
Post #1060467
Posted Tuesday, February 8, 2011 11:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:09 AM
Points: 12,901, Visits: 32,137
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1060474
Posted Tuesday, February 8, 2011 11:22 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Monday, September 22, 2014 6:13 AM
Points: 20,578, Visits: 9,618
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.
Post #1060480
Posted Tuesday, February 8, 2011 11:23 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Monday, September 22, 2014 6:13 AM
Points: 20,578, Visits: 9,618
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.
Post #1060483
Posted Tuesday, February 8, 2011 11:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 8, 2012 11:10 AM
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

Post #1060487
Posted Tuesday, February 8, 2011 11:31 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, September 25, 2014 12:52 PM
Points: 464, Visits: 1,037
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
Post #1060492
Posted Tuesday, February 8, 2011 11:33 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Monday, September 22, 2014 6:13 AM
Points: 20,578, Visits: 9,618
It only has to be a key in the countries table, not the other way around.


Post #1060495
Posted Tuesday, February 8, 2011 11:34 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Monday, September 22, 2014 6:13 AM
Points: 20,578, Visits: 9,618
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.
Post #1060497
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse