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 > Please give me example or link for these examples

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-02-10, 08:24
linux1880 linux1880 is offline
Registered User
 
Join Date: Jul 2010
Posts: 39
Please give me example or link for these examples

Dear friends,

Can anyone please give me link or tutorials for 1-to1 and 1-to-many mysql simple example, tutorials or links please ? I tried google but I couldn't find simple ones. Please help.
Reply With Quote
  #2 (permalink)  
Old 08-02-10, 09:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
to begin with, a true one-to-one relationship is not possible, it will always be one-to-zero-or-one
Code:
CREATE TABLE users
( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT
, name VARCHAR(99) NOT NULL
, hatsize DECIMAL(4,3) NOT NULL DEFAULT 6.875 -- six and seven eighths
);

CREATE TABLE user_resumes
( id INTEGER NOT NULL PRIMARY KEY 
, CONSTRAINT resume_user
     FOREIGN KEY ( id ) REFERENCES users ( id )
, resume BLOB NOT NULL
);
a user's rows cannot simultaneously be added to both tables (with or without one of them being an auto_increment)

a user is first added to the users table, and then subsequently added to the user_resumes table

since id is the PK of both tables, it is unique, so the relationship is one-to-zero-or-one
Code:
CREATE TABLE user_phones
( id INTEGER NOT NULL 
, phone VARCHAR(12) NOT NULL
, PRIMARY KEY ( id , phone ) 
, CONSTRAINT phone_user
     FOREIGN KEY ( id ) REFERENCES users ( id )
);
here a user can have multiple phone numbers (but the PK ensures that a given user cannot have the same number more than once)

so this is a one-to-zero-or-many relationship (the "zero" bit for the same reason as above)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-02-10, 11:59
linux1880 linux1880 is offline
Registered User
 
Join Date: Jul 2010
Posts: 39
thank you so much r937, it helped me a lot to understand. How can i use the same scene as many-to-many relationship ?
Reply With Quote
  #4 (permalink)  
Old 08-02-10, 12:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by linux1880 View Post
How can i use the same scene as many-to-many relationship ?
Code:
CREATE TABLE websites
( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT
, url VARCHAR(99) NOT NULL
);
CREATE TABLE users_own_websites
( user_id INTEGER NOT NULL
, site_id INTEGER NOT NULL
, PRIMARY KEY ( user_id , site_id )
, CONSTRAINT own_user
    FOREIGN KEY ( user_id ) REFERENCES users ( id )
, CONSTRAINT own_site
    FOREIGN KEY ( site_id ) REFERENCES websites ( id )
);
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-02-10, 15:29
linux1880 linux1880 is offline
Registered User
 
Join Date: Jul 2010
Posts: 39
sorry for the dumb question again, I could not understand
CONSTRAINT own_user,
CONSTRAINT resume_user
and
CONSTRAINT phone_user

Are these CONSTRAINT important to relate two tables ? when for example FOREIGN KEY ( site_id ) REFERENCES websites ( id ) is relating two tables. What is CONSTRAINT and CONSTRAINT values for ?
Reply With Quote
  #6 (permalink)  
Old 08-02-10, 15:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
a foreign key ~is~ a constraint, but assigning a user-defined constraint name is optional

by specifying the constraint name explicitly, you make it easier to remove the constraint (should it ever become necessary to do so) by referring to it using the constraint name you assigned, rather than letting the constraint name be assigned some default value by the database if you don't define it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-02-10, 15:50
linux1880 linux1880 is offline
Registered User
 
Join Date: Jul 2010
Posts: 39
Thank you so much sir. However while looking at MySQL :: MySQL 5.1 Reference Manual :: 13.6.4.4 FOREIGN KEY Constraints it says tables must be InnoDB to use foreign key constraints, while I did not specify engine, i think default is MyISAM. Will it work or I am getting wrong ?
Reply With Quote
  #8 (permalink)  
Old 08-02-10, 16:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
that is correct, foreign keys are not enforced in myisam tables

however, the CONSTRAINT syntax may still be specified, and will be parsed for correctness(and then ignored)

also, you can change the default table type if you wish
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-02-10, 17:54
linux1880 linux1880 is offline
Registered User
 
Join Date: Jul 2010
Posts: 39
Thank you so much, much appreceated. I did populate all of those tables with raw data on them, what mysql commands would be best to show results relating to column of one field to another fields, should i do joins commands etc or there are other commands. Sorry for too many questions.
Reply With Quote
  #10 (permalink)  
Old 08-02-10, 18:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by linux1880 View Post
what mysql commands would be best to show results relating to column of one field to another fields
sorry, i do not understand "results relating to column of one field to another fields"

i think you want a join but it's pretty hard to tell from this general description


Quote:
Originally Posted by linux1880 View Post
Sorry for too many questions.
i think you've pretty well got the answer you wanted (about 1-to-1, 1-to-many, and many-to-many relationships)

please don't let this thread evolve into a long tutoring session on how to do everything with databases, okay?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 08-02-10, 18:11
linux1880 linux1880 is offline
Registered User
 
Join Date: Jul 2010
Posts: 39
Thank you, I have got the answer for what i asked
Reply With Quote
  #12 (permalink)  
Old 08-03-10, 21:33
cherrylou0510 cherrylou0510 is offline
Registered User
 
Join Date: Aug 2010
Posts: 1
Great forum! I have learned a lot here. Thanks for the information guys!!
Reply With Quote
  #13 (permalink)  
Old 08-05-10, 13:14
grammowitch grammowitch is offline
Registered User
 
Join Date: Aug 2010
Posts: 1
ahh cool just what I was looking for too. THanks
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