Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to add a constraint between two domains in a table without using a trigger?

I have a following table, Client.

create table Client (
    ClientID    int identity primary key,
    TaxID       varchar(12),
    SSN         varchar(12)
)
GO

Client can have either TaxID or SSN or both. But either one should exist.
Currently, I am enforcing the rule thru following trigger.

create trigger trgClient_UniqueTaxIDSSN
    on Client
    after Insert, Update
as
    --; Check if either TaxID or SSN is not null.

But is there a way to declare a constraint to enforce the rule?

like image 977
dance2die Avatar asked Feb 28 '23 13:02

dance2die


2 Answers

ALTER TABLE Client ADD CONSTRAINT ck_TaxIDorSSN CHECK 
    (TaxID is not null or SSN is not null)
like image 135
Adam Robinson Avatar answered Mar 02 '23 01:03

Adam Robinson


You should be able to create a Check Constraint, on the Client table, to do this.

http://msdn.microsoft.com/en-us/library/ms188258.aspx
like image 22
Randy Minder Avatar answered Mar 02 '23 01:03

Randy Minder