- Introduction To MySQL
- MySQL RDBMS
- MySQL SQL
- MySQL SELECT Statement
- MySQL WHERE Clause
- MySQL AND, OR And NOT Operators
- MySQL ORDER BY Keyword
- MySQL INSERT INTO Statement
- MySQL NULL Values
- MySQL UPDATE Statement
- MySQL DELETE Statement
- MySQL LIMIT Clause
- MySQL MIN() And MAX() Functions
- MySQL COUNT(), AVG() And SUM() Functions
- MySQL LIKE Operator
- MySQL Wildcards
- MySQL IN Operator
- MySQL BETWEEN
- MySQL Aliases
- MySQL Joins
- MySQL INNER JOIN Keyword
- MySQL LEFT JOIN Keyword
- MySQL RIGHT JOIN Keyword
- MySQL CROSS JOIN Keyword
- MySQL Self Join
- MySQL UNION Operator
- MySQL GROUP BY Statement
- MySQL HAVING Clause
- MySQL EXISTS Operator
- MySQL ANY And ALL Operators
- MySQL INSERT INTO SELECT Statement
- MySQL CASE Statement
- MySQL NULL Functions
- MySQL Comments
- MySQL Operators
- MySQL CREATE DATABASE Statement
- MySQL DROP DATABASE Statement
- MySQL CREATE TABLE Statement
- MySQL DROP TABLE Statement
- MySQL ALTER TABLE Statement
- MySQL Constraints
- MySQL NOT NULL Constraint
- MySQL UNIQUE Constraint
- MySQL PRIMARY KEY Constraint
- MySQL FOREIGN KEY Constraint
- MySQL CHECK Constraint
- MySQL DEFAULT Constraint
- MySQL CREATE INDEX Statement
- MySQL AUTO INCREMENT Field
- MySQL Working With Dates
- MySQL Views
- MySQL Data Types
- MySQL Functions
MySQL UNIQUE Constraint
MySQL UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint.
However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
Practice Excercise Practice now
UNIQUE Constraint On CREATE TABLE
The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
Practice Excercise Practice now
UNIQUE Constraint On ALTER TABLE
To create a UNIQUE constraint on the "ID" column when the table is already created, use the following SQL:
ADD UNIQUE (ID);
To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);
Practice Excercise Practice now
DROP A UNIQUE Constraint
To drop a UNIQUE constraint, use the following SQL:
DROP INDEX UC_Person;
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP