How to create table if not exists with HSQLDB - hsqldb

I'm using HSQLDB for persisting a small data, in my query I want to create tables at first time and if they are not exist anymore.
However with HSQLDB I cannot execute the query "CREATE TABLE XYS IF NOT EXISTS" like other dbms like mysql or mssql.
Please give me solution for this case.

HSQLDB supports the syntax like the example below:
CREATE TABLE IF NOT EXISTS xyx (a int, b varchar(10))
The syntax is supported by recent HSQLDB 2.3.X versions.

Related

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.

Insert If Table Exists in Databricks or Spark SQL

I am writing in a Databricks notebook where I need to say something like:
%sql
IF EXISTS myTable INSERT OVERWRITE TABLE myTable SELECT * FROM somethingElse
I know that the INSERT OVERWRITE clause works, but I'm not sure how to get the IF EXISTS to work without breaking out of pure SQL code and using python (which would make the script messier).
Unfortunately, there is no DDL named "IF EXISTS" supported in Databricks.
You have to use command called "Drop Table":
Drop a table and delete the directory associated with the table from the file system if this is not an EXTERNAL table. If the table to drop does not exist, an exception is thrown.
IF EXISTS
If the table does not exist, nothing happens.
DROP TABLE [IF EXISTS] [db_name.]table_name
Example:
DROP TABLE IF EXISTS diamonds;
CREATE TABLE diamonds USING CSV OPTIONS (path "/databricks-datasets/Rdatasets/data-001/csv/ggplot2/diamonds.csv", header "true")
Reference: SQL Language Manual This is a complete list of Data Definition Language (DDL) and Data Manipulation Language (DML) constructs supported in Databricks.
Hope this helps.

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

A not-existing column should not break the sql query within select

In my case there are different database versions (SQL Server). For example my table orders does have the column htmltext in version A, but in version B the column htmltext is missing.
Select [order_id], [order_date], [htmltext] from orders
I've got a huge (really huge statement), which is required to access to the column htmltext, if exists.
I know, I could do a if exists condition with two begin + end areas. But this would be very ugly, because my huge query would be twice in my whole SQL script (which contains a lot of huge statements).
Is there any possibility to select the column - but if the column not exists, it will be still ignored (or set to "null") instead of throwing an error (similar to the isnull() function)?
Thank you!
Create a View in both the versions..
In the version where the column htmltext exists then create it as
Create view vw_Table1
AS
select * from <your Table>
In the version where the htmlText does not exist then create it as
Create view vw_Table1
AS
select *,NULL as htmlText from <your Table>
Now, in your application code, you can safely use this view instead of the table itself and it behaves exactly as you requested.
First thing why a column would be missing? definitely its been deleted somewhere. if so, then the delete process must have updated/fixed the dependencies.
Instead fixing it after breaking, its better to do smart move by adopting some protocols before breaking anything.
IF Exists is a workaround that can help to keep queries running but its an overhead considering your huge database and queries
The "best" way to approach this is to check if the column exists in your database or not, and build your SQL query dynamically based on that information. I doubt if there is a more proper way to do this.
Checking if a column exists:
SELECT *
FROM sys.columns
WHERE Name = N'columnName'
AND Object_ID = Object_ID(N'tableName');
For more information: Dynamic SQL Statements in SQL Server