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

藏色散人
Release: 2018-10-23 16:03:22
Original
8064 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!