SQL Certification - SPLessons

SQL Certification Unique Constraint

Home > Lesson > Chapter 17
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Certification Unique Constraint

Unique Constraint

shape Description

By defining a column with Unique Constraint, then that unique constraint column will not accept duplicate values. i.e, unique constraint will accept a single character or number. It will not accept repeated values in the table.One of a kind Constraint segment can just contain one NULL esteem. A Unique Constraint can be made upon a segment that can contain NULLs. Nonetheless, at most, just a solitary line may ever contain a NULL in that segment.

shape Syntax

create table <table_name>(<column_name1> datatype(size) constraint constraint name constraint  type, <column_name2> datatype(size));
Table_name => Any accurate table in the database. Column_name =>The operation that can be perform on a column in the table. constraint_type =>constraint type will specify the type of constraint, that going to be executed in the database.

shape Examples

By viewing the below example, the concept of Unique Constraint can be understood easily. [c]sql> create table stud2(stu_id number(4)constraint stud2_stu_id_unique,stu_name varchar(255),fee int); Query OK, 0 rows affected (0.32 sec) sql> insert into stud2 values(1001,'rock',12000); Query OK, 1 row affected (0.16 sec) sql> insert into stud2 values(1002,'hairy',13000); Query OK, 1 row affected (0.17 sec) sql> insert into stud2 values(1003,'scena',13000); Query OK, 1 row affected (0.16 sec) sql> insert into stud2 values(1003,'joe',13000); ERROR 1062 (23000): Duplicate entry '1003' for key 'stu_id'[/c] In the above example, the Unique Constraint is applied to column_name emp_id in which only unique emp_id's  values will be accept, and duplicate emp_id values will not be accepted.

Summary

shape Key Points

  • Unique Constraint - Unique constraint will not accept duplicate values.