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 2008 Forums
 Transact-SQL (2008)
 Need help to built Check Constraints

Author  Topic 

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-06-12 : 21:21:15
I've tables and data as following,
CREATE TABLE [dbo].[paymentType](
[idx] [int] IDENTITY(1,1) NOT NULL,
[cd] [varchar](10) NOT NULL,
[descp] [varchar](100) NOT NULL,
CONSTRAINT [PK_paymentType] PRIMARY KEY CLUSTERED
(
[idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[agroType](
[idx] [int] IDENTITY(1,1) NOT NULL,
[cd] [varchar](10) NOT NULL,
[descp] [varchar](100) NOT NULL,
CONSTRAINT [PK_agroType] PRIMARY KEY CLUSTERED
(
[idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [agroTypeUQ1] UNIQUE NONCLUSTERED
(
[cd] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[kwspType](
[idx] [int] IDENTITY(1,1) NOT NULL,
[cd] [varchar](10) NOT NULL,
[descp] [varchar](100) NOT NULL,
CONSTRAINT [PK_kwspType] PRIMARY KEY CLUSTERED
(
[idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [kwspTypeUQ1] UNIQUE NONCLUSTERED
(
[cd] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[PaymentItem](
[idx] [int] IDENTITY(1,1) NOT NULL,
[paymentType] [int] NOT NULL,
[amount] [decimal](10, 2) NOT NULL,
[info1] [varchar](10) NULL,
[info2] [varchar](50) NULL,
CONSTRAINT [PK_PaymentItem] PRIMARY KEY CLUSTERED
(
[idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


insert into paymentType values('agroType','Agriculture');
insert into paymentType values('kwspType','KWSP');

insert into agroType values('lmb','Lembu');
insert into agroType values('kbg','Kambing');

insert into kwspType values('ltat','Lembaga Angkatan Tentera');
insert into kwspType values('kbi','Koperasi Belia');


I'm looking for help to create Check Constraints on PaymentItem. The condition as following:
1. If paymentType='agroType', then info1 only can accept cd from agroType table ~ agroType(cd)
2. If paymentType='kwspType', then info1 only can accept cd from kwspType table ~ kwspType(cd)

I hope it's possible.

If not possible, please consult

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-06-13 : 10:35:02
You cannot reference another table in a Check Constraint. A Trigger could be developed that would support this logic though.

=======================================
Elitism is the slur directed at merit by mediocrity. -Sydney J. Harris, journalist (1917-1986)
Go to Top of Page

Idyana
Yak Posting Veteran

96 Posts

Posted - 2011-06-13 : 11:10:48
tq sir. i will re-design the PaymentItem table
Go to Top of Page
   

- Advertisement -