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)
 Regarding unique constraint -- need help

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-05 : 15:41:47
I've master tables and row as follow,
CREATE TABLE [dbo].[tCompy](
[idx] [smallint] IDENTITY(1,1) NOT NULL,
[cd] [varchar](50) NOT NULL,
[desn] [varchar](50) NOT NULL,
[stat] [bit] NOT NULL,
[crtby] [varchar](20) NOT NULL,
[crtdte] [smalldatetime] NOT NULL CONSTRAINT [DF_tCompany_crtdte] DEFAULT (getdate()),
[updby] [varchar](20) NOT NULL CONSTRAINT [DF_tCompany_updby] DEFAULT ('na'),
[upddte] [smalldatetime] NOT NULL CONSTRAINT [DF_tCompany_upddte] DEFAULT (getdate()),
[editno] [smallint] NOT NULL CONSTRAINT [DF_tCompany_editno] DEFAULT ((1)),
CONSTRAINT [PK_tCompany] 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 [tcompany01] 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];
insert into tCompy(cd,desn,stat,crtby)
values('snt','sn tech',1,'admin');

CREATE TABLE [dbo].[tCout](
[idx] [smallint] IDENTITY(1,1) NOT NULL,
[cd] [varchar](20) NOT NULL,
[desn] [varchar](50) NOT NULL,
[inettrnx] [bit] NOT NULL,
[stat] [bit] NOT NULL,
[remk] [varchar](100) NOT NULL,
[crtby] [varchar](20) NOT NULL,
[crtdte] [smalldatetime] NOT NULL CONSTRAINT [DF_TCounter_crtdte] DEFAULT (getdate()),
[updby] [varchar](20) NOT NULL CONSTRAINT [DF_tCounter_updby] DEFAULT ('na'),
[upddte] [smalldatetime] NOT NULL CONSTRAINT [DF_tCounter_upddte] DEFAULT (getdate()),
[editno] [smallint] NOT NULL CONSTRAINT [DF_tCounter_editno] DEFAULT ((1)),
CONSTRAINT [PK_tCounter] PRIMARY KEY CLUSTERED
(
[idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY],
CONSTRAINT [tcounter01] 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];
insert into tCout
(cd, desn, inettrnx, stat, remk, crtby)
values
('kl','kuala lumpur',1,1,'','admin');
insert into tCout
(cd, desn, inettrnx, stat, remk, crtby)
values
('iph','ipoh',1,1,'','admin');


Let's say, i designed as follow,
CREATE TABLE [dbo].[t1](
[idx] [smallint] IDENTITY(1,1) NOT NULL,
[tcompycd] [varchar](50) NOT NULL,
CONSTRAINT [PK_t1] 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]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[t1] WITH CHECK ADD CONSTRAINT [FK_t1_tcompycd] FOREIGN KEY([tcompycd])
REFERENCES [dbo].[tCompy] ([cd])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[t1] CHECK CONSTRAINT [FK_t1_tcompycd];

CREATE TABLE [dbo].[t2](
[idx] [smallint] IDENTITY(1,1) NOT NULL,
[t1idx] [smallint] NOT NULL,
[tcoutcd] [varchar](20) NOT NULL,
CONSTRAINT [PK_t2] 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]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[t2] WITH CHECK ADD CONSTRAINT [FK_t2_t1idx] FOREIGN KEY([t1idx])
REFERENCES [dbo].[t1] ([idx])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[t2] CHECK CONSTRAINT [FK_t2_t1idx]
GO
ALTER TABLE [dbo].[t2] WITH CHECK ADD CONSTRAINT [FK_t2_tcoutcd] FOREIGN KEY([tcoutcd])
REFERENCES [dbo].[tCout] ([cd])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[t2] CHECK CONSTRAINT [FK_t2_tcoutcd];

CREATE TABLE [dbo].[t3](
[idx] [smallint] IDENTITY(1,1) NOT NULL,
[t1idx] [smallint] NOT NULL,
[tcoutcd] [varchar](20) NOT NULL,
CONSTRAINT [PK_t3] 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]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[t3] WITH CHECK ADD CONSTRAINT [FK_t3_t1idx] FOREIGN KEY([t1idx])
REFERENCES [dbo].[t1] ([idx])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[t3] CHECK CONSTRAINT [FK_t3_t1idx]
GO
ALTER TABLE [dbo].[t3] WITH CHECK ADD CONSTRAINT [FK_t3_tcoutcd] FOREIGN KEY([tcoutcd])
REFERENCES [dbo].[tCout] ([cd])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[t3] CHECK CONSTRAINT [FK_t3_tcoutcd]


Me execute insert statement into t1 and t2 as follow,
declare @idx smallint
insert into t1 values('snt');
set @idx=scope_identity();
insert into t2(t1idx,tcoutcd) values(@idx,'iph');


I knew, how to create unique contraint into 1 table
My question is,
1. It's possible to create unique constraint into 2 table?
2. If possible, how to make it combination of table t1(tcompycd) and table t2(tcoutcd) is a unique?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-05 : 17:35:01
A constraint can not span more than one table. Hope I understood your question correctly.

You can instead write a trigger.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-05 : 17:50:05
quote:
Originally posted by tkizer

A constraint can not span more than one table. Hope I understood your question correctly.

You can instead write a trigger.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."



Hi Sir,
Felt better now. I understood constraint can not span more than one table.

So far, i've as follow
t1
idx | tcompycd
----------------------------
1 snt

t2
idx | t1idx | tcoutcd
----------------------------
1 1 iph

My objective now, i want to prevent the same record
declare @idx smallint
insert into t1 values('snt');
set @idx=scope_identity();
insert into t2(t1idx,tcoutcd) values(@idx,'iph');


inserted into t1 and t2.

Can you me, how the trigger look's like?

Really need help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-06 : 02:01:14
do you mean something like

CREATE TRIGGER Yourtrigger
ON t2
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2
SELECT MAX(i.idx),i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-06 : 12:45:51
quote:
Originally posted by visakh16

do you mean something like

CREATE TRIGGER Yourtrigger
ON t2
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2
SELECT MAX(i.idx),i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Me using your code as follow,
CREATE TRIGGER t201 
ON t2
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2
SELECT MAX(i.idx),i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


But it return error as follow,
An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-06 : 13:02:14
It means the value in column id in table t2 is set as an autoincrement i.e an identity property has been set for the column.

PBUH
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-06 : 13:04:13
quote:
Originally posted by Idera

It means the value in column id in table t2 is set as an autoincrement i.e an identity property has been set for the column.

PBUH



can you show to me, where to change in my code?
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-06 : 13:08:36
Just run this
SET IDENTITY_INSERT t2 ON

PBUH
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-06 : 13:17:25
quote:
Originally posted by Idera

Just run this
SET IDENTITY_INSERT t2 ON

PBUH



My alter trigger as follow,
ALTER TRIGGER [dbo].[t201] 
ON [dbo].[t2]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
SET IDENTITY_INSERT t2 ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2
SELECT MAX(i.idx),i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


My error as follow,
An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-06 : 13:20:06
Did u try my solution.Please run this

SET IDENTITY_INSERT t2 ON

I cannot be more specific than this.

PBUH
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-06 : 13:25:12
quote:
Originally posted by Idera

Did u try my solution.Please run this

SET IDENTITY_INSERT t2 ON

I cannot be more specific than this.

PBUH



1st, below was executed
SET IDENTITY_INSERT t2 ON


The result was,
Command(s) completed successfully.

2nd, below was executed as follow,
ALTER TRIGGER [dbo].[t201] 
ON [dbo].[t2]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2
SELECT MAX(i.idx),i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


The result was,
An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-03-06 : 14:07:40
The identity insert goes inside the trigger. Not run separately.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-06 : 14:21:06
quote:
Originally posted by GilaMonster

The identity insert goes inside the trigger. Not run separately.

--
Gail Shaw
SQL Server MVP



Me paste the identity inside the trigger as follow,
ALTER TRIGGER [dbo].[t201] 
ON [dbo].[t2]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
SET IDENTITY_INSERT t2 ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2
SELECT MAX(i.idx),i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


It return an error as follow,
An explicit value for the identity column in table 't2' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-07 : 02:27:14
make last insert like below and try

INSERT INTO t2 (t1idx ,tcoutcd)
SELECT i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-07 : 02:43:47
quote:
Originally posted by visakh16

make last insert like below and try

INSERT INTO t2 (t1idx ,tcoutcd)
SELECT i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





With your help, i'm successful to create trigger as follow,
CREATE TRIGGER [dbo].[t201]
ON [dbo].[t2]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON

IF EXISTS(SELECT t1.tcompycd, i.tcoutcd
FROM t1 JOIN INSERTED i ON i.t1idx =t1.idx
GROUP BY t1.tcompycd, i.tcoutcd HAVING COUNT(*)=1)
BEGIN
INSERT INTO t2 (t1idx ,tcoutcd)
SELECT i.t1idx,i.tcoutcd
FROM t1
JOIN INSERTED i
ON i.t1idx =t1.idx
GROUP BY i.t1idx,t1.tcompycd, i.tcoutcd
HAVING COUNT(*)=1
END
END


Currently, my t1 and t2 as follow,
t1
idx | tcompycd
----------------------
1 snt

t2
idx | t1idx | tcoutcd
-----------------------------
1 1 iph


But the problem is, me still can't achieve my objective
i want to prevent the same record

declare @idx smallint
insert into t1 values('snt');
set @idx=scope_identity();
insert into t2(t1idx,tcoutcd) values(@idx,'iph');


inserted into t1 and t2.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-07 : 08:26:56
what do you mean by same record?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-03-07 : 10:06:22
i think, i need to normalized my table design first sir.

tq for helping me
Go to Top of Page
   

- Advertisement -