If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Duplicate Checking Across Tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-19-09, 01:39
joatmon joatmon is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Duplicate Checking Across Tables

T1 <-- T2 <-- T3
Is it possible to define a unique constraint involving one column from T3 and one from T1?

e.g. Customer has multiple Application(s). Application has multiple Ticket(s). The TicketNumber should be unique for every Customer.

Thanks & Regards.
Reply With Quote
  #2 (permalink)  
Old 06-19-09, 07:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what are the primary keys for those tables?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-22-09, 04:09
joatmon joatmon is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
In the example,
Customer - CustomerId (Natural Key)
Application - Surrogate Key
Ticket - Surrogate Key
Reply With Quote
  #4 (permalink)  
Old 06-22-09, 07:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by joatmon
Ticket - Surrogate Key
presumably you're using an autoincrement or summat similar?

then it will be unique for every customer because it's unique, full stop

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-22-09, 22:24
joatmon joatmon is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Sorry, I will attempt to make it a little more clear.

TicketId is unique because it is autoincrement. However there is an additional attribute TicketNumber which is what I need to make unique within a customer. That is, I need to allow same ticket number across customers (C1-T1 and C2-T1), but need to ensure uniqueness within a customer (C1-T1 is unique). In summary, the CustomerId+TicketNumber combination should be unique. (Currently this is enforced by the application code and I am wondering whether any of the RDBMS support defining this as a DB constraint).
Reply With Quote
  #6 (permalink)  
Old 06-22-09, 23:03
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Only a few DBMS engines allow multi-column constraints. All of the DBMS engines that I can think of allow UNIQUE INDEX creation, which would do what you want.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #7 (permalink)  
Old 06-22-09, 23:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Pat Phelan
...which would do what you want.
only if CustomerID is redundantly carried in the Ticket table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 06-23-09, 10:53
sqlguru sqlguru is offline
Registered User
 
Join Date: Jun 2009
Posts: 66
Quote:
Originally Posted by r937
only if CustomerID is redundantly carried in the Ticket table
Although there is an applications table, they are probably referring to a join table (customers can have multiple applications) in which the customer id should be carried over to the ticket table unless he has a design problem where applications is not normalized.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On