473,471 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Unique values between two tables.

Ori
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.
Jul 20 '05 #1
2 3116
Ori (or*******@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, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
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
--
Jul 20 '05 #3

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

Similar topics

5
by: Westcoast Sheri | last post by:
Which will be a faster lookup of an item by, "color" in the following mySQL tables: "unique key," "primary key," or just plain "key"?? CREATE TABLE myTable ( number int(11) NOT NULL default '0',...
6
by: Bob Stearns | last post by:
I was under the impression that the primary key had to be a unique index. Since I usually create my primary indices before my primary keys, in order to get the indices in the same schema as their...
0
by: web1110 | last post by:
Hi y'all, I was playing with the NorthWind database, displaying subtables in a DataGrid. It worked fine for 3 levels, but when I added a fourth, I got the following error: These columns...
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...
2
by: Jimmy Stewart | last post by:
Ok, I'm trying to write a query that is starting to wear me down. What I'm trying to do is create a year end report that gets sent to all of my customers who meet two criteria. One they are...
4
by: jim | last post by:
I've been searching and trying out SQL statements for hours and I'm still having a problem. I'm querying 3 tables... and I only want the first unique record returned. Currently, the data...
1
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our...
1
by: yasmine | last post by:
Hi friends, Can anyone tell me how can i write a php script which extract all the unique values of a field from multiple tables? and also if anybody enters some new values it also should be...
9
by: Dawid Zolkiewicz | last post by:
Hello I'm new here, so at the beginning I'd like to say hello for everybody. First I'll describe my problem. There is db2 database :) with about 0.5 mln people. Every person has three...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.