Home mysql Why do you need a Constraint Name in MySQL?

Why do you need a Constraint Name in MySQL?

Author

Date

Category

Why do you need Constraint Name when creating communication tables in MySQL?
Example:

constraint name friends_ibfk_1
Constraint Name Friends_IBFK_2.
Constraint Name Friends_IBFK_3.

Answer 1, Authority 100%

each object in the database, including the external keys of the tables, should be the name necessary in order to further modify this object. If the name is not specified, MYSQL himself will think of a certain name.

Proposal Constraint Name Specifies the name of the external key explicitly. By setting the key name later, if you want to remove it, you can do it using Alter Table Tab Drop Foreign Key Name .

The same CONSTRAINT keyword is necessary for some compatibility with SQL standards. For example, in MySQL using it, it is also possible to declare a check limit that according to SQL standards should be restricted on the field, for example, that its value must be greater than zero. True, MySQL does not support verification of such restrictions, but the syntax of their creation does not consider for compatibility with other DBMS.


Answer 2, Authority 33%

This is a limitation of an external key that allows you to link data from one or more tables, for example, so that you cannot delete data from one table, without deleting related data to another or to delete / update in the same table led to a cascade deletion / update of data in other tables. This allows you to maintain the database consistency, not allowing the keys that refer to non-existent entries.


Answer 3

If you ask for the reason that these constraints interfere with you to perform import / export data, they can be disabled for the time of script execution:

set Foreign_Key_Checks = 0; Turns off

respectively
set Foreign_Key_Checks = 1;
Includes external keys check.

However, it is unsafe in terms of data integrity. Usually, the error of the external keys occurs when the order of insertion of the tables is disturbed. For example, the cities table is inserted, where the external key points to the country (each city belongs to some country), but the tables of countries still do not exist.

As I said, you can disable the keys on the time of inserting the data on your own and risk. But then there is a chance to get “drawing” data. In the case of cities, you can get the city, to. Does not belong to any countries.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions