How to recognize what constraint cause an exception in an insertion in Sqlite - validation

How could I know what type of constraint exception is raised in an insertion in Sqlite. What it caused?. For example if I design a table with an unique constraint and primary key constraint, and I insert a row that violate a constraint, sqlite will raise an SqliteConstraintException, but I would like to know which of both constraints was violated. That would be helpful for creating an error message for the user. Instead of writing functions that validate the row before inserting it. Thank you for your help.

That information is not provided as a field on the Exception. You can use the getMessage() method to get some information about the cause. Like UNIQUE constraint failed: event.id as an example. This is telling you the name and the column which violated the constraint, but not the name of the constraint which is violated.

Related

How to retrieve the row that caused exception using spring framework's namedParameterJdbcTemplate

I am inserting user information where a few columns have unique constraint on them one of which is email id. In testing when I try to insert the same user twice I get the
org.springframework.dao.DuplicateKeyException
it also does tell me which column violated the unique constraint but it does not give me back the row that triggered the violation. I have seen solution where the user has caught such exception and run a query based on column on which violation was triggered but does spring have anything that will return the row details along with the exception.
I tried looking but there is so much information explaining why the exception happened that I do not find a solution that returns the row that caused the violation.

Codeigniter ORA-00001: unique constraint

This error: Message: oci_execute (): ORA-00001: unique constraint (SCHEMA_NAME.NAME CONSTRAINT) violated
I wonder if I do not how to handle the error more simply, more generic.
Because otherwise I will have to work on each function of the models to check the data before adding, for no duplications and not give the error noted above.
Does anyone know a simple way to solve this?
Thanks.
There is a hint that can be specified that will allow the statement to succeed without inserting the duplicated data. It can be useful for replication or bulk data loading where the job may attempt to insert the same data multiple times. I wouldn't recommend it as part of a user application.
"The IGNORE_ROW_ON_DUPKEY_INDEX hint applies only to single-table INSERT operations. It is not supported for UPDATE, DELETE, MERGE, or multitable insert operations. IGNORE_ROW_ON_DUPKEY_INDEX causes the statement to ignore a unique key violation for a specified set of columns or for a specified index. When a unique key violation is encountered, a row-level rollback occurs and execution resumes with the next input row."

Create constraint in alter table without checking existing data

I'm trying to create a constraint on the OE.PRODUCT_INFORMATION table which is delivered with Oracle 11g R2.
The constraint should make the PRODUCT_NAME unique.
I've tried it with the following statement:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME);
The problem is, that in the OE.PRODUCT_INFORMATION there are already product names which currently exist more than twice.
Executing the code above throws the following error:
an alter table validating constraint failed because the table has
duplicate key values.
Is there a possibility that a new created constraint won't be used on existing table data?
I've already tried the DISABLED keyword. But when I enable the constraint then I receive the same error message.
You can certainly create a constraint which will validate any newly inserted or updated records, but which will not be validated against old existing data, using the NOVALIDATE keyword, e.g.:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
NOVALIDATE;
If there is no index on the column, this command will create a non-unique index on the column.
If you are looking to enforce some sort of uniqueness for all future entries whilst keeping your current duplicates you cannot use a UNIQUE constraint.
You could use a trigger on the table to check the value to be inserted against the current table values and if it already exists, prevent the insert.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
or you could just remove the duplicate values and then enfoce your UNIQUE constraint.
EDIT: After Jonearles and Jeffrey Kemp's comments, I'll add that you can actually enable a unique constraint on a table with duplicate values present using the NOVALIDATE clause but you'd not be able to have a unique index on that constrained column.
See Tom Kyte's explanation here.
However, I would still worry about how obvious the intent was to future people who have to support the database. From a support perspective, it'd be more obvious to either remove the duplicates or use the trigger to make your intent clear.
YMMV
You can use deferrable .
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
deferrable initially deferred NOVALIDATE;

Oracle unique constraint error message

I am maintaining a legacy application and I recently got contacted that people are getting an error message when they try to fill one of our oracle tables. Now, those oracle tables are not in our care, but I still want to try out something to help find the problem.
Anyway, the error message is the following:
java.sql.SQLException: ORA-00001: unique constraint (REO0.PK_TableName) violated :
I know I can find a lot of information online through google and here about this error message. That is not what my question is about.
The question is: the tablename shown here (which I put in bold), is that
the name of the table, or is the
PK_ part added to represent 'primary key' ?
Reason why I ask is: I can't directly get to this database, but somehow I can see all tables in REO0 and I can find one with TableName but not one with *PK_TableName* as the name for a table. So if this PK_ would refer to something like 'primary key' (which the constraint of is violated) then it would make a bit more sense.
PK_tablename is the name of the constraint, and as Alex Poole states in a good comment, it has been specified in the DDL (CREATE TABLE ... (columns, CONSTRAINT PK_tablename PRIMARY KEY(columns...) ), or ALTER TABLE ... ADD CONSTRAINT PK_tablename PRIMARY KEY(columns...) or CREATE UNIQUE INDEX PK_tablename ON ... (columns) for example). When no name has been given, Oracle generates a name which begins with SYS.
Note that usually PK_x suggests a primary key for table x, but your constraint might also be a foreign key constraint or a not null constraint for example.
The following query will tell you all:
SELECT * FROM all_constraints WHERE constraint_name = 'PK_TABLENAME'

org.springframework.dao.DataIntegrityViolationException

while adding some data to table from jsp, only one row of data gets inserted
Also an org.springframework.dao.DataIntegrityViolationException is thrown.
Can you say why this exception is raised?
The exception is in most cases about database constraint violation. - In your case I would guess it is some unique constraint - but this is only guessing.

Resources