FK Assignment on Surrogate Key Table Throws Error

FK Assignment on Surrogate Key Table Throws Error

am 31.07.2007 23:12:39 von pbd22

Hello.

I am getting the following error:

"""There are no primary or candidate keys in the referenced table
'UserPrecedence' that match the referencing column list in the foreign
key 'FK_USERS__UserPrecedence'."""

When running the following script:

alter table Users
add constraint FK_USERS__UserPrecedence
foreign key (userID)
references UserPrecedence(owner_userID)

I am just learning my way around Foreign Key assignments involving
surrogate keys. Do I need to be populating the owner_userID column in
UserPrecedence as I populate the userID column in Users? They are
supposed to be the same. I assumed that the PK/FK relationship causes
owner_userID to mimic userID in an automated way without much
intervention needed on my part. I guess I need some advice as to why I
am getting this error.

UserPrecedence Looks like this:

CREATE TABLE [dbo].[UserPrecedence](
[up_order] [int] NOT NULL,
[profile_userID] [bigint] NULL,
[owner_userID] [bigint] NOT NULL,
[search_name] [varchar](50) NULL,
CONSTRAINT [PK_UserPrecedence] PRIMARY KEY CLUSTERED
(
[owner_userID] ASC,
[up_order] 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

And Users Looks Like This:

CREATE TABLE [dbo].[Users](
[registerDate] [datetime] NULL,
[password] [varchar](50) NULL,
[role] [varchar](50) NULL,
[securityQuestion] [varchar](50) NULL,
[securityAnswer] [varchar](50) NULL,
[zipCode] [varchar](50) NULL,
[alternateEmail] [varchar](50) NULL,
[emailAddress] [varchar](50) NULL,
[bday_month] [varchar](50) NULL,
[bday_day] [varchar](50) NULL,
[bday_year] [varchar](50) NULL,
[userID] [int] IDENTITY(1,1) NOT NULL,
[gender] [varchar](50) NULL,
[siteId] [varchar](50) NULL,
[city] [varchar](50) NULL,
[state] [varchar](50) NULL,
[country] [varchar](50) NULL,
[edit_date] [varchar](50) NULL,
[lastName] [varchar](50) NULL,
[firstName] [varchar](50) NULL,
[confirmed] [bit] NULL DEFAULT ((0)),
CONSTRAINT [PK_userID] PRIMARY KEY CLUSTERED
(
[userID] 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

Re: FK Assignment on Surrogate Key Table Throws Error

am 31.07.2007 23:48:18 von Joe Celko

Have you considered a proper relational schema instead of a mock
sequential files? Do you even know about ISO-11179 data element
naming rules?

Look at what you posted. The name "UserPrecedence" is a relationship
among users, not an entity. You are using camelCase which is soooo
bad even MS gave up on it.

>> I am just learning my way around Foreign Key assignments involving surrogate keys. <<

Considering that FREIGN KEY is part of the foundation of RDBMS, isn't
that like a surgeon saying "I just found out about blood!" ? Also,
SQL Server has no surrogate keys -- read Dr. Codd. Why did you use
BIGINT? Do you really have a data model bigger than all the atomic
particles in the Universe?

Please give me an example of a "zip_code VARCHAR (50)" please? You
will get one because your design is so F***KED that it allows it. In
fqct this piece of sh*t is full of VARCHAR (50) columns. You never
looked at a single ISO standard, have you?

Please stop[p programing before you kill someone.