Unique Constraint [MySQL] [Database]

Unique constraint (UNIQUE)

The role of the unique constraint:

The value used to restrict a field cannot be repeated (but there can be multiple nulls)

Features of Unique Constraint:

  1. The same table can have multiple unique constraints
  2. A unique constraint can be that the value of a column is unique, or the value of a combination of multiple columns is unique
    • That is: our unique constraints can be declared as table-level constraints (can be declared as table-level constraints can also be declared as composite constraints)
  3. Unique constraint allows column value to be null
    • Multiple values ​​of fields with unique constraints are allowed to be null
  4. When creating a unique constraint, if the unique constraint is not named, the unique contract name defaults to the same as the column name
  5. MySQL will create a unique index on the column with the unique constraint by default, and the unique index name created is the same as the corresponding unique constraint name

How to add a unique constraint?

Here we add unique constraints in two ways

Method 1: Add a unique index during CREATE TABLE

Below we use an example to illustrate how to add a unique constraint to CREATE TABLE

CREATE TABLE test2(
# 这里就是声明了一个列级约束,也就是我们的唯一性约束
id INT UNIQUE,
last_name VARCHAR(15),
email VARCHAR(25),
salary DECIMAL(10,2)
);
  • Note: If we declare a column-level constraint when we create a table, then we cannot use the CONSTRAINT keyword to name the constraint
  • Here is the uniqueness constraint added to the id field in the test2 table (declared here as a column-level constraint)
CREATE TABLE test3(
id INT UNIQUE,
last_name VARCHAR(15),
email VARCHAR(25),
salary DECIMAL(10,2),
#这里就是声明了一个表级约束
[CONSTRAINT uk_test2_email] UNIQUE(email)
);
  • The content in [] above can be omitted, and the content in [] is actually a name for the unique constraint
  • The CONSTRAINT uk_test2_email here is actually a name for this unique constraint: uk_test2_email
  • If we do not name the unique constraint when adding the unique constraint, the default is the same as the field name (column name)

After we have created the table and added the unique constraint, at this time we can find that in the data in the table, the values ​​of the fields to which the unique constraint has been added cannot be repeated, but multiple values ​​can be NULL at the same time. It also shows that multiple nulls are not the same

INSERT INTO test2(id,last_name,email,salary)
VALUES(1,'tom','[email protected]',4800);
  • Here is the first time adding data after the table is created, this time there is no problem with the execution
INSERT INTO test2(id,last_name,email,salary)
VALUES(1,'tom','[email protected]'4600);
  • At this time, the addition will fail, because we have added a unique constraint to the id field in the test2 table. At this time, the value of the id field in the data in our test2 table cannot be repeated. At this time, we judge when adding this record. There is already a record with id 1 in our table. At this time, the id of the record we want to add cannot be 1, so an error will occur at this time.
INSERT INTO test2(id,last_name,email,salary)
VALUES(2,'tom1',NULL,4600),
(s,'tom2',NULL,4600);
  • At this time, the addition is successful, that is to say, we can add null values ​​to the fields where we add the unique constraint (UNIQUE), and we can add null values ​​multiple times. We are here to email the fields declared as unique in the table once. Add two records with null values, this time there is no error in the execution

For unique constraints, we can not only declare it as a column-level constraint, but also declare it as a table-level constraint. field)

So how to declare a unique constraint as composite?

CREATE TABLE USER(
id INT
'name' VARCHAR(15),
'password' VARCHAR(25),

#表级约束(这个时候我们将这个表级约束声明为复合约束)
CONSTRAINT uk_user_name_pwd UNIQUE('name','password'),
);
  • Here we are declaring a table-level constraint, and we are declaring it as a conforming constraint. Why is it called a composite constraint?
    • It should be this time that we constrain two fields (name and password) at a time with the same constraint

Note: When the unique constraint is declared as conforming, it is only repeated when the values ​​of these fields of the composite unique constraint are the same

eg:

INSERT INTO USER
VALUES(1,'tom','abc'),
(1,'tom1','abc');
  • We know that we added the composite unique constraint to the name and password fields. This time, when the values ​​of the name field and the password field are the same, it means no repetition. Here only the value of password is repeated, and for the name of the name The value is not repeated, this time it will be added successfully

Method 2: Add a unique constraint during ALTER TABLE

In case 2, we have two more ways to add unique constraints:

Method 1: ADD
ALTER TABLE test2
ADD [CONSTRAINT uk_test2_sal] UNIQUE(salary);
  • Adding a unique constraint in this add method is similar to adding a table-level constraint when adding it in CREATE TABLE.

Method 2: MODIFY

ALTER TABLE test2
MODIFY last_name VARCHAR(15) UNIQUE;

  • This MODIFY method of adding unique constraints is similar to the format of adding column-level constraints when we added them in CREATE TABLE.

So how do I remove the uniqueness constraint?

Before removing the unique constraint we need to know:

  1. Fields that add unique constraints will also automatically create unique indexes
  2. Deleting a unique constraint can only be deleted by deleting the unique index
  3. When deleting a unique index, specify a unique index name. The unique index name is the same as the unique constraint name.
  4. If the constraint name is not specified when creating a unique constraint, then if it is a single-column constraint, the default is the same as the column name, and if it is a composite constraint, then the default is the same as the column name of the first column in the combined column

Then here we give an example of how to delete a unique index (DROP INDEX)

Note: After the unique index is deleted, the corresponding unique constraint will be automatically deleted

ALTER TABLE test2
DROP INDEX last_name;

  • When we add a unique constraint to the last_name field in the test2 table, we add it as a column-level constraint during CREATE TABLE. When we add a unique index, we cannot specify a constraint name, so the unique constraint name defaults to the same as Our last_name column name is the same, then the unique index name is the same as the unique constraint name, then both are last_name
ALTER TABLE test2
DROP TABLE uk_test2_sal;

  • Here, when we add a unique constraint to the salary field in the test2 table, we add it as a table-level constraint during CREATE TABLE. When we add this unique constraint, we specify a name for this unique constraint: uk_test2_sal , then our unique index name at this time is: uk_test2_sal

Guess you like

Origin blog.csdn.net/m0_57001006/article/details/123564449