|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, August 08, 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
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 21,357,
Visits: 9,537
|
|
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 ?
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, August 08, 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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 11,638,
Visits: 27,712
|
|
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
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 21,357,
Visits: 9,537
|
|
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.
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 21,357,
Visits: 9,537
|
|
| 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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, August 08, 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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Yesterday @ 12:03 PM
Points: 412,
Visits: 862
|
|
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
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 21,357,
Visits: 9,537
|
|
It only has to be a key in the countries table, not the other way around.
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 21,357,
Visits: 9,537
|
|
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.
|
|
|
|