foreign key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santhanalakshmi
    New Member
    • May 2009
    • 147

    #1

    foreign key

    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 :

    Code:
    create table customer(cid int not null,cname varchar(50),PRIMARY KEY(cid))type=innodb;



    Code:
    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
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    Maybe this will help:

    Code:
    CREATE TABLE `customers` (
      `cid` int(10) unsigned NOT NULL auto_increment,
      `cname` varchar(50) default NULL,
      PRIMARY KEY  (`cid`)
    ) ENGINE=InnoDB;
    
    
    CREATE TABLE  `orders` (
      `orderid` int(10) unsigned NOT NULL auto_increment,
      `cid` int(10) unsigned NOT NULL,
      `cost` decimal(10,2) NOT NULL,
      PRIMARY KEY  (`orderid`),
      KEY `cid` (`cid`),
      CONSTRAINT `FK_customers_1` FOREIGN KEY (`cid`) REFERENCES `customers` (`cid`) ON UPDATE CASCADE
    ) ENGINE=InnoDB;

    Comment

    • santhanalakshmi
      New Member
      • May 2009
      • 147

      #3
      hi,
      No....its not working.I did not seen any changes in the foreign key table......

      Comment

      • dgreenhouse
        Recognized Expert Contributor
        • May 2008
        • 250

        #4
        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 "referentia l integrity."




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

        e.g.
        Code:
        INSERT INTO customers (cname) VALUES ("Dave Matthews")
        INSERT INTO customers (cname) VALUES ("Jack Johnson")
        
        INSERT INTO orders (cid,cost) VALUES (1,100.02)
        INSERT INTO orders (cid,cost) VALUES (2,5000)
        INSERT INTO orders (cid,cost) VALUES (1,323.44)
        
        SELECT b.cname,a.orderid,a.cost FROM orders a 
        JOIN customers b ON b.cid = a.cid 
        ORDER BY b.cname,a.orderid
        
        // Output
        cname          orderid       cost
        ==================================
        Dave Matthews     1         100.02
        Dave Matthews     3         323.44
        Jack Johnson      2        5000.00

        Comment

        • santhanalakshmi
          New Member
          • May 2009
          • 147

          #5
          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.

          Comment

          • dgreenhouse
            Recognized Expert Contributor
            • May 2008
            • 250

            #6
            See:
            http://www.mydigitallife.info/2006/1...-installation/

            Comment

            • santhanalakshmi
              New Member
              • May 2009
              • 147

              #7
              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.

              Comment

              • dgreenhouse
                Recognized Expert Contributor
                • May 2008
                • 250

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

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

                Comment

                Working...