Create table without checking foreign key on unexisting table - foreign-keys

I'm adapting a really big query (MySQL to SQL) that creates lots of tables and relationships. The problem is that it always check if a table exists prior to adding a foreign key referencing to that table.
So I have to reorder the queries to avoid this problem, and my question is if there's a instruction that can turn that check off, so it'll create the tables and add references without stopping the query with every error it encounters.
I'm working with SQL in an Azure DB.
Thank you.

Easiest way is to create all tables first and then add constraints using ALTER TABLE.
For example:
CREATE TABLE a(id INT PRIMARY KEY IDENTITY(1,1), b_id INT, c CHAR(10));
CREATE TABLE b(id INT PRIMARY KEY IDENTITY(1,1), z INT);
ALTER TABLE a ADD CONSTRAINT FK_a_b_id_b FOREIGN KEY (b_id) REFERENCES b(id);
Rextester Demo

Related

Informatica Foreign Key column population

I have two tables, SRC and TGT. I want to populate all my foreign key values in TGT from the target lookups. However, I'm getting nulls into the target. I have used all natural keys for lookup conditions. Can anyone please explain why I am seeing nulls?
For instance, I want to populate foreign key values for MPNG_ID, SESN-ID, WRKFLW-ID from lookup tables based on repository name and version number.
Did you validate if all your Join Conditions are working fine? Probably try to replicate same query in DB and check if its giving you the same result. I assume some Join might have messed up which might result in Null values. Ensure that even the spaces are in sync if you are using Varchar value.

Unable to add duplicate entry through informatica

I am using Informatica to finally write in the oracle table after performing certain logical operations on the data.
The problem is that if a certain ID was already previously processed and is present in the target table then it is not inserted again.
Please suggest a workaround.
Hi, This is because you might have same primary key in the source which is available in the target. Look into primary key columns and try loading them.
Altering your target table
alter table target_table_name drop constraint constraint name;

ibm db2 multiple alter table statements

I am trying to run multiple alter table statements for adding foreign keys on my database
I am using RazorSQL
this are my sql statement
ALTER TABLE SPO999.AVTVRSTEPLACILAPOD
ADD CONSTRAINT SQL100419145030510 FOREIGN KEY
(AVP_VRSTEPLACILA)
REFERENCES SPO999.VRSTEPLACILA
(VP_ID_VP)
ON DELETE NO ACTION
--ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
-- DDL Statements for foreign keys on Table SPO999.AVTVRSTEPLACILAVRPL
ALTER TABLE SPO999.AVTVRSTEPLACILAVRPL
ADD CONSTRAINT SQL100419145030630 FOREIGN KEY
(AVV_VRSTEPLACILA)
REFERENCES SPO999.VRSTEPLACILA
(VP_ID_VP)
ON DELETE NO ACTION
--ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
If I run one at a time it works, while if trying to run both at I get an SQL Error -104 a token,character or clause is invalid or missing.
I can not find a problem/solution
any suggestions?
thank you
I think it is likely that you can't have these two foreign key definitions referencing the same column within a single transaction.
Try adding commit; between the statements.
(Your SQL editor, depending on its settings, may automatically send a commit after each chunk of statements that you execute. That would explain the difference between the two cases).

can i avoid creation of a foreign key constraint for a one-to-one relationship?

i want to use eclipselink to partition my database. for performance reasons i will have one table (entity A) replicated to all nodes and one table (entity B) that is hash partitioned over all nodes.
Since every A has a one-to-one relationship with a B entity eclipseLink creates a foreign key constraint on a column of the "A"-Table. because of the different partitioning mechanisms this contraint will fail for a lot of entries in A.
currently the properties of the entities can change dayly so i wouldn't want to miss ddl-generation for tests and development.
Is it possible to tell eclipse link not to create this specific foreign key? all foreign keys?
the current test database is an in memory hsqldb. is it possible to tell the db to ignore foreign key constraints?
You can use your own DDL script to create the tables, or just drop the constraint with your own script or native SQL query.
You can disable all constraints by subclassing your database platform class (and using "eclipselink.target-database" with your subclass).
If there is no foreign key there is no relationship.
Alternatively, you could mark the property b in class A as transient so it does not get managed by JPA. It means that you will have to retrieve the appropiate b value yourself.
Also, you could just try to make field b nullable (if JPA supports null=true for a One-to-One relationship, I am not sure) and check what happens.

Django: Many-to-many through a table with (only) compound key

I have a legacy database with a table storing a many-to-many relationship, but without a single primary key column. Is there any way to convince Django to use it anyway?
Schematically:
Product 1<---->* Labeling *<---->1 Label
The Labeling table uses (product_id,label_id) as a compound primary key, and I don't see any way to inform Django about this. (Just using through gives me Unknown column 'labeling.id' in 'field list'.)
Do I need to fall back to custom SQL? Or am I missing something?
hope this helps you,
http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.db_index
If you add a unique_together to the model for the many-to-many table, Django will use those columns instead of expecting a primary key called id.