- Introduction To SQL
- SQL Syntax
- SQL SELECT Statement
- SQL SELECT DISTINCT Statement
- SQL WHERE Clause
- SQL AND, OR And NOT Operators
- SQL ORDER BY Keyword
- SQL INSERT INTO Statement
- SQL NULL Values
- SQL UPDATE Statement
- SQL DELETE Statement
- SQL TOP, LIMIT, FETCH FIRST Or ROWNUM Clause
- SQL MIN() And MAX() Functions
- SQL COUNT(), AVG() And SUM() Functions
- SQL LIKE Operator
- SQL Wildcards
- SQL IN Operator
- SQL BETWEEN Operator
- SQL Aliases
- SQL Joins
- SQL INNER JOIN Keyword
- SQL LEFT JOIN Keyword
- SQL RIGHT JOIN Keyword
- SQL FULL OUTER JOIN Keyword
- SQL Self Join
- SQL UNION Operator
- SQL GROUP BY Statement
- SQL HAVING Clause
- SQL EXISTS Operator
- SQL ANY And ALL Operators
- SQL SELECT INTO Statement
- SQL INSERT INTO SELECT Statement
- SQL CASE Statement
- SQL NULL Functions
- SQL Stored Procedures For SQL Server
- SQL Comments
- SQL Operators
- SQL CREATE DATABASE Statement
- SQL DROP DATABASE Statement
- SQL BACKUP DATABASE For SQL Server
- SQL CREATE TABLE Statement
- SQL DROP TABLE Statement
- SQL ALTER TABLE Statement
- SQL Constraints
- SQL NOT NULL Constraint
- SQL UNIQUE Constraint
- SQL PRIMARY KEY Constraint
- SQL FOREIGN KEY Constraint
- ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder;
- SQL DEFAULT Constraint
- SQL CREATE INDEX Statement
- SQL AUTO INCREMENT Field
- SQL Working With Dates
- SQL Views
- SQL Injection
- SQL Hosting
- SQL Data Types For MySQL, SQL Server, And MS Access
SQL UNIQUE Constraint
SQL 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
SQL UNIQUE Constraint On CREATE TABLE
The following SQL creates a UNIQUE
constraint on the "ID" column when the "Persons" table is created:
SQL Server / Oracle / MS Access:
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
MySQL:
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:
MySQL / SQL Server / Oracle / MS Access:
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
Practice Excercise Practice now
SQL UNIQUE Constraint On ALTER TABLE
To create a UNIQUE
constraint on the "ID" column when the table is already created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ADD UNIQUE (ID);
To name a UNIQUE
constraint, and to define a UNIQUE
constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);
Practice Excercise Practice now
DROP A UNIQUE Constraint
To drop a UNIQUE
constraint, use the following SQL:
MySQL:
DROP INDEX UC_Person;
SQL Server / Oracle / MS Access:
DROP CONSTRAINT 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