Solved

Can you have multiple foreign key constraints in a SQL create table statement?

Posted on 2011-02-25
3
372 Views
Last Modified: 2012-05-11
I get the folowing error when the Booksregions create table scripts is executed.

Msg 8148, Level 16, State 0, Line 1
More than one column FOREIGN KEY constraint specified for column 'RegionID', table 'dbo.BooksRegions'.

The books and Regions tables are created just fine.

Can you have multiple foreign key constraints in a SQL create table statement?

How do i resolve this error?



CREATE TABLE [dbo].[Books](
      [ISBN] [char](20) NOT NULL Primary key,
      [AuthorID] [char](10) NOT NULL,
      [Title] [nvarchar](250) NOT NULL,
      [Copyright_Year] [char](4) NOT NULL,
      [PublisherID] [char](10) NOT NULL,
      [Language] [char](30) NOT NULL,
      [Price] [money] NOT NULL,
)

CREATE TABLE [dbo].[Regions](
      [RegionID] [char](10) NOT NULL,
      [RegionDescription] [varchar](100) NULL,
      [Country] [char](30) NOT NULL
Primary Key(RegionID)
)


CREATE TABLE [dbo].[BooksRegions](
      [ISBN] [char](20) NOT NULL ,
      [RegionID] [char](10) NOT NULL
CONSTRAINT [FK_BooksRegion_ISBN] FOREIGN KEY([ISBN])
REFERENCES [dbo].[Books] ([ISBN])
CONSTRAINT [FK_BooksRegion_RegionID] FOREIGN KEY([RegionID])
REFERENCES [dbo].[Regions] ([RegionID])
       )
0
Comment
Question by:wademi
3 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 34977667
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 34977697
Hi,

I think your problem here is a missing comma between the constraints. Try this



/peter

CREATE TABLE [dbo].[Books](
      [ISBN] [char](20) NOT NULL Primary key,
      [AuthorID] [char](10) NOT NULL,
      [Title] [nvarchar](250) NOT NULL,
      [Copyright_Year] [char](4) NOT NULL,
      [PublisherID] [char](10) NOT NULL,
      [Language] [char](30) NOT NULL,
      [Price] [money] NOT NULL,
)

CREATE TABLE [dbo].[Regions](
      [RegionID] [char](10) NOT NULL,
      [RegionDescription] [varchar](100) NULL,
      [Country] [char](30) NOT NULL
Primary Key(RegionID)
) 


CREATE TABLE [dbo].[BooksRegions](
      [ISBN] [char](20) NOT NULL ,
      [RegionID] [char](10) NOT NULL
CONSTRAINT [FK_BooksRegion_ISBN] FOREIGN KEY([ISBN])
REFERENCES [dbo].[Books] ([ISBN]),
CONSTRAINT [FK_BooksRegion_RegionID] FOREIGN KEY([RegionID])
REFERENCES [dbo].[Regions] ([RegionID])
       )

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34977766
try this
two comma's missing

CREATE TABLE [dbo].[BooksRegions](
      [ISBN] [char](20) NOT NULL ,
      [RegionID] [char](10) NOT NULL,
CONSTRAINT [FK_BooksRegion_ISBN] FOREIGN KEY([ISBN])
REFERENCES [dbo].[Books] ([ISBN]),
CONSTRAINT [FK_BooksRegion_RegionID] FOREIGN KEY([RegionID])
REFERENCES [dbo].[Regions] ([RegionID])
       )
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question