In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what are some common constraints in mysql. I hope you will gain a lot after reading this article. Let's discuss it together.
Several common constraints in mysql are: 1, primary key; 2, default value; 3, unique; 4, foreign key; 5, non-empty. Constraint is a kind of restriction, which ensures the integrity and uniqueness of the data of the table by restricting the data of the rows or columns of the table.
Constraint is a kind of restriction, which ensures the integrity and uniqueness of the data of the table by restricting the data of the rows or columns of the table.
Several constraints commonly used in MYSQL:
1. Primary key
The primary key (PRIMARY KEY) is used to constrain a row in a table, and as an identifier for this row, the primary key can be accurately located to a row in a table, so the primary key is very important. The primary key requires that the data in this row cannot be duplicated and cannot be empty.
There is also a special primary key-compound primary key. The primary key can not only be a column in a table, but can also be identified by two or more columns in the table.
2. Default value
The default value constraint (DEFAULT) specifies what to do when the insert data is empty for a column with a DEFAULT constraint.
The DEFAULT constraint will only be reflected when using the insert statement (introduced in the previous lab). In the INSERT statement, if the position constrained by DEFAULT has no value, then the position will be filled with the value of DEFAULT.
3. Unique constraint
The unique constraint (UNIQUE) is relatively simple, which states that the values of a column specified in a table must not have duplicate values, that is, each value of this column is unique.
When the newly inserted data of the INSERT statement duplicates the existing data, INSERT fails if there is a UNIQUE constraint.
4. Foreign key
Foreign keys (FOREIGN KEY) can not only ensure data integrity, but also represent the relationship between tables.
A table can have multiple foreign keys, each foreign key must REFERENCES (reference) the primary key of another table, and the value of the column constrained by the foreign key must have a corresponding value in the column it references.
In INSERT, if the value constrained by the foreign key does not have a corresponding in the reference column, such as the following command, there is no dpt3 in the reference column (dpt_name of the department table), then INSERT fails
5. Non-empty
Non-null constraints (NOT NULL), which can be understood by listening to the name, must be non-empty when inserting values.
If a non-null constraint is violated in MySQL, no error will be reported, only a warning will be given.
After reading this article, I believe you have a certain understanding of the common constraints in mysql. If you want to know more about it, welcome to follow the industry information channel. Thank you for your reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.