473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Chicken and egg problem: FOREIGN KEY reference to a table that doesn't exist yet

I'm trying to create a local copy of a popular CRM database called
Salesforce.com. Many of the tables in the DB have FOREIGN KEY
references that I want to preserve, but I've run into a chicken and egg
problem. Table "A" has a reference to table "B," and table "B" has a
reference to table "A." So I can't CREATE one until the other exists.
Is there a way to disable these checks until I've created all the
schema?

Here's what I see (error first, then SQL that caused it):

Server: Msg 1767, Level 16, State 1, Line 1
Foreign key 'FK__UserRole__LastMo__48CFD27E' references invalid table
'User'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.

CREATE TABLE salesforce3.dbo."UserRole" ("Id" varchar(18) PRIMARY KEY ,
"Name" varchar(40), "ParentRoleId" varchar(18) REFERENCES
"UserRole"(Id), "RollupDescription" varchar(80),
"OpportunityAccessForAccountOwner" varchar(40),
"CaseAccessForAccountOwner" varchar(40), "LastModifiedDate" datetime,
"LastModifiedById" varchar(18) REFERENCES "User"(Id), "SystemModstamp"
datetime);

CREATE TABLE salesforce3.dbo."User" ("Id" varchar(18) PRIMARY KEY ,
"Username" varchar(80), "LastName" varchar(80), "FirstName"
varchar(40), "CompanyName" varchar(80), "Division" varchar(80),
"Department" varchar(80), "Title" varchar(80), "Street" text, "City"
varchar(40), "State" varchar(20), "PostalCode" varchar(20), "Country"
varchar(40), "Email" varchar(80), "Phone" varchar(40), "Fax"
varchar(40), "MobilePhone" varchar(40), "Alias" varchar(8), "IsActive"
bit, "TimeZoneSidKey" varchar(40), "UserRoleId" varchar(18) REFERENCES
"UserRole"(Id), "LocaleSidKey" varchar(40), "ReceivesInfoEmails" bit,
"ReceivesAdminInfoEmails" bit, "EmailEncodingKey" varchar(40),
"ProfileId" varchar(18) REFERENCES "Profile"(Id), "LanguageLocaleKey"
varchar(40), "EmployeeNumber" varchar(20), "WirelessEmail" varchar(80),
"LastLoginDate" datetime, "CreatedDate" datetime, "CreatedById"
varchar(18) REFERENCES "User"(Id), "LastModifiedDate" datetime,
"LastModifiedById" varchar(18) REFERENCES "User"(Id), "SystemModstamp"
datetime, "UserPermissionsMarketingUser" bit,
"UserPermissionsOfflineUser" bit, "UserPermissionsWirelessUser" bit,
"UserPermissionsSuperCssUser" bit, "UserPermissionsAvantgoUser" bit);

Jul 23 '05 #1
2 8356

<ad**********@marketsquaresolutions.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I'm trying to create a local copy of a popular CRM database called
Salesforce.com. Many of the tables in the DB have FOREIGN KEY
references that I want to preserve, but I've run into a chicken and egg
problem. Table "A" has a reference to table "B," and table "B" has a
reference to table "A." So I can't CREATE one until the other exists.
Is there a way to disable these checks until I've created all the
schema?

Here's what I see (error first, then SQL that caused it):

Server: Msg 1767, Level 16, State 1, Line 1
Foreign key 'FK__UserRole__LastMo__48CFD27E' references invalid table
'User'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.

CREATE TABLE salesforce3.dbo."UserRole" ("Id" varchar(18) PRIMARY KEY ,
"Name" varchar(40), "ParentRoleId" varchar(18) REFERENCES
"UserRole"(Id), "RollupDescription" varchar(80),
"OpportunityAccessForAccountOwner" varchar(40),
"CaseAccessForAccountOwner" varchar(40), "LastModifiedDate" datetime,
"LastModifiedById" varchar(18) REFERENCES "User"(Id), "SystemModstamp"
datetime);

