Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-25
3
Medium Priority
?
388 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

730 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