Solved

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

Posted on 2011-02-25
3
362 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now