954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How To Create Foreign Key

hi,im really new to MySQL,can some one help me in joining table pls...i hv 2 tables,customer table and record table

customer table with CustomerID,Customer Name,CustomerAddress,
record table with RecordID,CustomerID,Date,

how to make the CustomerID in record as foreign key? how to join that two tables so that record will be deleted when i delete the customer id? :sad:

i hope u guys could give me a hand :cry: thanks!

jessica81
Newbie Poster
1 post since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

hai i am also new

hemalatha.a.s
Newbie Poster
3 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

Just to take note that there is no Foreign key in MySQL unless your database's type is InnoDB. I assume you are using MyISAM.

DELETE FROM Customer WHERE CustomerID = yourcustomerid; DELETE FROM Records WHERE CusomerID = yourcustomerid;

Hope it helps.

PoA
Posting Whiz in Training
237 posts since Jul 2004
Reputation Points: 26
Solved Threads: 9
 
hai i am also new

Hi,
The below link has given the help that how to create foreign key in MySQL? Its really great help. http://www.1keydata.com/sql/sql-foreign-key.html

Cheers :)

joshisumitnet
Light Poster
26 posts since Jan 2009
Reputation Points: 9
Solved Threads: 0
 

Just to take note that there is no Foreign key in MySQL unless your database's type is InnoDB. I assume you are using MyISAM.

Hope it helps.


Hi Friends,

I also haveread in many websites that we cannot create foreign key in MyISM type database or tables. But It is possible to create foreign key in MyISM type database or tables. Here I am submitting two tables and the relationship between them using foreign key concept. So do practice in your maching also.
Two tables
1. Country
2. State

SQL for Country:
Create Table Country(Id int(2) Primary Key Not Null Auto_Increment,
CName varchar(10))

SQL for State:
Create Table State (Id int(2) Primary Key Not Null Auto_Increment,SName varchar(10),cid int(2),foreign key (cid) references country(id))

Try, it is working successfully in my machine (laptop).
Cheers :)

joshisumitnet
Light Poster
26 posts since Jan 2009
Reputation Points: 9
Solved Threads: 0
 

Just use it as if it was a foreign key!

A true foreign key would enable you to enforce relational integrity, but you can work with a database without it actually being enforced. You just carry out joins in the usual way and they work.

What you lose out on is not automatically getting a warning when there is an error such as no parent record for your foreign key eg foreignID should be 1023 and you entry 1032, for which no record exists in the other table. This would be accepted in MyISM but rejected in InnoDB.

drjohn
Posting Pro in Training
448 posts since Mar 2010
Reputation Points: 76
Solved Threads: 80
 

what if im using mysql?
n i wanna make a foreign key using PhpMyAdmin @local host.
some one help me please...

<(_ _)>

guzm_zone
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

which version of sql mngr you are using 2000 or 2005


and u can do same task by using trigares

hirenpatel53
Posting Whiz in Training
221 posts since Jun 2010
Reputation Points: 10
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You