Best way of importing tables to PostGre from Oracle? - sql

I have done some queries in a read-only (Oracle/SQL developer) dB where I have absolutely no privileges to create temporary tables or anything else. I want to export the tables resulting of my queries into another db (PostGre/pgAdmin) db in order to be able to do other queries on the result.
Is there an easy way to create the columns of my exported tables in the PostGre db using pgAdmin or do I have to create all the columns manually ?

You could install the Oracle Foreign Data Wrapper in your PostgreSQL database and use IMPORT FOREIGN SCHEMA to create foreign tables for your Oracle tables.
Then you can use
CREATE TABLE local_table AS SELECT * FROM foreign_table;
to copy the data.

Related

Postgres database.table notation

Im trying to alter a table ALTER TABLE database.table...
What is the correct notation to access tables in a DB from the base postgres db without having to explicitly connect to that database?
There is no way. You have to connect to the right database.
A DATABASE within MySQL is comparable with a SCHEMA in PostgreSQL and many other brands.
MySQL: ALTER TABLE database.table...
Others: ALTER TABLE schema.table...
From the MySQL manual:
CREATE SCHEMA is a synonym for CREATE DATABASE.
This actually means that a single MySQL server has just a single database. This database can have many schema's and every schema can have many tables.
Within a single database you can jump from one schema to the other schema, no problem. This works for MySQL, PostgreSQL and many others. You can not jump from one database to another database without a new database connection because it's a different instance.
It is that MySQL uses a different name for a schema, it calls this a database. A little confusing.
If you want the same thing in other databases, like PostgreSQL, just use schema's within a single database.

Can I migrate a partitioned table to a non-partitioned table in Oracle with the CREATE TABLE statement?

I have an Oracle 11g partitioned table with 10 partitions for ten years of data, each on its own tablespace partitioned by range. Each year-partition contains 12 monthly-partitions.
I would like to convert this table to a non-partitioned table, before migrating all the database to Postgresql 10.7 with ora2pg.
I've read that I could first backup this table by expdp and then import it using PARTITIONS_OPTIONS parameter option of impdp.
But is it also possible to use this following statement as a strict equivalent ?
CREATE TABLE IF NOT EXISTS non_partitioned_table AS SELECT * FROM partitioned_table
I would not lose any data, but what about the indexes ?
Is there other differences between these two procedures ?
Syntax you posted doesn't exist in Oracle (there's no if not exists clause there).
Therefore, you'd
create table non_partitioned_table as select * from partitioned_table;
If object whose name is non_partitioned_table already exists, that command would fail.
No indexes would be created automatically - you'd have to create them manually, but - you'd do that in PostgreSQL anyway, wouldn't you? Why bother in Oracle as you won't be using that table for anything (except migration purposes); right?
You can use expdp to export the PARTITION table.
Then use impdp with the MERGE option to import it back into a non partition table. How you get the data into postgres is up to you.
expdp TABLES=scott.part_tab USERID="' / as sysdba'" DIRECTORY=test_dir DUMPFILE=part_tab.dmp LOGFILE=part_tab.log
impdp USERID="'/ as sysdba'" TABLES=scott.part_tab DIRECTORY=test_dir DUMPFILE=part_tab.dmp LOGFILE=imp_part_tab.log REMAP_SCHEMA=scott:testuser

Delete Multiple Table In single SQL Query?

I am using Room Database over SQlite and Live Data in my android application. I need to delete whole table data not database so I am going to delete table in separate Query like:
DELETE From Table_name
But I want to delete multiple tables data not drop in single Query not all tables. So please give me an appropriate solution.

oracle create table into new database with primary key and indexes

I have a table called TableA in DatabaseA and I want to create the same TableA in DatabaseB. I am able to do so but copying only the structure and the data, I seem unable to also create the primary keys and indexes. Is there an SQL statement I can use that copies the table structure, the table data, the primary keys and indexes please?
I am using Oracle 11G.
1. First Method
To get tables and indexes without data see following post
Stack Post
after creating table you can load data using
insert into dest_table as select * from source_table
2. Second Method
use expdp to take backup of source table using table=yourtable parameter as this will by default will take indexes and when you will import using impdp on destination database it will automatically rebuild those indexes.

Oracle sql developer - export DDL - only create table sql

I want to run unit tests by generating all tables in HSQLDB, present in my oracle database.
For that I want to export all DDL create table statements from oracle tables.
I tried export database, but along with create table sql I am getting lot other SQLs like,
" PARTITION BY RANGE ("CREATION_DATE") " etc.
How do I export all oracle tables(schema) to HSQLDB? is there any better way?
You can use the DBMS_METADATA.GET_DDL() function to get the table definition, and modify what is included with the SET_TRANSFORM_PARAM() options, specifically in this case the PARTITIONING parameter.
There are lots of examples for you to search for, but here's one that shows the DDL being simplified with similar transformations.
It's some work, but you can implement your own tool to create the DDL.
All you need is stored in the Oracle database catalogue.
To create just tables (without index and constraints) you need these 2 tables:
USER_TAB_COLUMNS
USER_TABLES
You will find a detailed documentation of these tablese here:
Oracle Database Reference
Other usefull Oracle tables are
USER_CONSTRAINTS
USER_INDEXES