Solved

relationship in Mysql

Posted on 2002-03-14
3
813 Views
Last Modified: 2010-08-05
Hai all,

Presently Iam using phpMyAdmin software to create databases in mysql.
I don't know how to create relationships between the tables.

can anyone who r familiar with the above software can help me out.

Thanks,
Manju
0
Comment
Question by:manju70
  • 2
3 Comments
 
LVL 6

Expert Comment

by:andreif
Comment Utility
Hm, what do you mean exactly by "creating relationship"?

There is only tables in MySQL and all relationships you must keep in your mind - so, it's your own business how to join tables and which criteria to use.

If you mean creating n-m relationship between two tables, you have to create extra table, for example

You have tables BOOK and CATEGORY
where BOOK has book_id as key and CATEGORY has category_id as a key. To make a relationship, you have to create table
BOOK_CATEGORY (table name is up to you :))
with columns book_id and category_id.
Now, to connect books with categories you will add records to BOOK_CATEGORY table
0
 
LVL 5

Accepted Solution

by:
kelfink earned 30 total points
Comment Utility
By relationships, you probably mean FOREIGN KEY, and yes, sort of, mysql can support them.

The syntax is such as:

create table a ( id int not null primary key );
create table b ( a_id int not null,
                 foreign key (a_id) references a );

But, mysql doesn't enforce the integrity, unless you use InnoDB tables...

 insert into a ( id ) values ( 1 );

0
 
LVL 5

Expert Comment

by:kelfink
Comment Utility
I hit submit too soon.

Basically, without using innodb, you will be able to do this:
insert into a ( id ) values ( 1 );
insert into b ( a_id ) values ( 2 );

which should fail, if it were being enforced.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now