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 enforce referential integrity with Foreign Key constraint?

Execute the following Microsoft SQL Server Transact-SQL (T-SQL) script in Management Studio (SSMS) Query Editor, SQLCMD or other client software to demonstrate the use of FOREIGN KEY constraint to enforce referential integrity between the Currency and CurrencyRate tables.

USE [tempdb]

 

GO

 

CREATE TABLE [dbo].[Currency] (

  [CurrencyCode] [NCHAR](3)    NOT NULL,

  [Name]         NCHAR(25)    NOT NULL,

  [ModifiedDate] [DATETIME]    NOT NULL CONSTRAINT [DF_Currency_ModifiedDate] DEFAULT (getdate()),

  CONSTRAINT [PK_Currency_CurrencyCode] PRIMARY KEY CLUSTERED ( [CurrencyCode] ASC ) WITH ( PAD_INDEX = OFF,IGNORE_DUP_KEY = OFF ) ON [PRIMARY])

ON [PRIMARY]

 

GO

 

USE [tempdb]

 

GO

 

CREATE TABLE [dbo].[CurrencyRate] (

  [CurrencyRateID]   [INT]    IDENTITY ( 1 , 1 )    NOT NULL,

  [CurrencyRateDate] [DATETIME]    NOT NULL,

  [FromCurrencyCode] [NCHAR](3)    NOT NULL,

  [ToCurrencyCode]   [NCHAR](3)    NOT NULL,

  [AverageRate]      [MONEY]    NOT NULL,

  [EndOfDayRate]     [MONEY]    NOT NULL,

  [ModifiedDate]     [DATETIME]    NOT NULL CONSTRAINT [DF_CurrencyRate_ModifiedDate] DEFAULT (getdate()),

  CONSTRAINT [PK_CurrencyRate_CurrencyRateID] PRIMARY KEY CLUSTERED ( [CurrencyRateID] ASC ) WITH ( PAD_INDEX = OFF,IGNORE_DUP_KEY = OFF ) ON [PRIMARY])

ON [PRIMARY]

 

GO

 

ALTER TABLE [dbo].[CurrencyRate]

WITH CHECK ADD CONSTRAINT [FK_CurrencyRate_Currency_FromCurrencyCode] FOREIGN KEY ( [FromCurrencyCode] ) REFERENCES [dbo].[Currency]([CurrencyCode])

 

GO

 

ALTER TABLE [dbo].[CurrencyRate]

 CHECK CONSTRAINT [FK_CurrencyRate_Currency_FromCurrencyCode]

 

GO

 

ALTER TABLE [dbo].[CurrencyRate]

WITH CHECK ADD CONSTRAINT [FK_CurrencyRate_Currency_ToCurrencyCode] FOREIGN KEY ( [ToCurrencyCode] ) REFERENCES [dbo].[Currency]([CurrencyCode])

 

GO

 

ALTER TABLE [dbo].[CurrencyRate]

 CHECK CONSTRAINT [FK_CurrencyRate_Currency_ToCurrencyCode]

 

GO

 

-- premature population of CurrencyRate result in error

INSERT dbo.CurrencyRate

      ([CurrencyRateDate],

       [FromCurrencyCode],

       [ToCurrencyCode],

       [AverageRate],

       [EndOfDayRate])

SELECT getdate(),

       1,

       2,

       0.55,

       0.40

 

GO

 

/*

Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CurrencyRate_Currency_FromCurrencyCode". The conflict occurred in database "tempdb", table "dbo.Currency", column 'CurrencyCode'.

The statement has been terminated.

*/

 

--correct population of tables

INSERT [dbo].[Currency]

      ([CurrencyCode],

       [Name])

SELECT 1,

       'USD'

 

INSERT [dbo].[Currency]

      ([CurrencyCode],

       [Name])

SELECT 2,

       'EUR'

 

INSERT dbo.CurrencyRate

      ([CurrencyRateDate],

       [FromCurrencyCode],

       [ToCurrencyCode],

       [AverageRate],

       [EndOfDayRate])

SELECT getdate(),

       1,

       2,

       0.85,

       0.80

 

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.