473,774 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help writing sql to check for records that invalidate a foreign key

57 New Member
Hi I am trying to write some sql to because we have a missing foregin key constraint. The following is what I have been asked to do:

1. COMPULSORY_WORK .reference to LISTED_BUILDING .reference

2. The constraint name should be COMPULSORY_WORK _FK

3. Provide data check SQL for records which will invalidate the foreign key

I have created the constraint with the code:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE COMPULSORY_WORK ADD (
  2.   CONSTRAINT COMPULSORY_WORK_FK FOREIGN KEY (REFERENCE)
  3.     REFERENCES LISTED_BUILDING (REFERENCE));
but before this I am meant to "Provide data check SQL for records which invalidate the foreign key." This is the part I don't know how to do as I am a beginner at SQL.

Below is an example of a completely different requirement and I am assuming I should be editing the code in the select statement, I have no idea what to type though..

Expand|Select|Wrap|Line Numbers
  1. SET SERVEROUT ON
  2. DECLARE
  3.   CURSOR GET_PARAMETER IS
  4.     SELECT UPPER(PARAMETER_NAME) PARAMETER_NAME
  5.     FROM STANDARD_PARAMETER
  6.     GROUP BY UPPER(PARAMETER_NAME) HAVING COUNT(*) > 1; 
  7.  
  8. BEGIN
  9.   FOR REC IN GET_PARAMETER LOOP
  10.     DBMS_OUTPUT.PUT_LINE('UNABLE TO CREATE AN UPPER KEY CONSTRAINT ON THE STANDARD_PARAMETER TABLE BECAUSE THE PARAMETER '||  REC.PARAMETER_NAME||' IS DUPLICATED');
  11.  
  12.   END LOOP;
  13.  
  14. END;
Nov 21 '08 #1
3 2427
Pilgrim333
127 New Member
Hi,

Do you need to write your own procedure to validate before you implement the foreign key? Or is a query that retrieves all the invalid data enough?

Pilgrim.
Nov 21 '08 #2
Pilgrim333
127 New Member
If you are needed to check the contraint before, then the code below should be what you need

Expand|Select|Wrap|Line Numbers
  1. SET SERVEROUT ON 
  2. DECLARE 
  3.   CURSOR GET_INVALID_DATA IS 
  4.     SELECT DISTINCT A.FK
  5.     FROM   TABLE_WITH_FK A
  6.     WHERE  NOT EXISTS
  7.                  (SELECT 1
  8.                   FROM   TABLE_WITH_FK_AS_ID B
  9.                   WHERE  B.ID = A.FK
  10.                   AND    ROWNUM = 1
  11.                  )
  12.    ;  
  13.  
  14. BEGIN 
  15.   FOR REC IN GET_INVALID_DATA LOOP 
  16.     DBMS_OUTPUT.PUT_LINE('UNABLE TO CREATE A FOREIGN KEY CONSTRAINT ON THE TABLE_WITH_FK TABLE BECAUSE THE FOREIGN KEY '||  REC.FK ||' DOES NOT EXIST IN TABLE_WITH_FK_AS_ID'); 
  17.   END LOOP; 
  18.  
  19. END; 
  20.  
It should be something like this, please try and tell your results.

Pilgrim.
Nov 21 '08 #3
moorcroft
57 New Member
Hi thanks, this is exactly what I was after
Nov 24 '08 #4

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

Similar topics

9
3928
by: collincm | last post by:
Hi, I am trying to optimize a table for inserts. Half of the timeron cost is in the FK lookup! These tables for example CREATE TABLE FOO2( FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK PRIMARY KEY, DATA VARCHAR(200) NOT NULL, LASTTIME TIMESTAMP NOT NULL);
9
10666
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the table from which I want to delete has two foreign keys that references two other tables and it is also referenced by another table. But this shouldn't be a problem, since I set the commit mode to none (or *none) at all places where this makes...
19
4108
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
10
2889
by: Tom | last post by:
I am looking for some ideas for how to design the layout of the form for data entry and to display the data for the following situation: There are many sales associates. A sales associate can work for multiple companies and work for multiple divisions within each company. Within each division he can work in multiple departments and within each department he can work with multiple groups. In each group he works on multiple projects. All the...
0
1351
by: Miguel Dias Moura | last post by:
Hello, I am working on an Asp.Net 2.0 / SQL 2005 web site. I am using profile to save the users info on the database. For example, I have the following structure: Public Structure Name Public First As String Public Last As String End Structure
11
4498
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any AutoNumber* fields in them. Correct me if I'm wrong, but I'm assuming this means that I cannot now alter these existing Access tables and change their primary key to an "AutoNumber" type. If I'm right about this, I need some suggestions as to the...
43
2626
by: SLH | last post by:
hi people. im trying to validate input received via a text area on an ASP page before writing it to a database. i cant use client side javascript due to policy, so it all has to happen on the server. here is what i was trying, but pieces of it continue to break for one reason or another. the thinking behind this function was like this: if the input is less than 10 characters long, fail. if its 10 characters or greater, but it doesnt...
5
2351
by: Will | last post by:
- I know enough ASP and Access to be dangerous :) - I need to put up a data base on our web server with 3 related tables. - They will be accessed by a limited number of people. - Each user will have to "log on" with a username and password and only have access to their records. - Their will be a main table with two associated tables... - This is not the application but it is simmilar in structure... Example:
2
4947
kcdoell
by: kcdoell | last post by:
Hello: I am trying to create a union query but do not have a lot of experience. Basically I have the below tables: The Tables: Table Name = tblPrior CreditRegIDFK; Number; Foreign Key for the Credit Region ID
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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
10039
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
9914
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
6717
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
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
3
2852
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.