How disable check constraint on connection - jackcess

java.sql.DriverManager.getConnection("jdbc:ucanaccess:///user/dev/project/project.mdb")
I have this error
Java::NetUcanaccessJdbc::UcanaccessSQLException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10991 table: EMPLOYE column: DATE
I doesn't want control integrity at the connection, what is the property parameter to disable check contrainte at the connection ?
Thank for your help

If this exception is thrown at the connection time, it means that the database data are inconsistent with its contraints: ucanaccess is properly building a dbms layer with all integrity contraints but the creation of one of those constraints fails.
You can't disable it, ucanaccess must create all constraints so that when you try to insert/update/delete a record, all related constraints are checked and your db consistent state is preserved.
Why don't you drop the not null constraint in the database table definition?(you have just to set the "required" property, on the mentioned column, from true to false). It doesn't seem to be taken into account...

Related

Is it possible to access current column data on conflict

I want to get such behaviour on inserting data (conflict on id):
if there is no model with same id in db do INSERT
if there is entry with same id in db and that entry is newer (updated_at field) do NOT UPDATE
if there is entry with same id in db and that entry is older (updated_at field) do UPDATE
I'm using Ecto for that and want to work on constraints, however I cannot find an option to do so in documentation. Pseudo code of constraint could look like:
CHECK: NULL(current.updated_at) or incoming.updated_at > current.updated_at
Is such behaviour possible in Postgres?
PostgreSQL does not support CHECK constraints that reference table
data other than the new or updated row being checked. While a CHECK
constraint that violates this rule may appear to work in simple tests,
it cannot guarantee that the database will not reach a state in which
the constraint condition is false (due to subsequent changes of the
other row(s) involved). This would cause a database dump and reload to
fail. The reload could fail even when the complete database state is
consistent with the constraint, due to rows not being loaded in an
order that will satisfy the constraint. If possible, use UNIQUE,
EXCLUDE, or FOREIGN KEY constraints to express cross-row and
cross-table restrictions.
If what you desire is a one-time check against other rows at row
insertion, rather than a continuously-maintained consistency
guarantee, a custom trigger can be used to implement that. (This
approach avoids the dump/reload problem because pg_dump does not
reinstall triggers until after reloading data, so that the check will
not be enforced during a dump/reload.)
That should be simple using the WHERE clause of ON CONFLICT ... DO UPDATE:
INSERT INTO mytable (id, entry) VALUES (42, '2021-05-29 12:00:00')
ON CONFLICT (id)
DO UPDATE SET entry = EXCLUDED.entry
WHERE mytable.entry < EXCLUDED.entry;

Is it safe to drop a table column constraint in postgres

I'm looking at a production table in postgres with the following constraint which due to third party collaboration we need to remove.
"customer_email_unique" UNIQUE CONSTRAINT, btree (customer_email)
This is a production table, what risks are there if I remove the constraint? If it causes problems can it be recreated after to an existing table, with existing data in it?
It looks like the command to drop the constraint is
ALTER TABLE your_table DROP CONSTRAINT customer_email_unique;
We're a React/ Node stack and I can see what the code is doing with regard to what will happen if the constraint is dropped, my lack of knowledge is more towards data and what happens if you drop a constraint.
Thanks,
This is a production table, what risks are there if I remove the constraint? If it causes problems can it be recreated after to an existing table, with existing data in it?
The risk is that you'll drop the constraint and non-unique entries will be inserted. You won't be able to reapply the unique constraint without deleting the non-unique rows or updating them to be non-unique. Another risk it that you'll drop the wrong constraint, or reapply the constraint incorrectly. Finally, there may be code which assumes that column is unique.
To mitigate this risk, write a script to drop the constraint ("up"), and one to restore uniqueness and reapply the constraint ("down"). Test it on an equivalent table on a non-production database.
This is the general idea of schema migrations. Every schema change is done by two scripts, an "up" script to apply the change and a "down" script to undo the change. Many ORMs, such as typeorm, support migrations. They make schemas reproducible so all environments know they have the same schemas, schemas can be tested, and in general mitigate the risk of schema changes.

How to rename a table in a database which have referential integrity maintain?

