Originally posted by Ed Carrington:
I would like to make sure there are no duplicate data entries in my JSP that populates an Oracle database with a table called MainTable. MainTable has an Id field that is the primary key, ValData with a varchar data type, Fid and Fid2 are number data types. Fid and Fid2 are foreign key values that are taken from another table.
Sometimes someone can enter duplicate data and the ValData, Fid and Fid2 will end up like this:
Is there anything in Java I can implement to prevent duplicate data entry in the above example?
I'm assumming ID is a database sequence that is auto-incremented... in your app
you should try to do something likey this (sorry this is the PL/SQL version... but I'm a long way from Java/JDBC profecient):
BEGIN
Select 1
FROM MainTable
WHERE ValData = myValData
AND Fid = myFid
AND Fid2 = myFid2
Exception
When no_data_found Then
INSERT INTO MainTable (ValData, Fid, Fid2)
VALUES (myValData, myFid, MyFid2);
END
My idea differs than Jeanne's because I come from a database-centric background. This doesn't make my suggestion better... it's just a suggestion. Without knowing more about how the data in the table was used... I would just handle it outside of adding more RI to my database.
[ October 21, 2007: Message edited by: Paul Campbell ]