472,952 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 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 2168
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.