Solved

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

Posted on 2011-02-25
3
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

734 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