Home  >  Article  >  Daily Programming  >  How to create foreign keys in mysql database? (Pictures + Videos)

How to create foreign keys in mysql database? (Pictures + Videos)

藏色散人
藏色散人Original
2018-10-23 16:03:228113browse

This article mainly introduces how to create foreign keys in mysql database.

Regarding the basic introduction of foreign keys in mysql database, we provide this article [What are Mysql foreign keys? What are the uses? ], I have already introduced it to you, friends who need it can choose to refer to it.

Understanding the basic definition of foreign keys, how do we create foreign keys?

Below we will introduce to you through a simple example how to create a mysql foreign key.

First we can open the management tool through the cmd command and log in to the mysql database.

How to create foreign keys in mysql database? (Pictures + Videos)

Then query the database through the show database statement.

How to create foreign keys in mysql database? (Pictures + Videos)

Then use test database and execute the following SQL statement written by ourselves. Here we create a data table stu1 and add a foreign key to the c_id field so that it can be associated with the class id.

How to create foreign keys in mysql database? (Pictures + Videos)

#We can check whether the data table stu1 is created successfully.

How to create foreign keys in mysql database? (Pictures + Videos)

Finally, we can query the table structure or table information through the "show create table stu1" statement to see whether the foreign key is successfully created.

How to create foreign keys in mysql database? (Pictures + Videos)

The picture shows that the foreign key has been successfully created.

Note: Only the InnoDB storage engine supports creating foreign keys!

This article is an introduction to how to create foreign keys in mysql database. It is simple and easy to understand. I hope it will be helpful to friends in need!

If you want to know more about mysql, you can follow the PHP Chinese website mysql video tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to create foreign keys in mysql database? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn