Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the foreign key in mysql

Shulou Source: shulou.com Published: 2022-05-31 21:33:01 09月22日 Update

This article introduces the relevant knowledge of "what is the foreign key in mysql". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Foreign keys definitions are subject to the following conditions:

Both tables must be InnoDB tables and they must not be TEMPORARY tables.

Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For non-binary (character) string columns, the character set and collation must be the same.

In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.

In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

Index prefixes on foreign key columns are not supported. One consequence of this is that BLOB and TEXT columns cannot be included in a foreign key, because indexes on those columns must always include a prefix length.

If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically.

According to the instructions in mysql, when building foreign keys, we must pay attention to the relationship between the two table fields and be consistent. There are more data types in mysql than in oracle. I encountered the problem of int and unsigned. The id field of 1 table is int, so that the id field of 1 table is int,unsigned. As a result, I built a foreign key many times, but it was unsuccessful. I looked for it for a long time before I saw it. It still seems too careless.

This is the end of the content of "what is the foreign key in mysql". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Fields content more knowledge practicality consistency success carelessness achievement and then between dilemmas geniuses realities situations data articles cases types results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Docker Xiaomi macOS