Click here to Skip to main content
15,905,781 members
Articles / Programming Languages / SQL
Article

Generic way to do primarykey & foreign key validation from frontend

Rate me:
Please Sign up or sign in to vote.
1.25/5 (8 votes)
5 Aug 20052 min read 51.7K   19   5
Generic way to do primarykey & foreign key validation from frontend

Following is the way to do the primarykey & foreign key validation before the data is actually inserted or updated in tables.

following are the steps

Assumption is that at oracle side the table is having a proper defination of primary key and foreign key.

1)

create global temporary table pkfk_chk_columns (

col_name varchar2(30),

col_label varchar2(40),

col_value varchar2(1000)

) on commit delete rows

2) compile the attached procedure

create or replace procedure PKFK_CHK(tablename VARCHAR2,

PKey_FKey VARCHAR2,

retval OUT VARCHAR2,

errmsg OUT VARCHAR2) is

-- PKey_FKey := 'PK' or 'FK'

From any frontend application before you submit the request to save the data in oracle tables, you need to insert records in table pkfk_chk_columns

col_name :- Name of the field (in UPPERCASE)

col_label :- Label of the field

col_value :- VALUE of the field

For Primary Key check

Insert records in pkfk_chk_columns related to primary key fields

Call the stored procedure PKFK_CHK by passing relevant values

- tablename on which the primary key or foreign key needs to be checked

- PK :- Primary Key check

- retval and errmsg will be error code and userfriendly error message which can be thrown to enduser from the frontend application.

For Foreign Key check

Insert records in pkfk_chk_columns related to foreign key fields

- tablename on which the primary key or foreign key needs to be checked

- FK :- Foreign Key check

- retval and errmsg will be error code and userfriendly error message which can be thrown to enduser from the frontend application OR Pass the retval and error message to application error handler routine.

DO not insert records into this table if data is NULL or blank for the given field.

SO NO MORE ORA-xxx errors related to primary key or foreign keys..

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Program Manager
India India
From RYK Science College & JDC Bytco Institute of Management Studies, Nasik. I am into Software Industry since 18 years. Manage small to large scale global deliveries. Working as Program Manager in Fundtech.

Comments and Discussions

 
GeneralNice trick ... but ... Pin
Scott Walter5-Aug-05 5:35
Scott Walter5-Aug-05 5:35 
GeneralRe: Nice trick ... but ... Pin
Keith Farmer5-Aug-05 16:16
Keith Farmer5-Aug-05 16:16 
What's he's trying to do isn't to trap an error. He's trying to verify the validity of the key before he submits the query.

Your solution deals with error trapping. Admittedly, I agree it's a better approach, provided that the exception is propagated back to the client. (Why aren't there subclasses to the SqlException types? The catch-all SqlException is just as bad as the catch-all Exception.)
GeneralRe: Nice trick ... but ... Pin
SudhirKankal9-Aug-05 6:23
SudhirKankal9-Aug-05 6:23 
GeneralRe: Nice trick ... but ... Pin
Keith Farmer9-Aug-05 6:27
Keith Farmer9-Aug-05 6:27 
GeneralRe: Nice trick ... but ... Pin
Scott Walter20-Aug-05 8:06
Scott Walter20-Aug-05 8:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.