473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

foreign key

147 100+
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
Jan 22 '10 #1
7 2487
dgreenhouse
250 Expert 100+
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.  
Jan 22 '10 #2
santhanalakshmi
147 100+
hi,
No....its not working.I did not seen any changes in the foreign key table......
Jan 22 '10 #3
dgreenhouse
250 Expert 100+
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.  
Jan 22 '10 #4
santhanalakshmi
147 100+
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.
Jan 22 '10 #5
santhanalakshmi
147 100+
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.
Jan 25 '10 #7
dgreenhouse
250 Expert 100+
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.
Jan 26 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: dcp | last post by:
I just installed the 4.1.0-alpha-max-nt version of MySql and have just started playing around with it. My first test was to try to create a couple of tables, one with a foreign key constraint. ...
0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
2
by: geoff | last post by:
The table creation script(at the end of this post) works fine on 4.0.1-alpha-win, but the foreign key constraints fail on 4.0.15-win. I am starting the server with the same command for both...
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
4
by: Simon Bond | last post by:
Hi there, I should point out I am very new to MySql, im trying to learn what I can. Basically im using Navicat to work with my databases. When making a foreign key, i am asked to enter the...
2
by: adammitchell | last post by:
How can you indicate that a FOREIGN KEY constraint references two columns in two different tables? "SQL Server Books Online" show an example of how to reference two columns in the SAME table:...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
10
by: Shawn Chisholm | last post by:
Hi, I am trying to deal with a deadlock situation caused by foreign key references on insert and I was wondering if anyone knows what order the foreign keys are locked (or evaluated) in for a...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.