DB2/400 : ALTER TABLE foreign key - db2

I am having a SQL problem on a DB2/400 database.
I would like to know if there is a particular method for adding a constraint on a foreign key of a table.
Right now, I'm using the statement below to declare a foreign key
in the table "TABELE_A" of the schema "MY_SCHEMA":
alter table MY_SCHEMA.TABELE_A
foreign key (IDAUTO_B) references MY_SCHEMA.TABELE_B (IDAUTO_B).
The "ALTER TABLE" statement runs without problems.
But as soon as I make an "update" in the table "MY_SCHEMA.TABELE_B ", I have a crash.
The SQL code error is SQL7008 and the reason code is 3 : "MY_SCHEMA.TABELE_B" not journaled, no rights on the journal or the state of the log is * STANDBY.
My question is : is it necessary logging tables to set up a referenciel integrity on SQL DB2/400 data-base ?

Related

Vendor-independent SQL syntax for dropping unique and primary key constraints

Precondition:
I am using Liquibase with SQL scripts in my app. I started testing with Oracle DB, but now I need to switch to PostgreSQL DB.
Problem:
When I added constraints, I didn't add the names of the constraints.
Liquibase changelog contains a script for dropping unique and primary key constraints:
alter table SCENARIO drop primary key/
alter table SCENARIO drop unique (OWNER_ID)/
This syntax doesn't sync with PostgreSQL
Could you give some advice, on how to resolve this problem?
Screenshots:

Awkward/wrong PostgreSQL foreign-key definition

As a database developer, I experienced this notice when I tried to make a data-only dump a PostgreSQL(10.1) database 'tlesson'.
Notice =>
pg_dump: NOTICE: there are circular foreign-key constraints on this table:
pg_dump: members
Dump command =>
$ pg_dump -U postgres -d translesson -a
A 'tlesson' table 'members' constraint =>
ALTER TABLE ONLY members
ADD CONSTRAINT friend_fk FOREIGN KEY (friend_id) REFERENCES members(member_id);
That is, 'friend_id' column refers own table's primary key as the foreign-key.
Should I drop the 'friend_fk' constraint to remove the notice I'm having?
If you always drop the entire database then this isn't a problem, because the generated SQL (or pg_restore) will enable (create) foreign keys only after all the data was loaded, so there is no problem in that case.
However if you only dump a single table without the FKs then, importing is only going to work if you manually drop the FK before restoring, then re-create it afterwards.
The reason is that it's nearly impossible to generate INSERT statements in the correct order if you have circular references

Postgres: Error constraint "fk" of relation "tbl" does not exist (with Yii - PHP)

I searched for this problem. But my postgres user has enough grant and I do not think I have misspelling error. However I am newbie.
I have this error message:
21:38:03 set search_path='public'
21:38:03 ALTER TABLE public.tbl_user DROP CONSTRAINT "fk-user-access-user-id"
21:38:03 ERROR: constraint "fk-user-access-user-id" of relation "tbl_user" does not exist
I use the PhpStorm. I just open the database view, expanded the tbl_user table, right click and select "drop". And I got this error in the console.
So the above SQL command generated by the PhpStorm.
Then I tried with these commands manually on Ubuntu:
ALTER TABLE tbl_user DROP CONSTRAINT "fk-user-access-user-id"
ALTER TABLE "tbl_user" DROP CONSTRAINT "fk-user-access-user-id"
But I get the same error.
In the PhpStorm I see this definition:
"fk-user-access-user-id" FOREIGN KEY (access_id) REFERENCES tbl_access (id)
The tbl_access table exists with the primary id key.
I do not understand this error message, because the "fk-user-access-user-id" foreign key is at the tbl_user and so for me the 'relation "tbl_user" does not exist' strange. I do not understand.
I tried to find similar problem on StackOverflow, but I gave up after 20x question reading.
By the way, the postgres code was generated by the Yii framework.
$this->addColumn('{{%user}}', 'access_id', $this->integer()->notNull()->defaultValue(1)->after('status'));
$this->addForeignKey('fk-user-access-user-id', '{{%user}}', 'access_id', '{{%access}}', 'id');
first row mean add access_id column to the user table.
second row: create foreign key with 'fk-user...' name on tbl_user table's access_id column references to tbl_access table's id column.
So I used this PHP code to generate this SQL commands. I prefer this way because for me the migration files are very useful.
Most likely the definition and actual implementation in your underlying DB has changed from what the app has recorded. Depending on what the history is, either a change in the app for that foreign key relationship was not migrated to persist the change at the database level, or someone has executed some operation directly at the DB level to remove the relationship. You will need to sync up the app layer to the DB at this point I would think.

ALTER TABLE Database NOCHECK CONSTRAINT FK_XXXXXX_DB

I need to make an update on a table (sql 2008 server).
I use sqlcmd and my update is on a file named update.sql and i use sqlcmd for run it.
In this update, i need to replace a value for an other.
Example:
use db
go
update db SET fab=9 where fab=10
Go
update db set fab=7 where fab=8
GO
update db set fab=6 where fab=17
GO
update db set fab=11 where fab=12
GO
And I have this message :
Message 2627, Level 14, State 1 sqlserveur Server, Line 1 Violation of PRIMARY KEY constraint PK_database can not insert duplicate key in object dbo.database. duplicate key value is <2.9 the statement has been terminated
Could you please tell me how I can use THE NOCHEK CONTRAINT, because I've try it but it's does work.
thank you very Much!!!!!
ALTER TABLE Test_Table
NOCHECK CONSTRAINT your_Constraint_Name
GO
This is one way of disabling your constraints but it only works for Foreign keys and check constraints.
Your Error Message says Violation of PRIMARY KEY constraint this cant be disabled you will need to drop the Primary Key.

PostgreSQL constraints - ON DELETE CASCADE not being restored

I have run into problems when restoring a PostgreSQL database schema in another server. More precisely, some of the tables don't seem to have the same foreign key constraints associated with them that they used to in the original database. For example, the ON DELETE CASCADE clause seems to have completely evaporated from all of the constraint definitions.
That's probably because the dumping procedure didn't backup the ON DELETE CASCADE clauses in your table definitions.
Firstly you should delete the foreign key constraints on your tables and then go on to altering them:
Something like the following:
ALTER TABLE ONLY *your_table* DROP CONSTRAINT your_constraint;
After that, recreate the constraints with something like:
ALTER TABLE ONLY your_table ADD CONSTRAINT your_constraint (...ON DELETE CASCADE, etc..);