Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 10-11-2008, 04:25 PM   #1 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,975
Default Foreign key error, in my JSP Project


Hi friends,
I am developing an Web Application using JSP/Servlet and MySQL as backend.. I have a main table having 17fields. Out of 17, 4 were foreign key..(I hope u all know foreign key...A field linked from other tables primary key)...
In the first JSP form, user only wants to enter three forein key fields along with other datas and the fourth one left aside...
But it show me an error saying that foreign_key field cannot be "null".

I try to fix this by storing the string "NULL" in a variable(Like String temp="NULL") and in PreparedStatement, i give like this
ps.setString(1,temp);
instead of the previous one like
ps.setNull(1,21);//Here 21 is the equivalent for String

But now it shows a weird error like this

java.sql.SQLException: Duplicate key or integrity constraint violation, message
from server: "Cannot add or update a child row: a foreign key constraint fails
(`raj1/tbl_rmadetails`, CONSTRAINT `fk_raj1tbl_vendortbl` FOREIGN KEY (`VENDOR_I
D`) REFERENCES `tbl_vendor` (`vendor_id`) ON DELETE NO ACTION ON UPDATE NO ACTIO
N)"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:1977)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:11 63)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java: 2236)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java
:1741)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java
:1588 )
at com.rajk.javacode.servlets.RMAEntryModel.addEntry( RMAEntryModel.java:
266)
at com.rajk.javacode.servlets.RMAEntryServlet.doPost( RMAEntryServlet.jav
a:51)

Now dudes, please tell me whats the possible cause for the problem....

Yeah, i knew that foreign key shouldnt left null, but my client doesnt want to enter that particular foreign key field....

And FYI i cant change that foreign key field to normal one as it collapses the project..

Help me friends....
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is online now  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 10-11-2008, 05:18 PM   #2 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Foreign key error, in my JSP Project

IMHO, the flaw lies in you DB design and not at UI level. I guess you have not normalized your tables properly. If you give the exact entities involved in the relationships, I can help better.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 10-11-2008, 05:23 PM   #3 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,975
Default Re: Foreign key error, in my JSP Project

^^
May i know, what i have to do inorder to check where the flaw lies in DB....
And i am not a expert in DB, still a n00b...
I want u to explain a bit( or well if u can ) abt normalization, which will helps me fix this error....
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is online now  
Old 10-11-2008, 05:41 PM   #4 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Foreign key error, in my JSP Project

Normalization is the core of DB design. For more info read here http://en.wikipedia.org/wiki/Database_normalization

I'm no expert too. Understanding and practicing normalization is a skill which can be perfected only by experience. Coz strict adherence to normalization can sometimes hit performance badly.

For a quick fix, I can help you out, if you can post the DB fields in the child table you are talking of and the tables to which the 4 FKs refer to.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 10-11-2008, 05:46 PM   #5 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,975
Default Re: Foreign key error, in my JSP Project

Quote:
mysql> desc tbl_rmadetails;
+---------------------+-------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+------------+----------------+
| DATE | date | YES | | 0000-00-00 | |
| RMA_NO | int(11) | NO | PRI | NULL | auto_increment |
| CUSTOMER_ID | varchar(45) | NO | MUL | | |
| ITEM_ID | varchar(45) | NO | MUL | | |
| CUSTOMER_INV_NO | varchar(20) | YES | | NULL | |
| CUSTOMER_INV_DATE | date | YES | | NULL | |
| CUSTOMER_INV_AMOUNT | int(11) | YES | | NULL | |
| SERIAL_NO | varchar(20) | YES | | NULL | |
| FAULT_DESC | varchar(45) | YES | | NULL | |
| REPLACED_SERIAL_NO | varchar(20) | YES | | NULL | |
| STATUS | varchar(45) | NO | | | |
| VENDOR_ID | varchar(45) | NO | MUL | NULL | |
| VENDOR_INV_NO | varchar(20) | YES | | NULL | |
| VENDOR_INV_DATE | date | YES | | NULL | |
| VENDOR_RMA_REF | varchar(45) | YES | | NULL | |
| REMARKS | varchar(45) | YES | | NULL | |
| DELI_DATE | date | YES | | NULL | |
+---------------------+-------------+------+-----+------------+----------------+
17 rows in set (0.01 sec)
mysql> desc tbl_vendor;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| vendor_id | varchar(45) | NO | PRI | | |
| vendor_name | varchar(45) | NO | | | |
| vendor_password | varchar(45) | NO | | | |
| vendor_address | varchar(200) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
Oops, sorry for this...the main table "tbl_rmadetails" just have 3 FK fields and one PK..
Sorry for that..Anyway the problem is i dont want to enter the VENDOR_ID FK field..

Help me fix this error...
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is online now  
Old 10-11-2008, 06:00 PM   #6 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Foreign key error, in my JSP Project

I was wondering why it was not allowing null in the first place. Coz you can create a row in child table without a parent in which case the foreign key field will be null.

In your case you have made the vendor_id column NOT NULL. This DDL statement should fix it for you.
Code:
alter table tbl_rmadetails modify VENDOR_ID varchar(45);
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 10-11-2008, 06:13 PM   #7 (permalink)
Excessive happiness
 
furious_gamer's Avatar
 
Join Date: Jun 2008
Location: Bangalore
Posts: 2,975
Default Re: Foreign key error, in my JSP Project

It works man....
Thanks for that dude....

(How i forgot to look at that... this reminds me that "damn, u still a n00b in DB!!!
__________________
My First Android phone : Samsung Galaxy SL i9003 - Rooted & Gingerbread XXKPQ
Updated : superteekz_V2 ROM for XXKPQ.

PS Request
furious_gamer is online now  
Old 10-11-2008, 07:12 PM   #8 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Foreign key error, in my JSP Project

DB design is a subtle art very few have really mastered. I'm as much of a n00b as you are.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
India May Ban Facebook, Orkut and Foreign Social Networking Sites iMav Technology News 18 03-04-2008 12:41 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 02:58 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2