473,769 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__48CFD27 E' 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), "ParentRole Id" varchar(18) REFERENCES
"UserRole"( Id), "RollupDescript ion" varchar(80),
"OpportunityAcc essForAccountOw ner" varchar(40),
"CaseAccessForA ccountOwner" varchar(40), "LastModifiedDa te" datetime,
"LastModifiedBy Id" 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), "CompanyNam e" 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), "MobilePhon e" varchar(40), "Alias" varchar(8), "IsActive"
bit, "TimeZoneSidKey " varchar(40), "UserRoleId " varchar(18) REFERENCES
"UserRole"( Id), "LocaleSidK ey" varchar(40), "ReceivesInfoEm ails" bit,
"ReceivesAdminI nfoEmails" bit, "EmailEncodingK ey" varchar(40),
"ProfileId" varchar(18) REFERENCES "Profile"(I d), "LanguageLocale Key"
varchar(40), "EmployeeNumber " varchar(20), "WirelessEm ail" varchar(80),
"LastLoginD ate" datetime, "CreatedDat e" datetime, "CreatedByI d"
varchar(18) REFERENCES "User"(Id), "LastModifiedDa te" datetime,
"LastModifiedBy Id" varchar(18) REFERENCES "User"(Id), "SystemModstamp "
datetime, "UserPermission sMarketingUser" bit,
"UserPermission sOfflineUser" bit, "UserPermission sWirelessUser" bit,
"UserPermission sSuperCssUser" bit, "UserPermission sAvantgoUser" bit);

Jul 23 '05 #1
2 8398

<ad**********@m arketsquaresolu tions.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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__48CFD27 E' 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), "ParentRole Id" varchar(18) REFERENCES
"UserRole"( Id), "RollupDescript ion" varchar(80),
"OpportunityAcc essForAccountOw ner" varchar(40),
"CaseAccessForA ccountOwner" varchar(40), "LastModifiedDa te" datetime,
"LastModifiedBy Id" 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), "CompanyNam e" 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), "MobilePhon e" varchar(40), "Alias" varchar(8), "IsActive"
bit, "TimeZoneSidKey " varchar(40), "UserRoleId " varchar(18) REFERENCES
"UserRole"( Id), "LocaleSidK ey" varchar(40), "ReceivesInfoEm ails" bit,
"ReceivesAdminI nfoEmails" bit, "EmailEncodingK ey" varchar(40),
"ProfileId" varchar(18) REFERENCES "Profile"(I d), "LanguageLocale Key"
varchar(40), "EmployeeNumber " varchar(20), "WirelessEm ail" varchar(80),
"LastLoginD ate" datetime, "CreatedDat e" datetime, "CreatedByI d"
varchar(18) REFERENCES "User"(Id), "LastModifiedDa te" datetime,
"LastModifiedBy Id" varchar(18) REFERENCES "User"(Id), "SystemModstamp "
datetime, "UserPermission sMarketingUser" bit,
"UserPermission sOfflineUser" bit, "UserPermission sWirelessUser" bit,
"UserPermission sSuperCssUser" bit, "UserPermission sAvantgoUser" 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**********@m arketsquaresolu tions.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****@sommarsk og.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
8263
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 table shipnet.1 indeed does not exist but I can't understand why it's looking for it. Any ideas?
3
19558
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. chk_multiuser.Value = 0 What could be the cause of this error ?
4
8314
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 create a import specification. So I did this (InductionImport), and included it in my code as below: DoCmd.TransferText acImportDelim, InductionImport, "ImportedInductionLots", CurrentPath & strfile, 0 I created the import specs as per...
4
1234
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 queries the database and then directs them to the correct server. This works fine as long as their name actually exists in the database. If it doesn't the the page fails at the server = dataSet.Tables(0).Rows(0).Item(0).ToString()
4
3039
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, changed the Output to Class Library, and compile it as a DLL. I then reference it from my program. In the About Box form's Public Sub New routine, I placed the following code, where I'm getting all the assembly attributes from the calling...
1
5142
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 adapter. the problem is that not all rows in gridview exist in database (since it's an outer join) so I want to UPDATE statement to insert the row if it doesn't exist.
1
3507
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
78091
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
6672
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 sound and text.. When I'm making the dictation with PHP I'm getting an error like this.: Query failed (SELECT word FROM repository): Table 'test_dig_dk.repository' doesn't exist the first one of the php, that I soud use for makeing the text is.. ...
3
10372
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 that are supposed to be there, but running a SELECT * FROM `configuration`; (for example) returns "test.configuration does not exist). I've tried the following repair statement since I'm not too familiar with backing up and restoring data. ...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.