This article mainly introduces the simple use of mysql foreign keys.
In the previous article, I have introduced you to the basic definition of mysql foreign keys and the method of creating foreign keys. Friends who need it can read these two articles first.
《What is Mysql foreign key? What are the uses? 》
《How to create a foreign key in mysql database? 》
I think everyone should have a certain understanding of mysql foreign keys.
Below we will give you a simple example to introduce to you the basic operations of mysql foreign keys in Navicat (database management tool).
Navicat is a set of fast, reliable and relatively cheap database management tools, designed to simplify database management and reduce system management costs. And is built with an intuitive graphical user interface that allows you to create, organize, access and share information in a secure and simple way.
First of all, there are two data tables in this database, class (class table) and stu1 (student table).
Then if we want to associate these two tables, we need to create a foreign key for the student table, which is the id in the class, that is to say, the student table The foreign key c_id field is the primary key id of the class table.
And we need to fill in the content in the class table before we can fill in the data in the student table.
When we do not delete the data in the student table, but directly delete the records in the class table, an error will occur when executing delete.
This is because the class table involves the data of the student table with foreign keys. Only by deleting the data of the related student table can the deletion operation be performed in the class table!
When we change the c_id in the student table, an error will also appear (as shown below). It should be noted that the foreign key field c_id here must be consistent with the primary key data in the class table on which we rely. The type matches!
The content of this chapter is about the related use of mysql foreign keys Operation introduction, it is also very simple, I hope it will be helpful to friends in need !
So about the Navicat database management tool mentioned in the article, friends who are interested can also read the previous related Navicat articles:
[How about Navicat Import and export sql files? 】
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 use mysql foreign keys? Introduction to the use of foreign keys in Mysql database (pictures, text + video). For more information, please follow other related articles on the PHP Chinese website!