Unix Technical Forum

Unique values between two tables.

This is a discussion on Unique values between two tables. within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I have two tables such that in each table I need to make sure that column x in ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 03:53 AM
Ori
 
Posts: n/a
Default Unique values between two tables.

Hi,

I have two tables such that in each table I need to make sure that
column x in table A and column y in table B have a unique values
meaning that a user cannot insert a value to column A if its already
exist in column B and vice versa.

How can I enforce it? Please remember that this two different tables.

Thanks,

Ori.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 03:53 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Unique values between two tables.

Ori (orianavim@hotmail.com) writes:
> I have two tables such that in each table I need to make sure that
> column x in table A and column y in table B have a unique values
> meaning that a user cannot insert a value to column A if its already
> exist in column B and vice versa.
>
> How can I enforce it? Please remember that this two different tables.


Triggers is one one way.

You can also set up an indexed view which is a SELECT with UNION ALL of
the two tables with a unique index on the common key.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 03:53 AM
David Portas
 
Posts: n/a
Default Re: Unique values between two tables.

You can create a third table to enforce the constraint:

CREATE TABLE C (x INTEGER PRIMARY KEY, z CHAR(1) NOT NULL CHECK (z IN
('A','B')), UNIQUE (x,z))

CREATE TABLE A (x INTEGER NOT NULL, z CHAR(1) NOT NULL DEFAULT 'A' CHECK (z
= 'A'), FOREIGN KEY (x,z) REFERENCES C (x,z) /* PRIMARY KEY ??? */ )

CREATE TABLE B (x INTEGER NOT NULL, z CHAR(1) NOT NULL DEFAULT 'B' CHECK (z
= 'B'), FOREIGN KEY (x,z) REFERENCES C (x,z) /* PRIMARY KEY ??? */ )

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:54 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com