In this video I have shown how to add a Unique Constraint in SQL Server table field by SQL Script.
Suppose I want that in my SQL field every data must be unique and it will be checked just when I want to insert any duplicate data. When anyone want insert duplicate data it will throw an error from database system.
Actually if can be checked from application user interface also it we do it in database level, it will increase database integrity and efficient.
For this case we can set it by creating Unique Constraint by which every data will by checked through the Constraint and will show an error if any duplicated data tried to insert.
These process can be done by two ways: by design view and SQL script.
Here is the Syntax of SQL Script:
When Creating Table:
SYNTAX:
CREATE TABLE TableName
(
Fieldname datatype UNIQUE
)
To set it to multiple field:
CREATE TABLE TableName
(
Fieldname01 datatype,
Fieldname02 datatype,
- - - - - -
Fieldname nth datatype,
CONSTRAINT NameofConstraint UNIQUE (Fieldname01, Fieldname02)
)
After Creating Table: After Creating table unique key can be added by Altering
SYNTAX:
ALTER TABLE Tablename
ADD CONSTRAINT NameOfConstraint UNIQUE (FieldNames);
To Delete/Drop the Constraint :
ALTER TABLE Tablename
DROP CONSTRAINT NameOfConstraint
Here I have shown how to do to do it by design View:
Here is the steps:
1. Create a table
2. Go to design view of table structure
3. Right click then -- Indexes/Keys - - Click Add - - Give a meaning full name – Change type to Unique key - - Select table and Column name and sorting order - - Save the table
4. By Same process we can do it for two field
To delete the unique constraint: Expand Table from Object explorer - - Select Created Constraint under Key Tab – Right mouse -- Delete
To find the code I have written in this video, join to our facebook group by clicking following link:
/ 2719024791473074
You can also like to our facebook Page to get all video code clicking on following link:
https://www.facebook.com/permalink.ph...
You can follow us on twitter clicking on following link:
/ itsolut62078624
We will create thousands of video related with information technology from our practical experience and those are used in our daily work and development. Stay with us.
Do like, share and comments and subscribe to my channel and click on bell button to get notification when new video uploaded. You may suggest me to make new video with new topic.
My Channel Subscription link:
/ @itsolution8998
Thanks again
Sur cette page du site, vous pouvez voir la vidéo en ligne How to add Unique Constraint in SQL table by SQL script durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur ITSolution 19 février 2019, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 76 fois et il a aimé 1 téléspectateurs. Bon visionnage!