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 > Database Server Software > MySQL > how to relate two tables (was "real basic question")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-05, 11:42
godonholiday godonholiday is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
how to relate two tables (was "real basic question")

hi, i am an absolute begginer with this.
i was wondering how i do the following:

I have a table called topic i created it like this:

CREATE TABLE Topic (
Topic_ID NOT NULL AUTO_INCREMENT PRIMARY KEY,
Topic_Name TEXT,
Topic_info TEXT
);

i have another table called Img

CREATE TABLE Img (
Img_ID NOT NULL AUTO_INCREMENT PRIMARY KEY,
Img_Name TEXT,
Img_Autor TEXT
);

I want to have the Img_ID also in the Topic table (as a foreing key??)

how do i do this? do i just type it in bellow Topic_info? or do i have to call it a key?

any help would be great

thanks in advace
g

Last edited by godonholiday; 01-02-05 at 15:26. Reason: no help given
Reply With Quote
  #2 (permalink)  
Old 01-02-05, 18:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
in order to do this correctly, you need to answer the following questions

can a single topic have 0 images?
can a single topic have more than 1 image?
can a single image have 0 topics?
can a single image have more than 1 topic?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-03-05, 06:43
godonholiday godonholiday is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
can a single topic have 0 images?
Yes some time it does not need an image

can a single topic have more than 1 image?
No each topic will have a max of one image

can a single image have 0 topics?
YES as a new image might be needed, the old one will be stored but not used

can a single image have more than 1 topic?
No each image is related to the specific topic

i hope you can help

thanks in advance

g
Reply With Quote
  #4 (permalink)  
Old 01-03-05, 10:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
based on those specs, it's a 1-1 relationship and therefore the tables could be merged, or the foreign could go into either table

but let's, for the sake of argument, say that you can use the same picture in more than one topic

thus the foreign key goes into the topic

create your tables in this sequence:

CREATE TABLE Img
( Img_ID integer NOT NULL AUTO_INCREMENT PRIMARY KEY
, Img_Name varchar(255)
, Img_Author varchar(255)
);

CREATE TABLE Topic
( Topic_ID integer NOT NULL AUTO_INCREMENT PRIMARY KEY
, Topic_Name varchar(255)
, Topic_info varchar(255)
, Img_ID integer
, foreign key (Img_ID) references Img(Img_ID)
);

note that ordinary mysql tables don't support foreign keys, but it's a good idea to define them that way anyway

also, i gave your ID columns the INTEGER datatype, and changed TEXT to VARCHAR(255) because you probably don't need 65K to name a topic or image
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-03-05, 10:38
godonholiday godonholiday is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
thanks, going to give it a go now,

thanks again

g
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