Add foreign key to existing tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AMT India
    New Member
    • Feb 2007
    • 64

    Add foreign key to existing tables

    I am using mysql 4.2. I have a big database with lots of data. Now there is no foreign key relationship between the tables. But every one has a primary key. Can I alter these tables to bring foreign key without affecting the data? Thanx in advance....
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, AMT.

    Are these tables MyISAM or InnoDB?

    If you're not sure, try issuing a [code=mysql]SHOW CREATE TABLE `tableName`\G[/code].

    Look for ENGINE=MyISAM or ENGINE=InnoDB.

    Comment

    • AMT India
      New Member
      • Feb 2007
      • 64

      #3
      Originally posted by pbmods
      Heya, AMT.

      Are these tables MyISAM or InnoDB?

      If you're not sure, try issuing a [code=mysql]SHOW CREATE TABLE `tableName`\G[/code].

      Look for ENGINE=MyISAM or ENGINE=InnoDB.
      Some of them are MyISAM and some are InnoDB...what I have to do?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, AMT.

        Only InnoDB tables properly support foreign key constraints right now. You can change the storage engine for any table by executing this command:
        [code=mysql]
        ALTER TABLE `tableName` ENGINE=InnoDB;
        [/code]

        MyISAM is faster, especially for large data sets, than InnoDB. The trade-off is that MyISAM does not support row locking, transactions nor foreign key constraints.

        Check out this document for more information on setting up foreign key constraints on an InnoDB table.

        Comment

        • AMT India
          New Member
          • Feb 2007
          • 64

          #5
          Originally posted by pbmods
          Heya, AMT.

          Only InnoDB tables properly support foreign key constraints right now. You can change the storage engine for any table by executing this command:
          [code=mysql]
          ALTER TABLE `tableName` ENGINE=InnoDB;
          [/code]

          MyISAM is faster, especially for large data sets, than InnoDB. The trade-off is that MyISAM does not support row locking, transactions nor foreign key constraints.

          Check out this document for more information on setting up foreign key constraints on an InnoDB table.
          Ok..i will change tables to InnoDB. But can I add foreign key for a table which contains data?

          Comment

          • AMT India
            New Member
            • Feb 2007
            • 64

            #6
            specify foreign keys for tables with data

            I have two tables with lots of data. How can I specify foreign keys for these tables in MySQL 4. (Both tables has primary key)

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Merged duplicate threads.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, AMT.

                Originally posted by AMT India
                Ok..i will change tables to InnoDB. But can I add foreign key for a table which contains data?
                You sure can. Simply use the ALTER TABLE command, and add foreign keys the same way you would any other.

                Comment

                • mwasif
                  Recognized Expert Contributor
                  • Jul 2006
                  • 802

                  #9
                  Originally posted by AMT India
                  Ok..i will change tables to InnoDB. But can I add foreign key for a table which contains data?
                  Make sure you do not have FULLTEXT index in any MyISAM table. FULLTEXT is only supported in MyISAM tables.

                  Comment

                  • AMT India
                    New Member
                    • Feb 2007
                    • 64

                    #10
                    Thanx a lot for all the replies...

                    Comment

                    Working...