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 > circular relationship

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-05, 20:41
roustabout roustabout is offline
Registered User
 
Join Date: Jan 2005
Posts: 14
Question circular relationship

Hi all,

Why the cycle in relationship between CREW and SHIP is true?

Thanks
Attached Images
File Type: jpg diagram.JPG (11.3 KB, 167 views)
Reply With Quote
  #2 (permalink)  
Old 01-30-05, 21:53
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Poor design?

-PatP
Reply With Quote
  #3 (permalink)  
Old 01-31-05, 00:10
Vmusic Vmusic is offline
Registered User
 
Join Date: Dec 2004
Posts: 54
Relationships

Hi,
Simply ask yourself what the relationship is.

A crew (person) could work, over time, on one or many ships.

A ship could have one or many crew (people) working on it.

Simple?
Vmusic
Reply With Quote
  #4 (permalink)  
Old 01-31-05, 00:11
Vmusic Vmusic is offline
Registered User
 
Join Date: Dec 2004
Posts: 54
Relationships

I couldn't see the cardinality very well.....but I thought that was a many to many. That's not recursive...
Reply With Quote
  #5 (permalink)  
Old 01-31-05, 01:25
roustabout roustabout is offline
Registered User
 
Join Date: Jan 2005
Posts: 14
Quote:
Originally Posted by Vmusic
Hi,
Simply ask yourself what the relationship is.

A crew (person) could work, over time, on one or many ships.

A ship could have one or many crew (people) working on it.

Simple?
Vmusic
hi all, this is actually a past year exam question.

Here what I understand:

A ship is assigned to one crew which is the captian. The same captain can be a captain on one or many ships.

One crew is assigned to one ship

haha this is confusing... am i correct?

I have implemented the tables in Mysql.

I found out that i have to create the crew table without declaring ship_id foreign key first. Otherwise I can't input some data into the tables. After I have completed entering the data, I used the alter table command to ship's foreign key to crew's tables.

Last edited by roustabout; 01-31-05 at 01:32.
Reply With Quote
  #6 (permalink)  
Old 01-31-05, 06:17
certus certus is offline
Registered User
 
Join Date: Dec 2003
Location: Canada
Posts: 710
Post the question.
__________________
visit: relationary
Reply With Quote
  #7 (permalink)  
Old 01-31-05, 19:37
roustabout roustabout is offline
Registered User
 
Join Date: Jan 2005
Posts: 14
Quote:
Originally Posted by certus
Post the question.
1. Why the circular relationship is true (refer to the diagram).
2. Create Tables using the sql command
Reply With Quote
  #8 (permalink)  
Old 01-31-05, 22:12
certus certus is offline
Registered User
 
Join Date: Dec 2003
Location: Canada
Posts: 710
Create the tables and look at the foreign keys. Do you notice any peculiarities?
__________________
visit: relationary
Reply With Quote
  #9 (permalink)  
Old 02-01-05, 03:52
roustabout roustabout is offline
Registered User
 
Join Date: Jan 2005
Posts: 14
Quote:
Originally Posted by certus
Create the tables and look at the foreign keys. Do you notice any peculiarities?
Yes....

Code:
create table crew
(c_id int not null,
c_name varchar(50),
c_dob date,
s_id int,
primary key(c_id),
foreign key(s_id) references ship(s_id) on delete cascade;
);

Code:
create table ship
(s_id int not null,
s_name varchar(50),
c_id int,
primary key(s_id),
foreign key(c_id) references crew(c_id) on delete cascade
);

IF I were to create the crew table first, it gave an error because of the s_id fk for ship table havent been created . THe problem will be similar if I start creating the ship table first. haha

One way to do it is... to create the ship table without the s_id foreign key and later on after inserting the dummy data, I use this command to add the foreign into the crew table.

Code:
alter table crew
add foreign key(s_id) references ship(s_id) on delete cascade;
Reply With Quote
  #10 (permalink)  
Old 02-01-05, 05:12
certus certus is offline
Registered User
 
Join Date: Dec 2003
Location: Canada
Posts: 710
You have discovered why the circular reference is true.

You've been presented with a design flaw and you found it. You don't have to beat it.
__________________
visit: relationary
Reply With Quote
  #11 (permalink)  
Old 02-01-05, 06:11
certus certus is offline
Registered User
 
Join Date: Dec 2003
Location: Canada
Posts: 710
Consider this as a solution:
Attached Images
File Type: jpg untitled.JPG (28.3 KB, 150 views)
__________________
visit: relationary
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