473,401 Members | 2,139 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,401 software developers and data experts.

CASE in FK

i have the next problem

in the database we use, some of the data was imported from excel and excel made all numbers starting with a 0 lose that 0.

i have to find a way to link 20009 to 020009.

but the database has a rather poor design, so other records that still work can be

49 = 49
020010 = 020010
20011 = 20011

is there a way to, if a foreign key is not equal, switch to a diffrent one (i tried with cases and unions, but nothing worked), or would updating these lines be the best solution (theres about 50K lines in a database, and 5 databases, so updating would be alot of work)

also, this database will stop being used in march 2007, so it would be best if i could get it in a query now.
Dec 5 '06 #1
5 1238
almaz
168 Expert 100+
i have the next problem

in the database we use, some of the data was imported from excel and excel made all numbers starting with a 0 lose that 0.

i have to find a way to link 20009 to 020009.

but the database has a rather poor design, so other records that still work can be

49 = 49
020010 = 020010
20011 = 20011

is there a way to, if a foreign key is not equal, switch to a diffrent one (i tried with cases and unions, but nothing worked), or would updating these lines be the best solution (theres about 50K lines in a database, and 5 databases, so updating would be alot of work)

also, this database will stop being used in march 2007, so it would be best if i could get it in a query now.
It looks like your DB has a VERY BAD design.
Make sure that all your numeric fields are NUMERIC indeed, meaning that field that has a '020009' value has an INT datatype, not varchar/char/etc. When you do that you'll find that 020009 EQUALS TO 20009. Strange. isn't it? :). And all foreign keys would automagically work as expected.
Dec 5 '06 #2
It looks like your DB has a VERY BAD design.
Make sure that all your numeric fields are NUMERIC indeed, meaning that field that has a '020009' value has an INT datatype, not varchar/char/etc. When you do that you'll find that 020009 EQUALS TO 20009. Strange. isn't it? :). And all foreign keys would automagically work as expected.
that would eb a solution, if it werent for some records to be having L25482 as id

and i think L25482 won't fit in a int datatype :S
Dec 5 '06 #3
almaz
168 Expert 100+
that would eb a solution, if it werent for some records to be having L25482 as id

and i think L25482 won't fit in a int datatype :S
And that means that you do have a bad DB design :\. In this case, if you cannot get rid of varchar data type as a primary key, you'll have to manually parse the column revealing "missing" primary keys, something like this one:

Expand|Select|Wrap|Line Numbers
  1. -- See if you have missing foreign key references.
  2. select parent_id from child_table
  3. where parent_id not in (select id from parent_table)
  4.  
  5. --Try to add zeros (only if it solves the issue):
  6.  
  7. update child_table
  8. set parent_id = '0' + parent_id
  9. where parent_id not in (select id from parent_table)
  10. and ('0' + parent_id) in (select id from parent_table)
  11.  
But still the best choice would be to review the DB design
Dec 5 '06 #4
iburyak
1,017 Expert 512MB
Do you have one to one relationship in those tables or one to many?

If one to one I think it is possilbe to write a query.
Dec 5 '06 #5
And that means that you do have a bad DB design :\. In this case, if you cannot get rid of varchar data type as a primary key, you'll have to manually parse the column revealing "missing" primary keys, something like this one:

Expand|Select|Wrap|Line Numbers
  1. -- See if you have missing foreign key references.
  2. select parent_id from child_table
  3. where parent_id not in (select id from parent_table)
  4.  
  5. --Try to add zeros (only if it solves the issue):
  6.  
  7. update child_table
  8. set parent_id = '0' + parent_id
  9. where parent_id not in (select id from parent_table)
  10. and ('0' + parent_id) in (select id from parent_table)
  11.  
But still the best choice would be to review the DB design
the not in allowed me to find out that one of the two references had only a few broken links. this solved the issue

the database is getting renewed, but we need to get data out of this one first, before going to the new one :)

thank you for the help
Dec 7 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: Elliot Temple | last post by:
Hi I have two questions. Could someone explain to me why Python is case sensitive? I find that annoying. Also, why aren't there multiline comments? Would adding them cause a problem of some...
17
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
19
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
1
by: ST | last post by:
Hi, I'm trying to debug someone else's code, and I'm going thru this Select Case statement. I'm having problems with the "OTHER" case...in that when the first line of the case is false, it jumps...
2
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
7
by: Lauren Quantrell | last post by:
Is there any speed/resource advantage/disadvantage in using Select Case x Case 1 Case 2 etc. many more cases... End Select VS.
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
24
by: clockworx05 | last post by:
Hey guys i have this program that i need to write for class. here are the instructions: Write a function called foo that asks the user for their age. Pass the age value to a function called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.