Open the navicat tool to connect to the mysql service, and then choose to open a database. Here, a test database is selected for demonstration, and then foreign keys for the user table and school table are selected. Select the t_user table and click Design Table.
On the design table page, the default is the field page. Here you can edit the field content for the table. We select the foreign key option.
Related recommendations: "Navicat for mysql graphic tutorial"
Foreign key setting page, select the second column field For the button on the right, you can leave the first column blank and a foreign key name will be automatically generated. You can customize one if you need to standardize the naming.
In the field drop-down list that pops up, select a field to which you want to add a foreign key. Here we are adding a foreign key to the school table, so select school_id.
#Then select the reference table, which is the table pointed to by the foreign key. The reference database does not need to be selected. The default is the current database. Reference table selection school table t_school_info.
Refer to the selection of fields, because the two tables are bound by id association, just select id here.
The last is the constraint rules when deleting and updating. If you choose cascade deletion CASCADE, the school table data deletion will cascade delete the user data. You can also choose the RESTRICT constraint method. You must first delete all users associated with a school before you can delete school information.
#After completing the above selections, click Save, and then the foreign key constraints will take effect. You can edit the data in the table to see the effect.
The above is the detailed content of How to add constraints in navicat. For more information, please follow other related articles on the PHP Chinese website!