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
 what will happen if i dont use a foregin key?

Author  Topic 

soorejmg
Starting Member

1 Post

Posted - 2010-07-01 : 08:40:17
I know about foreign key from w3schools tutorial. I know that," The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables."
How does it prevent but?
What if i didnot use a foreign key? I can just update the related tables right whenever situation demands and deleete the rows whenever situation demands?

Or can i say it as a contraint just to avoid codinng errors by SQL programmers?

Please help
Thanks
Soorej

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-01 : 08:49:16
quote:
How does it prevent but?

SQL Server throws an error if someone is trying to delete a record that has dependent record(s).
(But: it is possible to make cascading deletes)

quote:
What if i didnot use a foreign key?

Then your programmers / the application should work without any flaw because SQL Server isn't watching for referential integrity for you!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2010-07-01 : 08:57:11
quote:
Originally posted by soorejmg

I know about foreign key from w3schools tutorial. I know that," The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables."
How does it prevent but? By checking the underlying table to ensure the integrity you've set up applies
What if i didnot use a foreign key? I can just update the related tables right whenever situation demands and deleete the rows whenever situation demands? Yes

Or can i say it as a contraint just to avoid codinng errors by SQL programmers? It won't avoid coding errors but it will catch them before you move into production, provided you test. If you have a fair amount of turnover by your developers, this is a great way for them to get to know the relationships of your tables to one another. Referential integrity is a beautiful thing.

Please help
Thanks
Soorej



Terry

-- A word to the wise ain't necessary - it's the stupid ones that need the advice. -- Bill Cosby
Go to Top of Page
   

- Advertisement -