in Technology by
What is a UNIQUE constraint in SQL?

1 Answer

0 votes
by

A UNIQUE constraint ensures that all values in a column are different. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined per table. The code syntax for UNIQUE is quite similar to that of PRIMARY KEY and can be used interchangeably.

CREATE TABLE Students ( 	 /* Create table with a single field as unique */
    ID INT NOT NULL UNIQUE
    Name VARCHAR(255)
);

CREATE TABLE Students ( 	 /* Create table with multiple fields as unique */
    ID INT NOT NULL
    LastName VARCHAR(255)
    FirstName VARCHAR(255) NOT NULL
    CONSTRAINT PK_Student
    UNIQUE (ID, FirstName)
);

ALTER TABLE Students 	 /* Set a column as unique */
ADD UNIQUE (ID);

ALTER TABLE Students 	 /* Set multiple columns as unique */
ADD CONSTRAINT PK_Student 	 /* Naming a unique constraint */
UNIQUE (ID, FirstName);

Related questions

0 votes
    What constraint is not a strict requirement for a service to be called a RESTful web service? 1. Uniform Interface 2. Code ... 3. Stateless 4. Client-Server 5. All of the above...
asked Jun 26, 2021 in Technology by JackTerrance
0 votes
    How can we temporarily disable a foreign key constraint in MySQL?...
asked Feb 1, 2021 in Technology by JackTerrance
0 votes
    What are the limitations of CHECK constraint?...
asked Dec 18, 2020 in Technology by JackTerrance
0 votes
0 votes
    In order to overcome constraint of linearly separablity concept of multilayer feedforward net is proposed? (a) yes ... Networks of Neural Networks Please answer the above question....
asked Sep 4, 2022 in Education by JackTerrance
0 votes
    I am using Laravel 5.7 version. I got the below error for users_activations table while run the command ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am using Laravel 5.7 version. I got the below error for users_activations table while run the command ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I'm trying to populate a database using seeder. I get an error: SQLSTATE[23000]: Integrity constraint ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Drop Table cannot be used to drop a table referenced by a _________ constraint. (a) Local Key (b) ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    To include integrity constraint in an existing relation use : (a) Create table (b) Modify table (c) ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which of the following can be addressed by enforcing a referential integrity constraint? (a) All phone ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
...