Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:45:38+00:00 2026-05-16T14:45:38+00:00

USE [Fk_Test2] GO /****** Object: Table [dbo].[Owners] Script Date: 08/20/2010 16:52:44 ******/ SET ANSI_NULLS

  • 0
USE [Fk_Test2]
GO

/****** Object:  Table [dbo].[Owners]    Script Date: 08/20/2010 16:52:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Owners](
    [Owner] [varchar](10) NOT NULL,
 CONSTRAINT [PK_Owners] PRIMARY KEY CLUSTERED 
(
    [Owner] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


/****** Object:  Table [dbo].[Key]    Script Date: 08/20/2010 16:49:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Key](
    [owner] [varchar](10) NOT NULL,
    [key_id] [varchar](10) NOT NULL,
    [description] [varchar](10) NOT NULL,
 CONSTRAINT [PK_Key] PRIMARY KEY CLUSTERED 
(
    [owner] ASC,
    [key_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Key]  WITH CHECK ADD  CONSTRAINT [FK_Key_Owners] FOREIGN KEY([owner])
REFERENCES [dbo].[Owners] ([Owner])
GO

ALTER TABLE [dbo].[Key] CHECK CONSTRAINT [FK_Key_Owners]
GO



/****** Object:  Table [dbo].[Bldg]    Script Date: 08/20/2010 16:50:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Bldg](
    [bldg] [varchar](10) NOT NULL,
    [owner] [varchar](10) NOT NULL,
 CONSTRAINT [PK_Bldg] PRIMARY KEY CLUSTERED 
(
    [bldg] ASC,
    [owner] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Bldg]  WITH CHECK ADD  CONSTRAINT [FK_Bldg_Owners] FOREIGN KEY([owner])
REFERENCES [dbo].[Owners] ([Owner])
GO

ALTER TABLE [dbo].[Bldg] CHECK CONSTRAINT [FK_Bldg_Owners]
GO


/****** Object:  Table [dbo].[KeyToBuilding]    Script Date: 08/20/2010 17:13:52 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[KeyToBuilding](
    [owner] [varchar](10) NOT NULL,
    [bldg] [varchar](10) NOT NULL,
    [key_id] [varchar](10) NOT NULL,
 CONSTRAINT [PK_KeyToBuilding] PRIMARY KEY CLUSTERED 
(
    [owner] ASC,
    [bldg] ASC,
    [key_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[KeyToBuilding]  WITH CHECK ADD  CONSTRAINT [FK_KeyToBuilding_Key] FOREIGN KEY([owner], [key_id])
REFERENCES [dbo].[Key] ([owner], [key_id])
GO

ALTER TABLE [dbo].[KeyToBuilding] CHECK CONSTRAINT [FK_KeyToBuilding_Key]
GO

ALTER TABLE [dbo].[KeyToBuilding]  WITH CHECK ADD  CONSTRAINT [FK_KeyToBuilding_Bldg] FOREIGN KEY([owner], [bldg])
REFERENCES [dbo].[Bldg] ([owner], [bldg])
GO

ALTER TABLE [dbo].[KeyToBuilding] CHECK CONSTRAINT [FK_KeyToBuilding_Bldg]
GO

Whenever I try to set this up I get an Error
“The columns in table “Key” or “Bldg” do not match an existing primary key or UNIQUE constraint.

Am I trying to implement this relationship in the wrong way? or doing something dumb?

The system is for tracking the keys (physical) people have to different buildings. That is why there is key_id and Key they aren’t database keys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:45:39+00:00Added an answer on May 16, 2026 at 2:45 pm

    This is the source of the syntax error;

    the primary key in the Bldg table is :

        CONSTRAINT PK_Bldg PRIMARY KEY (bldg, [owner])
    

    And in KeyToBuilding table, FK_KeyToBuilding_Bldg you are trying to reference ([owner], bldg) instead of (bldg, [owner])

    It helps if you simply clean-up the code a bit, the following executes fine:

    CREATE TABLE dbo.Owners (
        [owner] varchar(10) NOT NULL
    
      , CONSTRAINT PK_Owners PRIMARY KEY CLUSTERED ([owner])
    ) ;
    
    
    CREATE TABLE dbo.[Key] (
        [owner] varchar(10) NOT NULL
      , key_id  varchar(10) NOT NULL
      , [description] varchar(10) NOT NULL
    
      , CONSTRAINT [PK_Key] PRIMARY KEY ([owner], key_id)
    ) ;
    
    ALTER TABLE dbo.[Key] ADD CONSTRAINT FK_Key_Owners
        FOREIGN KEY([owner] REFERENCES dbo.Owners ([owner])  ;
    
    
    CREATE TABLE dbo.Bldg (
        bldg    varchar(10) NOT NULL
      , [owner] varchar(10) NOT NULL
    
      , CONSTRAINT PK_Bldg PRIMARY KEY (bldg, [owner])
    ) ;
    
    ALTER TABLE dbo.Bldg ADD CONSTRAINT FK_Bldg_Owners
        FOREIGN KEY([owner] REFERENCES dbo.Owners ([owner]);
    
    
    CREATE TABLE dbo.KeyToBuilding (
        [owner] varchar(10) NOT NULL
      , bldg    varchar(10) NOT NULL
      , key_id  varchar(10) NOT NULL
    
        , CONSTRAINT PK_KeyToBuilding PRIMARY KEY ([owner], key_id)
    );
    
    ALTER TABLE dbo.KeyToBuilding ADD
        CONSTRAINT FK_KeyToBuilding_Key
          FOREIGN KEY([owner], key_id) REFERENCES dbo.[Key] ([owner], key_id)
    
      , CONSTRAINT FK_KeyToBuilding_Bldg
          FOREIGN KEY(bldg, [owner]) REFERENCES dbo.Bldg (bldg, [owner]) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Hibernate 3.6.8. Here is my tables: CREATE TABLE UTILIZATION ( ID BIGINT
I'm not sure how to use composite key. My Categories table has CategoryId (PK,FK),
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
I have a Notes table with a uniqueidentifier column that I use as a
Should we use a flag for soft deletes, or a separate joiner table? Which
I use the fallowing NHibernate Mapping (NH 3.2) with MS SQL Server 2008 (set
I just started to use the PHP symfony framework. Currently I'm trying to create
I want to use a table valued function and call it from within multiple
I have asp.net membership and I use the built in Create user method since
When I use hibernate eclipse plugin to generate java class from my table, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.