Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Desiging Relations

Author  Topic 

iwaters
Starting Member

13 Posts

Posted - 2005-09-29 : 11:18:18

I have a database design problem for a case management system. The main table "Case" can have one or more Exhibits, one or more Systems, and one or more Media. No problem so far. A System can also have one or more Media. Either a System or a Media can become an Exhibit at a later date. Therefore either a System or a Media can be related to one Exhibit. Which is a one to one relationship is it not? However the data in these three tables is different so I can't really combine them and have a field which is checked if it becomes an exhibit. For those still with with me, how to I design the tables and relationships so that a Media or System can become an Exhibit without duplicating the data between the tables. Also how do I design the fact that a the Media can be related directly to the Case (One-To-Many) and to a System (One-To-Many)?

Any ideas? Driving me mad!



rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-09-29 : 13:27:25
Do You mean that a System / Media becomes an Exhibit (i.e morphs into another entity) ?
Or is it a role that it can come to play at a certain point in it's life ?

Does an Exhibit need to be related to either a System / Media or can it exist on it's own ?

Does a Media have to belong to a System, or can it be related to Case directly, without the intermediary System ?
If a Media is related to both a System and a Case, can the Cases be different (via System, via direct relation to Case) ?

>> how to I design the tables and relationships so that a Media or System can become an Exhibit without duplicating the data between the tables
What data is in common ?

___________________________________________________________________
An expert is somebody who learns more and more about less and less,
until he knows absolutely everything about nothing.
Go to Top of Page

iwaters
Starting Member

13 Posts

Posted - 2005-09-29 : 13:40:23
Yes basically there can be an Exhibit which can be neither a Media or a System. There can be a Media related to a Case without a system, or it can be related to a system which in turn is related to a case. An Exhibit, System or Media will always relate to a Case, except the Media which can relate to a System or a Case - it will never relate to both, it will either relate to the Case or the System.

Sometime during the life cycle a Media or System play the role of an exhibt. In such a case the item in question is now both a Media or System item AND an exhibit. The data in common is fields such as description, type etc. The exhibit has it own data which is only relevant if it is an exhibit.
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-09-29 : 13:48:23
On top of that: Are you interested in history? I mean do you need to know if and when System or Media became an Exhibit?
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-09-29 : 16:46:34
Hi,

You could model a supertype on top of SYSTEM & MEDIA,
and relate that supertype to the EXHIBITS & CASES.

Example follows where I use a supertype ITEM.
For Data Integrity this example would need further constraints so that the keys in SYSTEM_TYPE & MEDIA_TYPE nevere overlap.

SET NOCOUNT ON

CREATE TABLE [CASE]
(
CASE_NR INT IDENTITY(1,1) PRIMARY KEY
-- case specifics
)

CREATE TABLE EXHIBIT
(
CASE_NR INT NOT NULL REFERENCES [CASE](CASE_NR)
,EXHIBIT_NR INT IDENTITY(1,1) PRIMARY KEY
-- exhibit specifics
)

CREATE TABLE ITEM
(
CASE_NR INT NOT NULL REFERENCES [CASE](CASE_NR)
,ITEM_NR INT IDENTITY(1,1) PRIMARY KEY
,EXHIBIT_NR INT NULL REFERENCES EXHIBIT(EXHIBIT_NR)
-- common to system & media
)

CREATE TABLE SYSTEM_ITEM
(
ITEM_NR INT NOT NULL PRIMARY KEY REFERENCES ITEM(ITEM_NR)
-- system specifics
)

CREATE TABLE MEDIA_ITEM
(
ITEM_NR INT NOT NULL REFERENCES ITEM(ITEM_NR) PRIMARY KEY
,ITEM_NR_SYSTEM INT NULL REFERENCES SYSTEM_ITEM(ITEM_NR)
-- media specifics
)

-- Add some sample data
INSERT [CASE] DEFAULT VALUES

-- Add two MEDIA
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]'); INSERT MEDIA_ITEM(ITEM_NR) VALUES(SCOPE_IDENTITY())
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]'); INSERT MEDIA_ITEM(ITEM_NR) VALUES(SCOPE_IDENTITY())

