473,379 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

unique constraint to a column from another table

Is it possible to create a unique constraint to a column from another
table? For example:

tb_current:
current_names
--------------
aaa
bbb

tb_new:
new_name
--------
ccc

Now I want to create a constraint on tb_new.new_name to be unique with
respect to tb_current.current_names. However, tb_new.new_name should
not be unique to itself. So I should not be able to insert 'aaa' to
tb_new.new_name. But I should be able to insert 'ccc' to
tb_new.new_name.

Here's the script to reproduce this example:

create table tb_current
(
current_names varchar(10)
)
create table tb_new
(
new_name varchar(10)
)
insert tb_current values ('aaa')
insert tb_current values ('bbb')
insert tb_new values ('ccc')

select * from tb_current
select * from tb_new

insert tb_new values ('aaa') -- this should NOT be allowed
insert tb_new values ('ccc') -- this should be allowed

Oct 29 '05 #1
3 2189
It's hard to understand your spec. Is it that what you want is that any
new_name that you insert into tb_new does not exist in tb_current? If so, a
trigger will do it:

create trigger triu_tb_new on tb_new after insert, update
as
if @@ROWCOUNT = 0
return

if exists (select * from inserted i
join tb_current c on c.current_names = i.new_name)
begin
raiserror ('Names exist in tb_current.', 16, 1)
rollback tran
end
go

You can put a similar trigger on tb_current:

create trigger triu_tb_new on tb_new after insert, update
as
if @@ROWCOUNT = 0
return

if exists (select * from inserted i
join tb_new_name n on i.current_names = n.new_name)
begin
raiserror ('Names exist in tb_new_name.', 16, 1)
rollback tran
end
go

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
<uw********@email.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Is it possible to create a unique constraint to a column from another
table? For example:

tb_current:
current_names
--------------
aaa
bbb

tb_new:
new_name
--------
ccc

Now I want to create a constraint on tb_new.new_name to be unique with
respect to tb_current.current_names. However, tb_new.new_name should
not be unique to itself. So I should not be able to insert 'aaa' to
tb_new.new_name. But I should be able to insert 'ccc' to
tb_new.new_name.

Here's the script to reproduce this example:

create table tb_current
(
current_names varchar(10)
)
create table tb_new
(
new_name varchar(10)
)
insert tb_current values ('aaa')
insert tb_current values ('bbb')
insert tb_new values ('ccc')

select * from tb_current
select * from tb_new

insert tb_new values ('aaa') -- this should NOT be allowed
insert tb_new values ('ccc') -- this should be allowed

Oct 29 '05 #2
<uw********@email.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Is it possible to create a unique constraint to a column from another
table? For example:

tb_current:
current_names
--------------
aaa
bbb

tb_new:
new_name
--------
ccc

Now I want to create a constraint on tb_new.new_name to be unique with
respect to tb_current.current_names. However, tb_new.new_name should
not be unique to itself. So I should not be able to insert 'aaa' to
tb_new.new_name. But I should be able to insert 'ccc' to
tb_new.new_name.

Here's the script to reproduce this example:

create table tb_current
(
current_names varchar(10)
)
create table tb_new
(
new_name varchar(10)
)
insert tb_current values ('aaa')
insert tb_current values ('bbb')
insert tb_new values ('ccc')

select * from tb_current
select * from tb_new

insert tb_new values ('aaa') -- this should NOT be allowed
insert tb_new values ('ccc') -- this should be allowed


I suspect it would be a mistake to implement it that way, although in
prinicple you could do so through triggers.

Why not just have one table for the common "Name" attribute and a unique key
for that column. Add an extra column to distinguish between "current" and
"new" names if that distinction is important.

--
David Portas
SQL Server MVP
--
Oct 29 '05 #3
Tom Moreau wrote:
It's hard to understand your spec. Is it that what you want is that any
new_name that you insert into tb_new does not exist in tb_current? If so, a
trigger will do it:


Yes, that is what I want. Thanks for the suggestion, I think that will
work.

Oct 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
3
by: Prince Kumar | last post by:
Is there any way I can define an Unique constraint or unique index which allows more than one null values for the same column combination in DB2? ie, If my index is defined on (col3, col4) where...
5
by: aj | last post by:
DB2 WSE 8.1 FP5 Red Hat AS 2.1 What is the difference between adding a unique constraint like: ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE ( <COL1>) ; and adding a...
7
by: Brian Keating | last post by:
Hi there, Is it possible to add a unique constraint on two columns in a table, so that the constraint is a composite of the two? i.e. these two columns together should be unique...? i.e....
2
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the...
2
by: Benjamin Smith | last post by:
I have two tables like following: create table attendancereport ( id serial unique not null, staff_id integer not null references staff(id), schoolyear varchar not null references...
3
by: Bruce Wood | last post by:
I know that this isn't a C# question, but I can't find a newsgroup specifically devoted to ADO.NET, other than a moribund one that deals with ADO in general. This problem is driving me to...
10
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
6
by: Alvin SIU | last post by:
Hi all, I have a table in Db2 v8 like this: Team Name Role ------ -------- --------------------- A Superman Leader A Batman Member A WonderWoman Member B ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.