Friday, 20 March 2009

SQL Contraints in CREATE TABLE statement

CREATE TABLE [dbo].[Table1](
  [Id] [int] NOT NULL,
  [ParentId] [int] NOT NULL,
  [Name] [datetime] NULL,
  [Date] [datetime] NULL
  CONSTRAINT [DF_Table1_Date] DEFAULT getdate(), --default
  CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED  -- primary key
  (
    [Id] ASC
  ),
  CONSTRAINT [UQ_Table1_Name] UNIQUE NONCLUSTERED --unique key
  (
    [Name] ASC
  ),
  CONSTRAINT [FK_Table1_Table2_ParentId] FOREIGN KEY --foreign key
  (
    [ParentId]
  ) REFERENCES [Table2](
    [Id]
  )
)

No comments:

Post a Comment