-- Add two SYSTEM
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]'); INSERT SYSTEM_ITEM(ITEM_NR) VALUES(SCOPE_IDENTITY())
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]'); INSERT SYSTEM_ITEM(ITEM_NR) VALUES(SCOPE_IDENTITY())

-- Add two MEDIA related to a SYSTEM
DECLARE @item_nr INT
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]')
SET @item_nr = SCOPE_IDENTITY()
INSERT SYSTEM_ITEM(ITEM_NR) VALUES(@item_nr)
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]'); INSERT MEDIA_ITEM(ITEM_NR,ITEM_NR_SYSTEM) VALUES(SCOPE_IDENTITY(),@item_nr)
INSERT ITEM(CASE_NR) SELECT IDENT_CURRENT('[CASE]'); INSERT MEDIA_ITEM(ITEM_NR,ITEM_NR_SYSTEM) VALUES(SCOPE_IDENTITY(),@item_nr)


PRINT 'All SYSTEMS...'
SELECT i.* FROM ITEM i JOIN SYSTEM_ITEM si ON i.ITEM_NR = si.ITEM_NR
PRINT 'All MEDIA...'
SELECT i.*,mi.ITEM_NR_SYSTEM FROM ITEM i JOIN MEDIA_ITEM mi ON i.ITEM_NR = mi.ITEM_NR


DROP TABLE MEDIA_ITEM,SYSTEM_ITEM,ITEM,EXHIBIT,[CASE]


Edit.
you could get rid of the nulls by adding 2 bridge tables...
Go to Top of Page

iwaters
Starting Member

13 Posts

Posted - 2005-09-30 : 09:43:19
rockmoose thanks for that, it makes sense and works well for me.

You mention putting 2 bridge tables into to remove the NULLS. Would these go between the ITEM Supertype and SYSTEM_ITEM/MEDIA_ITEM subtypes? Would the bridge table contain a composite key of EXHIBIT_NR and ITEM_NR, or have I got that completly wrong?

Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-10-01 : 16:54:51
quote:
Originally posted by iwaters

rockmoose thanks for that, it makes sense and works well for me.

You mention putting 2 bridge tables into to remove the NULLS. Would these go between the ITEM Supertype and SYSTEM_ITEM/MEDIA_ITEM subtypes? Would the bridge table contain a composite key of EXHIBIT_NR and ITEM_NR, or have I got that completly wrong?


Not completely wrong....
A) remove the NULL field EXHIBIT_NR in ITEM, and replace with the following relation:
CREATE TABLE EXHIBIT_ITEM
(
EXHIBIT_NR INT NOT NULL REFERENCES EXHIBIT(EXHIBIT_NR)
,ITEM_NR INT NOT NULL REFERENCES ITEM(ITEM_NR)
,PRIMARY KEY(EXHIBIT_NR,ITEM_NR)
-- specifics to the relation between the exhibit and the item (system or media), such as date when the item became an exhibit
)

Since not all systems or media are exhibits (non-inclusive relationship), it makes sense to break this information out and keep in a separate relation. by doing this it is possible to store additional information about the why's and when's the relation came into existance.
Depending on how you specify the key:
PRIMARY KEY(EXHIBIT_NR,ITEM_NR) --(many:many)
PRIMARY KEY(ITEM_NR) --(one:many)
An item (sytem or media) can belong to many or one exhibits.


B) remove the NULL field ITEM_NR_SYSTEM in MEDIA_ITEM,
and replace with the following relation:
CREATE TABLE SYSTEM_MEDIA
(
ITEM_NR_SYSTEM INT NOT NULL REFERENCES SYSTEM_ITEM(ITEM_NR)
,ITEM_NR_MEDIA INT NOT NULL REFERENCES MEDIA_ITEM(ITEM_NR) PRIMARY KEY
-- specifics to the relation between the system and item, such as reason why this media belongs to the system
)

All media will have an entry in the MEDIA_ITEM table.
Any media that is also related to a system will have an entry in this table.
Go to Top of Page
   

- Advertisement -