After giving the below command
set integrity for table_name off
I am getting following error
DB2 SQL error: SQLCODE: -290, SQLSTATE: 55039, SQLERRMC: null Message:
Table space access
What could be the possible reason for this?
What I want to achieve is: I want to temporarily disable the constraints so that I can rename the actual table and create a new table with the actual table name. Then i will enable the constraints. Any help of pointers in this regard will be appreciated.
set integrity does not "disable constraints".
As per the SET INTEGRITY statement manual page, set integrity off
Specifies that the tables are placed in set integrity pending state. Only very limited activity is allowed on a table that is in set integrity pending state.
WHen you try to rename a table with constraints you get SQL0750N, which says:
If the error pertains to a RENAME statement, drop the view, materialized query
table, trigger, SQL function, SQL method, check constraint, referential constraint,
expression-based index, or XSR object dependent on the table before issuing the
RENAME statement. Objects dependent on the table can be determined by querying the
catalog.
I.e. you have to drop the constraint, rename the table, then add the constraint back. It is just the way it works. Something like ADMIN_MOVE_TABLE will do the drop and re-create of constraints required, but then that until actually physically moves the table data, so is maybe not a good idea if you simply want to do a rename. Many GUI tolls (such as IBM Data Studio) will generate the required DROP, RENAME, CREATE statements for you.
The error you currently have SQL0290N Table space access is not allowed is a different issue, and will be due to some other cause.

How to disable foreign key constraints in postgresql

I'm using AWS Aurora Postgres and using DMS to migrate from RDS postgres to Aurora PG. In order to perform the FULL LOAD I want to disable foreign key constraints and triggers on all the objects. I'm able to disable triggers but couldn't find a way to disable constraints.
Below doesn't work:
ALTER TABLE so_items DISABLE CONSTRAINT so_items_so_id_fkey;
It throws:
ERROR: syntax error at or near "CONSTRAINT"
LINE 1: ALTER TABLE so_items DISABLE CONSTRAINT so_items_so_id_fkey;
^
SQL state: 42601
Character: 30
Setting "session_replication_role" = "replica" in the parameter group didn't work. While the DMS task tries to truncate the table part of preparation it still fails with foreign key violation errors.
Please advise any workarounds.
Note: I couldn't do below since in RDS I do not have permissions to do so even with master account:
alter table so_items disable trigger ALL;
ERROR: permission denied: "RI_ConstraintTrigger_c_16520" is a system trigger
SQL state: 42501
You shouldn't modify the triggers a Postgres constraint relies on. This is an implementation detail for which you shouldn't care about.
You cannot disable constraints, really.
To turn constraints temporarily off, you can defer the constraint check to the end of transactions:
ALTER TABLE so_items ALTER CONSTRAINT so_items_so_id_fkey DEFERRABLE INITIALLY DEFERRED;
With that modification the constraint is evaluated after a modification at the end of the current transaction. This will allow you to break the constraint inside of a transaction.
You may DROP CONSTRAINTs
ALTER TABLE so_items DROP CONSTRAINT so_items_so_id_fkey;
which will delete it permanently.
Edit: It is also possible to disable the triggers which also affects the foreign key constraints of the table
ALTER TABLE so_items DISABLE TRIGGER ALL;
But when you are re-enabling the triggers afterwards, the foreign keys are not checked. This might lead to invalid / inconsistent foreign keys in the database.
For Postgres :-
It is easier to disable all triggers with:
SET session_replication_role = 'replica';
And after migration reenable all with
SET session_replication_role = 'origin';

Getting ORA-00001(unique constraint violated) when COMMITing?

We're getting a ORA-00001 (unique constraint violated) in a batch job. However, the error occurs when a COMMIT is issued, not at the time the offending record is inserted.
Questions:
How come that the unique constraint is checked at COMMIT? (Are there some settings we can use so that the check occurs at the time of the INSERT?)
How can we find out the offending SQL/record that lead to the unique constraint violation?
Any help is appreciated!
Additional Information/Question:
The "offending" constraint is marked as IMMEDIATE and NON-DEFERRABLE. Can this be overridden in the transaction?
Constraints can be marked/defined as deferrable. In that case constraint checks can be either "immediate" or "deferred". When defining the constraint you can set a default/initial value, initially immediate or initially deferred. When set to deferred the constraint is enforced not until you commit the transaction.
You can change the behaviour of deferrable constraints e.g. via
set constraints all immediate;
see also: http://www.oracle.com/technology/oramag/oracle/03-nov/o63asktom.html
Constrains can be defined as deferred, meaning that they are checked at commit, not at the time of the data change. See the following 2 links:
http://www.oracle-base.com/articles/8i/ConstraintCheckingUpdates.php
http://www.oracle.com/technology/oramag/oracle/03-nov/o63asktom.html
hope it helps