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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Enforcing this constraint

Author  Topic 

hrishy
Starting Member

47 Posts

Posted - 2008-07-22 : 05:00:05
I need to enforce a constarint something like this

NameMaster
Name Code Phone1 Phone2
H1 1 NULL NULL
H1 2 300 400
H2 1 100 100
H3 2 200 800
H4 1 NULL NULL

NameCountryRef
Name Code Country
H1 1 Ghana
H1 2 USA
H2 2 USA
H3 1 GHANA
H4 1 USA

CountryRef
Code Country PhoneMandatory
1 Ghana N
2 USA Y
3 UK Y

I need to enforce the constraint wheneever a new record is added to
the name master table and the country code feild is in USA
or UK then since the phone mandatory feild is Y we cannot have phone numbers as null

I know this can be done in the application but should'nt the database also enforce this constraint ?

But i am currently clueless how to enforce it at the database level.


regards
Hrishy

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-22 : 05:07:26
You can write a trigger to check of a namemaster record belongs to an USA telephone number and rollback the transaction (generate an error) if the phonenumber is empty or is null.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-22 : 05:08:44
if you want to do this at db level you need to write a trigger on NameMaster to check this and raise error if not followed.
Go to Top of Page
   

- Advertisement -