CREATE TABLE salesforce3.dbo."User" ("Id" varchar(18) PRIMARY KEY ,
"Username" varchar(80), "LastName" varchar(80), "FirstName"
varchar(40), "CompanyName" varchar(80), "Division" varchar(80),
"Department" varchar(80), "Title" varchar(80), "Street" text, "City"
varchar(40), "State" varchar(20), "PostalCode" varchar(20), "Country"
varchar(40), "Email" varchar(80), "Phone" varchar(40), "Fax"
varchar(40), "MobilePhone" varchar(40), "Alias" varchar(8), "IsActive"
bit, "TimeZoneSidKey" varchar(40), "UserRoleId" varchar(18) REFERENCES
"UserRole"(Id), "LocaleSidKey" varchar(40), "ReceivesInfoEmails" bit,
"ReceivesAdminInfoEmails" bit, "EmailEncodingKey" varchar(40),
"ProfileId" varchar(18) REFERENCES "Profile"(Id), "LanguageLocaleKey"
varchar(40), "EmployeeNumber" varchar(20), "WirelessEmail" varchar(80),
"LastLoginDate" datetime, "CreatedDate" datetime, "CreatedById"
varchar(18) REFERENCES "User"(Id), "LastModifiedDate" datetime,
"LastModifiedById" varchar(18) REFERENCES "User"(Id), "SystemModstamp"
datetime, "UserPermissionsMarketingUser" bit,
"UserPermissionsOfflineUser" bit, "UserPermissionsWirelessUser" bit,
"UserPermissionsSuperCssUser" bit, "UserPermissionsAvantgoUser" bit);


If you have access to a source database, then Enterprise Manager can script
tables and constraints separately, so you could generate one script to
create the tables, then a second to add the constraints with ALTER TABLE.

Alternatively, if all you have to go on is your script, you can use CREATE
SCHEMA (see Books Online):

create schema authorization dbo
create table UserRole (...)
create table [User] (...)

By the way, "User" is a reserved word in MSSQL, so it isn't a good choice
for a table name, but I assume that if it's a third-party product, you can't
do much about that anyway.

Simon
Jul 23 '05 #2
(ad**********@marketsquaresolutions.com) writes:
I'm trying to create a local copy of a popular CRM database called
Salesforce.com. Many of the tables in the DB have FOREIGN KEY
references that I want to preserve, but I've run into a chicken and egg
problem. Table "A" has a reference to table "B," and table "B" has a
reference to table "A." So I can't CREATE one until the other exists.
Is there a way to disable these checks until I've created all the
schema?


Just do:

CREATE TABLE this_one
CREATE TABLE that_one
...
ALTER TABLE this_one ADD CONSTRAINT fk_this_that FOREIGN KEY ...
ALTER TABLE that_one ADD CONSTRAINT fk_that_this FOREIGN KEY ...
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Rae | last post by:
OS XP Pro SP2 MYSQL Ver: 5.0.0-alpha nt Whenever I view a table or run a query in the Control Center (0.9.4-beta) I get the following error: ERROR 1146: Table 'shipnet.1' doesn't exist ...
3
by: CLarkou | last post by:
On a client's machine with Office 97, my access program gives an error "The Expression you entered refers to an object that is closed or doesn't exist" when I am assigning a value in the checkbox....
4
by: Regnab | last post by:
I'm automating the import of all text files into Access from a source folder. I was getting the "Field 'F1' doesn't exist in destination table" error, so I checked it out on the net and it said to...
4
by: Mike | last post by:
Hi I've written my first asp.net page below. It queries an Access database with one table consisting of two columns - a username and a server name. The users enter their login name and the page...
4
by: Phil Galey | last post by:
I created an About box and am able to get all the assembly information from the program to show up in the About box except the Version. I created the About box as a separate Windows application,...
1
by: H5N1 | last post by:
hi there the topic says it all. I have a outer join select statement in tableadapter that populates GridView, I want to make it updatetable, so I need to provide an update command for table...
1
by: LanaR | last post by:
Hi, I need to create sql script that creates a table if it doesn't exist. I tried to use if not exists (select * from sysibm.systables where name ='mytable' then create table mytable ......
17
abdoelmasry
by: abdoelmasry | last post by:
Hi Men i have real problem using mysql database i make database for my site with mysql it have very important data but i think the database tables corrupted
2
by: kilo | last post by:
Hey.. I need someone hwo can help me making my sql table.. I have no php skills. I have payed for a php program that shoud make dictation for people that have some problems reading danish.. with...
3
by: moltendorf | last post by:
I copied the files from my "test" database on my old server (MySQL was not running) to my new server ("./mysql/data/test" folder), and after starting the server, SHOW TABLES; shows all of the tables...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.