views:

63

answers:

2

Also; can I add a negative INT inside a PRIMARY KEY column inside a table via JDBC?

+1  A: 

There should be no reason why you can't have a negative number in a primary key field, unless you are using an unsigned integer as its data type.

When you don't have a primary key defined (or a unique index for that matter), the database server has no way of knowing that the rows are unique. Being able to tell one row from another is pretty fundamental when it comes to databases, and I think maybe the designers of MySQL probably are forcing this on you -- I don't know, since I don't use MySQL all that much... It's a problem that I've never ran into, to be honest, because pretty much every table that I ever create has a PK!

Dave Markle
A: 

Why is it necessary to assign atleast one column in a table (in MySQL) as PRIMARY KEY for the table to accept UPDATE and INSERT statements via JDBC?

This is not a JDBC limitation. This is a database limitation. Consult DB-specific docs for answers.

Also; can I add a negative INT inside a PRIMARY KEY column inside a table via JDBC?

This is not a JDBC limitation. This is a database limitation. Consult DB-specific docs for answers.

To make things clear: JDBC is here just a simple tool to give you the ability to execute the SQL language using the Java language. If you get exceptions of java.sql package, then the problem more lies in the faulty SQL syntax used, or a limitation in the database in question, or in rare cases a bug in the DB-supplied JDBC driver, but it certainly isn't caused by the JDBC API in general as you seem to think.

BalusC
I absolutely know that JDBC is like a translator; but, when I tried using the `INSERT` statement in MySQL console, it successfully added the new row despite the table having no `PRIMARY KEY` column, but when doing the same from JDBC, it gave an Exception
Catfish
It is a JDBC limitation (or rather a MySQL JDBC driver limitation) that an UpdatableResultSet requires a primary key. When you update through a ResultSet, JDBC is more than just a simple tool to issue SQL.
Thilo
@Thilo: That's thus an implementation detail which still doesn't make it a JDBC API limitation. I still don't see any value of your downvote after a week. Have had a bad day?
BalusC
the reason for the down vote is that I kinda forgot about this thread. Have reverted it just now. Although your answer should have read like a solution rather than -- what it is currently -- more like reading out from a rule book... but then again, I question weren't that descriptive either.
Catfish
Catfish, you haven't downvoted it. Thilo did it.
BalusC