Connect with Expertise | Find Experts, Get Answers, Share Insights

foreign key

 
Join Date: May 2009
Posts: 63
#1: Jan 22 '10
hi,
can i anybody tell me..the use of FOREIGN KEY.I am creating two tables,one with the PRIMARY KEY and the other one is the FOREIGN KEY.


For Example :

Expand|Select|Wrap|Line Numbers
  1. create table customer(cid int not null,cname varchar(50),PRIMARY KEY(cid))type=innodb;



Expand|Select|Wrap|Line Numbers
  1. create table orders(orderid int not null PRIMARY KEY,cost int not null,cid int not null,index(cid),FOREIGN KEY(cid) REFERENCES PRIMARY KEY(cid))type=innodb;




what will do the FOREIGN KEY ?when i am trying to insert a record in the FOREIGN KEY table(ORDERS TABLE),its INSERTING .But its not reflected in the CUSTOMER TABLE.

Same thing,when i am trying to insert a record in the CUSTOMER TABLE,its INSERTING.But its not reflected in the ORDERS TABLE.

Same like for DELETING and for UPDATING.......please help me out.The need of FOREIGN KEY.I cant get it properly.thanks

dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#2: Jan 22 '10

re: foreign key


Maybe this will help:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `customers` (
  2.   `cid` int(10) unsigned NOT NULL auto_increment,
  3.   `cname` varchar(50) default NULL,
  4.   PRIMARY KEY  (`cid`)
  5. ) ENGINE=InnoDB;
  6.  
  7.  
  8. CREATE TABLE  `orders` (
  9.   `orderid` int(10) unsigned NOT NULL auto_increment,
  10.   `cid` int(10) unsigned NOT NULL,
  11.   `cost` decimal(10,2) NOT NULL,
  12.   PRIMARY KEY  (`orderid`),
  13.   KEY `cid` (`cid`),
  14.   CONSTRAINT `FK_customers_1` FOREIGN KEY (`cid`) REFERENCES `customers` (`cid`) ON UPDATE CASCADE
  15. ) ENGINE=InnoDB;
  16.  
 
Join Date: May 2009
Posts: 63
#3: Jan 22 '10

re: foreign key


hi,
No....its not working.I did not seen any changes in the foreign key table......
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#4: Jan 22 '10

re: foreign key


1- When you enter a record into the customers table, there is no reflection in the orders table.

2- When you enter a record into the orders table, you MUST provide a valid customer id (cid) with the other order column values. If you do not, you will get a "foreign key constraint" error.

3- In other words, you MUST insert a valid customer record BEFORE you can insert a valid orders record and a valid orders record is DIRECTLY dependent on a VALID customer record being existent.

I suggest you read up on "referential integrity."
http://en.wikipedia.org/wiki/Referential_integrity
http://databases.about.com/cs/admini...fintegrity.htm
http://www.odbms.org/download/007.02...ber%202005.PDF

4- When a table is setup as related to another, you typically JOIN the two related tables on the related column(s).

e.g.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO customers (cname) VALUES ("Dave Matthews")
  2. INSERT INTO customers (cname) VALUES ("Jack Johnson")
  3.  
  4. INSERT INTO orders (cid,cost) VALUES (1,100.02)
  5. INSERT INTO orders (cid,cost) VALUES (2,5000)
  6. INSERT INTO orders (cid,cost) VALUES (1,323.44)
  7.  
  8. SELECT b.cname,a.orderid,a.cost FROM orders a 
  9. JOIN customers b ON b.cid = a.cid 
  10. ORDER BY b.cname,a.orderid
  11.  
  12. // Output
  13. cname          orderid       cost
  14. ==================================
  15. Dave Matthews     1         100.02
  16. Dave Matthews     3         323.44
  17. Jack Johnson      2        5000.00
  18.  
 
Join Date: May 2009
Posts: 63
#5: Jan 22 '10

re: foreign key


Thanks....

before that i should enable INNODB Storage Engine.

mysql>show engines;


showing INNODB -> DISABLED.

Problems is due to this ah.How i should enable INNODB Storage Engine.

mysql version : 5.0.51a


and also i am using XAMPP Version 1.6.6a.
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#6: Jan 22 '10

re: foreign key


 
Join Date: May 2009
Posts: 63
#7: Jan 25 '10

re: foreign key


Hi,

thanks.But such configuration files(my.cnf or my.ini) are not found in the mysql/bin directory.I am running Xampp in windows XP.Please help me.I want to work out on the FOREIGN KEY.
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#8: Jan 26 '10

re: foreign key


The configuration file will probably be 1 directory above the bin directory.

If it is installed in c:\Program Files\MySQL\MySQL Server 5.0\ then that's where you'll more than likely find the file my.ini.
Reply