Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

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


Report this thread to moderator Post Follow-up to this message
Old Post
uw_badgers@email.com
10-29-05 01:23 AM


Re: unique constraint to a column from another table
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_badgers@email.com> wrote in message
news:1130545124.342622.45140@g47g2000cwa.googlegroups.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


Report this thread to moderator Post Follow-up to this message
Old Post
Tom Moreau
10-29-05 01:23 AM


Re: unique constraint to a column from another table
<uw_badgers@email.com> wrote in message
news:1130545124.342622.45140@g47g2000cwa.googlegroups.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
--



Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
10-29-05 12:23 PM


Re: unique constraint to a column from another table
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.


Report this thread to moderator Post Follow-up to this message
Old Post
uw_badgers@email.com
10-30-05 08:25 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 03:44 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006