You are here: unique constraint to a column from another table « MsSQL Server « IT news, forums, messages
unique constraint to a column from another table

Posted by uw_badgers on 10/14/24 11:30

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация