Import Oracle Dump file and NOVALIDATE for FKs - oracle

Situation:
I'm exporting data from the Oracle DB A, and importing it into an empty Oracle DB B.
When exporting from A, I deliberately export partially the data in different tables. That results into a dump file that for sure does not guarantee FK integrity due to the ignored data. Then I want to import it into the DB B it fails when creating the FKs.
Goal:
I want to have the same effect as NOVALIDATE has for FK Constraints but at import time. That is: for old data, it should not validate, and just check for new data. I didn't find a way to do it on the export, or import. Does any one know how can I do it?
Extra information:
I can't change the FK on the DB A before dumping it.
The data is so intertwined with old and new data, that there is no way to create the DB B while maintaining the consistency of FKs.
I need to create the FKs, because the new data will rely on them

Related

How can I export/import subset data from/to Oracle Database?

I was wondering what would be the approach to get rid of a lot of records from an Oracle database in order to create a lighter database for developer's laptops.
We aim to reduce the exports from different production environments NOT excluding entities, but reducing the number of records in each table mantaining the referential integrity.
Is there a tool/script around?
I was also wondering if transforming all the FKs on a replica DB to "on delete cascade" and deleting a subset of record from the entities on the top of the relational hierarchy would do the job.
Any suggestion?
With Jailer you can export data to an SQL script which can traverse foreign key constraints to include all data needed to maintain referential integrity.
http://jailer.sourceforge.net
If you wanted to export/import limit object from/to database, then you could EXCLUDE the objects, which you don't wanted to be part of your dump.
You can exclude any specific table to be exported/imported by specify the object type and object name.
EXCLUDE=TABLE:"='<TABLE_NAME>'"
==Update==
AFAIK, I don't see, if Oracle provides such flexibility to export subset data, but Oracle does have option to export partitioned data from TABLES
TABLES=[schema_name.]table_name[:partition_name] [, ...]

How do I export a table from 1 database to another database in Oracle (sql plus)

If I have 1 table in a database, and I want to export it, then import it into new table in a different database?
Should I set up the table with same fields in database two, or is there a way create empty table so all the import will work?
If you have a dblink established, a quick way to copy a table without intermediate files would be to execute this from the target database (the one where you want the new table to be copied):
create table my_new_table as
select *
from my_original_table#my_original_database
This presupposes the dblink, of course, and also that there is sufficient redo space to allow that much data to be copied in one fell swoop.
If not, you could also build the table this way and then do a bunch of insert into transactions to move the data in chunks.
If you only want the structure (your question sort of implied that, but I wasn't sure), you can always add a where 1 = 3 to copy only the structure.
This won't import constrains or indexes, but I'm not sure if that matters for what you seek.

Oracle export tool does not create some tables and sequences

I use oracle 11, and use exp/imp tools to migrate data between databases.
It works very fine IF all empty tables and sequences are already created in target database.
But If tables dont exists in target DB than a few bad things happen;
It still creates tables but only the ones with data, I couldnt find a way to force it create empty tables in target DB.
It does not create the sequences.
This is how I enter my values to export tool;
Users or Tables -> Tables
Export table data -> yes
Compress -> yes
Table or Partition to be exported -> I enter table names here one by one,
But it does accept table names without data..It says table does not exist, so no surprize they are not imported later.
Import Data only > no
Import File > Full path to Dump file.
List contents of import file > no
Ignore create error > no
import grants > yes
import table data > yes
import entire export > yes
Sequences are not exported in table mode. The documentation lists the objects exported in each mode, and that shows that sequences are only exported in user and full database modes.
Export is deprecated in 11g, as the documentation also states:
Original Export is desupported for general use as of Oracle Database 11g. The only supported use of original Export in Oracle Database 11g is backward migration of XMLType data to Oracle Database 10g release 2 (10.2) or earlier. Therefore, Oracle recommends that you use the new Data Pump Export and Import utilities
The empty tables are not being exported if you have deferred segment creation. This AskTom articles refers to it, and it's also mentioned in the documentation:
The original Export utility does not export any table that was created
with deferred segment creation and has not had a segment created for
it.
You can either use dbms_metadata.get_ddl() to get the table creation statements for all the tables, or just the empty ones, and build them manually from that; or force an extent to be allocated (as mentioned in the docs too); or use the supported and current data pump export and import. Based on previous questions you should only be using exp/imp if your customer refuses to handle data pump files, and I can't really think of a good justification for that.

Import specific tables from oracle dump file?

I have a dump of a huge oracle database so it is impossible to import it all. I want to import a specific table called X. The problem is that X has foreign keys. If I import just X, I will get the following error:
imp user/pass#dbName tables=X rows=y ignore=Y
ORA-02291: integrity constraint violated - parent key not found
I already have the whole db locally (but without data), I want to import all tables that are associated to X. How can I achieve that? I have plsql installed. I also need to know the order of these tables to know which to import at first.
You dan disable all DB constraints before the import, and re-enable them afterwards. See:
disable-all-table-constraints-in-oracle or
oracle_disable_constraints
There are few questions so I will try to answer one by one.
ORA-02291: integrity constraint violated - parent key not found
No brainer to guess this because as you know you don't have parent record for table X. By the way you may also want to use flag CONSTRAINTS=N because you already have db as you said.
"I want to import all tables that are associated to X. How can I achieve that?"
Well no option but to find all the dependencies manually (or use data dictionary tables user_cons_columns, user_constraints etc to lookup) and import those tables as well. Think of it if you don't do that. You will break your data integrity. If you still want that data in table X without dependencies then disable the constraints and then import. But you won't be enable your constraint again and I don't know what you want to do with broken data.
"I also need to know the order of these tables to know which to import at first."
Disable the constraints before import and then enable them after import. You don't have to worry about order in that case.

Import data from a single file into multiple Oracle tables

Is there a method to import data from a single file into multiple Oracle tables while maintaining the referential integrity?
Yes.
Without a lot more detail, I'll just say that you should look to external table to get the data from the file into the database, then select from the external table and use the 'INSERT ALL' feature to insert into multiple tables, from the single input.
Hope that helps.
There are couple alternatives (not an exhaustive list):
Walk the dependency graph of FOREIGN KEYs and make sure you insert data to "parents" before inserting it to "children".
Defer all the FOREIGN KEYs, so order of insertion does not matter. This is OK if you can perform the whole import in a single transaction.
Temporarily disable FOREIGN KEY constraints, import the data in any order, then re-enable them.

Resources