SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to create the Order and OrderDetail tables?

Execute the following Microsoft SQL Server Transact-SQL (T-SQL) script in Management Studio (SSMS) Query Editor, SQLCMD or other client software to create the Order and OrderDetail tables. OrderID is the Primary Key for the Order table. The OrderDetail table has many to one relationship to the Order table. The OrderDetail table has a composite Primary Key: OrderID and LineNumber. Each product ordered has a record in OrderDetail.

 

CREATE TABLE AdventureWorks.[dbo].[Order]

(

[OrderID] int identity(1,1) PRIMARY KEY,

[PurchaseOrderID] [int] NOT NULL,

[EmployeeID] [int] NULL,

[VendorID] [int] NULL,

[TaxAmount] [smallmoney] NULL,

[Freight] [smallmoney] NULL,

[SubTotal] [money] NULL,

[TotalDue] [money] NULL,

[Status] [tinyint] NOT NULL,

[ShipMethodID] tinyint NULL,

[ShipDate] [smalldatetime] NOT NULL,

[OrderDate] [smalldatetime] NULL,

[RevisionNumber] [tinyint] NULL,

[ModifiedDate] [smalldatetime] NULL,

CONSTRAINT constrOrdersRangeYear

CHECK ([OrderDate] >= '20001001'

AND [OrderDate] <= getdate()),

[ModifiedBy] char(10) NULL

)

GO

 

 

CREATE TABLE AdventureWorks.[dbo].[OrderDetail](

[OrderID] [int] REFERENCES [Order](OrderID),

[LineNumber] [smallint] ,

[ProductID] [int] NULL,

[UnitPrice] [smallmoney] NULL,

[OrderQty] [smallint] NULL,

[ReceivedQty] [float] NULL,

[RejectedQty] [float] NULL,

[OrderDate] [smalldatetime] NOT NULL

CONSTRAINT OrderDetailsRangeYearCK

CHECK ([OrderDate] >= '20000101'

AND [OrderDate] <= getdate()),

[DueDate] [datetime] NULL,

[LineTotal] AS (([UnitPrice]*[OrderQty])),

[StockedQty] AS (([ReceivedQty]-[RejectedQty])),

[ModifiedDate] [datetime] NOT NULL

CONSTRAINT [dfltOrderDetailsModifiedDate]

DEFAULT (getdate()),

[ModifiedBy] char(10) NULL,

PRIMARY KEY CLUSTERED

(

      [OrderID] ASC,

      [LineNumber] ASC

) ON [PRIMARY]

)

 

GO

 

 

 

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.