MySQL Constraint – UNIQUE




Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

Hello everyone in the previous article we learn about MySQL Constraint – NOT NULL and in this article, we will learn UNIQUE constraint with example. Let’s go.
What is a UNIQUE key?
The MySQL UNIQUE key used to specify that you don’t want to see duplicate data in a given field means all the values in a column are different. In MySQL, UNIQUE key is implemented through a constraint that is called UNIQUE Constraint.

MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

What is a UNIQUE Constraint?
MySQL UNIQUE constraint does not allow inserting a duplicate value in a column and each data value of a column that has the unique value. If you add to insert an already existing duplicate value in the unique column, then MySQL fetches an error.

Following is the CREATE TABLE statement with UNIQUE constraint syntax:
                              CREATE TABLE table_name (
                                             column1_name data type () UNIQUE,
                                             column2_name data type () UNIQUE
);
For define multiple column UNIQUE constraint then you can use following syntax:
CREATE TABLE table_name(
                              column1_name data type ()
                              column1_name data type ()
                              CONSRTAINT constraint_name UNIQUE (column1_name, column2_name)
);

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/


Example of CREATE TABLE statement with UNIQUE constraint:
                              CREATE TABLE EMPLOYEE (
                                            EID INT NOT NULL,
                                            First_Name VARCHAR (255),
                                            Last_Name VARCHAR (255),
                                            Address VARCHAR (255) UNIQUE,
                              );

MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Example for define multiple column UNIQUE constraint:
                              CREATE TABLE EMPLOYEE (
                                            EID INT NOT NULL,
                                            First_Name VARCHAR (255),
                                            Last_Name VARCHAR (255),
                                            Address VARCHAR (255),
                                            CONSTRAINT UC_EMPLOYEE UNIQUE (EID, Address)
                              );


You can add UNIQUE constraint when the table is already created using the ALTER TABLE statement. Following is the syntax and example:
               Syntax:              
ALTER TABLE table_name
                              ADD CONSTRAINT constraint_name UNIQUE (column_name);
               Example
                              ALTER TABLE EMPLOYEE
                              ADD CONSTRAINT UC_EMPLOYEE UNIQUE (EID, Address);

MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

If you want to DROP created a UNIQUE constraint, then follow the steps:
ALTER TABLE EMPLOYEE
DROP CONSTRAINT UC_EMPLOYEE;



Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/


Comments

Popular posts from this blog

ALTER statement in MySQL

Order By Clause in MySQL