Sql Tutorial Class


How to use unique constraint in sql server

How to use unique constraint in sql server.




SQL UNIQUE  Constraint: - It is used to apply different value in column(s).



UNIQUE Constraint allow one NULL value in column.



PRIMARY KEY Constraint do not allow a NULL value in column.



PRIMARY KEY Constraint can have only one in table.



UNIQUE Constraint can have more.




SQL UNIQUE Constraint on CREATE TABLE  Example

CREATE TABLE DeliveredId

(

OrderId varchar(10) NOT NULL CONSTRAINT deliveredprimary PRIMARY KEY,

ProductName varchar(20) NOT NULL,

OrderQuantity int NOT NULL,

ProductPrice float NOT NULL,

CustomerPhone varchar(20)NOT NULL,

TrackingId varchar(20)NOT NULL Constraint unqId UNIQUE

)



INSERT INTO OrderDetails VALUES('001','iPhone','100','500','01657684759','TRK002')

INSERT INTO OrderDetails VALUES('002','Laptop','200','700','01657435435','TRK003')

INSERT INTO OrderDetails VALUES('003','Printer','100','300','01655645332','TRK004')




DELETE or DROP UNIQUE Constraint in SQL
ALTER TABLE OrderDetails DROP CONSTRAINT unqId





ALTER or ADD UNIQUE Constraint in SQL
ALTER TABLE OrderDetails ADD CONSTRAINT unqId UNIQUE (TrackingId)






UNIQUE Constraint in SQL







UNIQUE Constraint in image

How to use unique constraint in sql

No comments:

Post a Comment