Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2004, 06:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default Referential Integrity

I have a database with 3 base tables:

LicenceNumbers
Customers and
Machines

There are MANY LicenceNumbers.
There are MANY Customers.
Each Customer can have MANY Machines.
Each Customer can have MANY LicenceNumbers.
Each Machine can have MANY LicenceNumbers.
ONE LicenceNumber can only be linked to ONE Customer OR ONE Machine.

In the real world that the database is trying to model, if a customer and not a machine is given a licence number then all the machines that are linked to that customer inherit the licence number.

How can I enforce this referential integrity in my database?

Regards
Owain Williams
__________________
Regards
Owain Williams
 
Old May 12th, 2004, 12:22 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

The table structure I would create, you will need one more table:
(... = what ever other fields you may need)
(PK = primary Key, FK = foreign key)

licenceNumbersTable
ID (PK, int, autoIncrement)
...
...

CustomersTable
ID (PK, int, autoIncrement)
...
...

MachinesTable
ID (PK, int, autoIncrement)
...
...

CustomerMachinesTable
ID (PK, int, autoIncrement)
MID (FK, int,(machineID))
CID (FK, int,(customerID))
LNID (FK, int,(licenceNumberID))
...
...

This would create a structure for you to put relationships in place. Enterprize manager has a useful graphical tool for this. Create this structure, click on diagrams, add all your tables to the diagram, click hold n drag the primary keys to the relavent forign keys.

Wind is your friend
Matt
 
Old May 12th, 2004, 03:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Matt, that looks like the sort of thing I am after, I didn't think of adding the licence number primary key to the customer - machine linking table.

Regards
Owain Williams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Referntial Integrity ajabbabahalmigiriha Reporting Services 0 October 26th, 2007 11:02 AM
UserID Membership & Referential Integrity bkosscus BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 August 28th, 2006 02:35 AM
foreign key constraints - referential integrity DTSDeveloper SQL Server DTS 0 September 6th, 2005 01:10 PM
xml referential integrity mescalito XML 0 December 10th, 2004 08:33 AM
referential integ. not being enforced? Raif Access 0 June 14th, 2004 12:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.