Mysql foreign Key Usage Analysis

Source: Internet
Author: User

Mysql foreign key usage is the basic knowledge in the Mysql database. The following describes Mysql foreign Key Usage in detail. If you are interested in this, take a look.

Mysql foreign key constraints were created with Mysql last night and have never been successful. We found this one today. It turns out that only InnoDB tables can use Mysql Foreign keys.

Only InnoDB tables can use foreign keys.

 
 
  1. CREATE TABLE person (   
  2. id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,   
  3. name CHAR(60) NOT NULL,   
  4. PRIMARY KEY (id)   
  5. )type=innoDB;   
  6.  
  7. CREATE TABLE shirt (   
  8. id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,   
  9. style ENUM('t-shirt', 'polo', 'dress') NOT NULL,   
  10. color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,   
  11. owner SMALLINT UNSIGNED NOT NULL,   
  12. FOREIGN KEY (owner) REFERENCES PERSON(id)   
  13. ON DELETE CASCADE   
  14. ON UPDATE CASCADE,   
  15. PRIMARY KEY (id)   
  16. )type=innoDB;   
  17.  

Then open MySQL Administrator and finally see something about the foreign key. Try to manually delete it and use MySQL Administrator to create it.

 
 
  1. INSERT INTO person VALUES (NULL, 'Antonio Paz');   
  2.  
  3. SELECT @last := LAST_INSERT_ID();   
  4.  
  5. INSERT INTO shirt VALUES   
  6. (NULL, 'polo', 'blue', @last),   
  7. (NULL, 'dress', 'white', @last),   
  8. (NULL, 't-shirt', 'blue', @last);   
  9.  
  10. INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');   
  11.  
  12. SELECT @last := LAST_INSERT_ID();   
  13.  
  14. INSERT INTO shirt VALUES   
  15. (NULL, 'dress', 'orange', @last),   
  16. (NULL, 'polo', 'red', @last),   
  17. (NULL, 'dress', 'blue', @last),   
  18. (NULL, 't-shirt', 'white', @last);   
  19.  
  20. SELECT * FROM person;   
  21.  
  22. SELECT * FROM shirt;   
  23.  

Test UPDATE Association

 
 
  1. UPDATE PERSON SET id=3 WHERE id=1;   
  2. SELECT * FROM shirt;   

Key to test DELETE

 
 
  1. DELETE FROM PERSON WHERE id=3;   
  2. SELECT * FROM shirt;  

New Pricing strategies for MySQL database products

Take you to the DB2 Character Set and MySql Character Set

View MySQL database table commands

Hash-based Mysql table sharding

How MySQL defines foreign keys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.