Oracle sql developer - export DDL - only create table sql - 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

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

How to get CREATE TABLE Statement from existing table in db2?

I am using DB2 and Oracle SQL Developer.
How to get the CREATE TABLE Statements from the existing tables?
There are too many tables and it will be a very lengthy process to do manually.
There is a special db2look utility for DDL extraction in Db2. You may refer to its options and their meaning at this link.
If you want SQL access to its capabilities, you may use the SYSPROC.DB2LK_GENERATE_DDL stored procedure supporting most of the utility's options. The routine has an output parameter getting "invocation number" int value after its call.
In case of a single table:
CALL SYSPROC.DB2LK_GENERATE_DDL ('-e -noview -t MY_SCHEMA.MY_TABLE', ?);
SELECT SQL_STMT
FROM SYSTOOLS.DB2LOOK_INFO_V
WHERE OP_TOKEN = <value_of_output_parameter_from_call_above>
ORDER BY OP_SEQUENCE;
In SQLDeveloper if you can see the table there's the initial Create Table Statement in the SQL Tab
You should do that for each table, this is a way to do it but I'm not sure it's fast enough for you.

Best way of importing tables to PostGre from Oracle?

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.

Creating table in mysql

Is it possible to create more than one table at a time using single create table statement.
For MySQL, you can use multi-query to execute multiple SQL statements in a single call. You'd issue two CREATE TABLE statements separated by a semicolon.
But each CREATE TABLE statement individually can create only one table. The syntax supported by MySQL does not allow multiple tables to be created simultaneously.
#bsdfish suggests using transactions, but DDL statements like CREATE TABLE cause implicit transaction commits. There's no way to execute multiple CREATE TABLE statements in a single transaction in MySQL.
I'm also curious why you would need to create two tables simultaneously. The only idea I could come up with is if the two tables have cyclical dependencies, i.e. they reference each other with foreign keys. The solution to that is to create the first table without that foreign key, then create the second table, then add the foreign key to the first table with ALTER TABLE ADD CONSTRAINT. Dropping either table requires a similar process in reverse.
Not with MS SQL Server. Not sure about mysql.
Can you give more info on why you'd want to do this? Perhaps there's an alternative approach.
I don't know, but I don't think you can do that. Why you want to do this?
Not in standard SQL using just the 'CREATE TABLE' statement. However, you can write multiple statements inside a CREATE SCHEMA statement, and some of those statements can be CREATE TABLE statements. Next question - does your DBMS support CREATE SCHEMA? And does it have any untoward side-effects?
Judging from the MySQL manual pages, it does support CREATE SCHEMA as a synonym for CREATE DATABASE. That would be an example of one of the 'untoward side-effects' I was referring to.
(Did you know that standard SQL does not provide a 'CREATE DATABASE' statement?)
I don't think it's possible to create more than one table with a 'CREATE TABLE' command. Everything really depends on what you want to do. If you want the creation to be atomic, transactions are probably the way to go. If you create all your tables inside a transaction, it will act as a single create statement from the perspective of anything going on outside the transaction.