<< previous page   --   table of contents   --   next page >>
| | | | | | | |
  • Return to Table of Contents
  • Table of Contents

    1. General Information
    2. MySQL Installation
    3. Tutorial Introduction
    4. Database Administration
    5. MySQL Optimisation
    6. MySQL Language Reference
    7. MySQL Table Types
    8. MySQL APIs
    9. Extending MySQL

    Chapter 7:  MySQL Table Types 515 7.5.4.2  Foreign Key Constraints Starting from version 3.23.43b InnoDB features foreign key constraints.  InnoDB is the rst MySQL table type which allows you to de ne foreign key constraints to guard the integrity of your data. The syntax of a foreign key constraint de nition in InnoDB: FOREIGN KEY (index_col_name, ...) REFERENCES table_name (index_col_name, ...) [ON DELETE CASCADE | ON DELETE SET NULL] Both tables have to be InnoDB type and there must be an index where the foreign key and the referenced key are listed as the rst columns.  InnoDB does not auto-create indexes on foreign keys or referenced keys:  you have to create them explicitly. Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion.  The size and the signedness of integer types has to be the same.  The length of string types need not be the same. Starting from version 3.23.50 you can also associate the ON DELETE CASCADE or ON DELETE SET NULL  clause with the foreign key constraint. If  ON DELETE CASCADE  is speci ed, and a row in the parent table is deleted, then InnoDB automatically  deletes  also  all  those  rows  in  the  child  table  whose  foreign  key  values  are equal to the referenced key value in the parent row.  If ON DELETE SET NULL is speci ed, the child rows are automatically updated so that the columns in the foreign key are set to the SQL NULL value. Starting  from  version  3.23.50,  InnoDB  does  not  check  foreign  key  constraints  on  those foreign key or referenced key values which contain a NULL column. Starting from version 3.23.50 the InnoDB parser allows you to use backquotes (`) around table and column names in the above de nition but the InnoDB parser is not yet aware of possible variable lower_case_table_names you give in `my.cnf'. An example: CREATE TABLE parent(id INT NOT NULL, PRIMARY KEY (id)) TYPE=INNODB; CREATE TABLE child(id INT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL ) TYPE=INNODB; If  MySQL  gives  the  error  number  1005  from  a  CREATE TABLE  statement,  and  the  error message  string  refers  to  errno  150,  then  the  table  creation  failed  because  a  foreign  key constraint was not correctly formed.  Similarly, if an ALTER TABLE fails and it refers to errno 150, that means a foreign key de nition would be incorrectly formed for the altered table. Starting from version 3.23.50 InnoDB allows you to add a new foreign key constraint to a table through ALTER TABLE yourtablename ADD CONSTRAINT FOREIGN KEY (...) REFERENCES anothertablename(...) Remember to create the required indexes rst, though. In InnoDB versions < 3.23.50 ALTER TABLE or CREATE INDEX should not be used in connec- tion with tables which have foreign key constraints or which are referenced in foreign key
     

    Customer Support CentreMySQL Reference Manual

    Web Hosting Services
    UNIX WEB HOSTING
    MERCHANT ACCOUNTS
    DEDICATED SERVERS
    E-COMMERCE HOSTING
    SUPPORT & FAQ's
    TERMS OF USE
    Domain Services
    DOMAIN
    REGISTRATION
    MANAGE
    YOUR ACCOUNT
    SUPPORT & FAQ's
    TERMS OF USE
    Corporate Info
    ABOUT US
    OUR NETWORK
    CONTACT US
    SITE MAP
    Copyright © 2002 Dyntex Group, Inc. All Rights Reserved
  • Return to Table of Contents
  • Back to top

  • Web Hosting: Manuals & FAQ's

    1. Unix-Based Web Hosting
    2. Unix Dedicated Servers
    3. Windows Dedicated Servers
    4. CuteFTP User’s Guide
    5. CuteHTML User’s Guide
    6. WS_FTP Pro User's Guide
    7. Miva Order User's Guide
    8. Miva Merchant User's Guide