If you want to add a “named” default and a “named” unique constraint to one column, when creating a table with T-SQL, you can use the “inline syntax”:

 

Here we create a table with a column “Code”, that has a “named” default and a “named” unique constraint:

if object_id('dbo.Product') is null
begin
    create table dbo.Product
    (
        Id int identity(1,1) not null constraint PK_dbo_Product_Id primary key,
        Code uniqueidentifier not null constraint UQ_dbo_Product_Code unique(Code)
                                       constraint DF_dbo_Product_Code default newid()
    )
end
go

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.