Clickhouse. Create table from exist mysql DB - sql

It is necessary to create a table based on the existing mysql database, but only with those records that have passed the WHERE check. I can simply create a table with records, but I cannot understand the logic of how to insert a block of WHERE checks. The official wiki simply says AS SELETECT 1. Sample code is provided below. Help me please.
"CREATE TABLE test_table (id UInt32, payment_date DateTime('Europe/Moscow'))
ENGINE = MySQL('{host}:{port}', '{database}', 'test_table', '{username}', '{password}')
AS SELECT create_date FROM test_table
WHERE create_date > '2020-01-01'";

Related

How to make a new copy of table without CREATE TABLE but using INSERT

I'm using SQL Server 2014 and trying to figure out some not trivia task.
I have a table PROPERTY and need to copy some of the data to absolutely new table PROPERTY_1 (PROPERTY_1 is not created and I'm not allowed to use CREATE TABLE). I have to use INSERT only!
I've googled some fancy commands like INSERT INTO from MySQL:
INSERT INTO PROPERTY_1 SELECT * FROM PROPERTY
but it's no help because (surprise-surprise!) PROPERTY_1 is not created.
Is it any possible way to pass this task or it's just some kind of weird task?
You must use Select Into as described in the Microsoft: documentation
You can use Select into to create the structure only and use Insert into to copy the data like below:
SELECT * INTO PROPERTY_1 FROM PROPERTY WHERE 1=0
INSERT INTO PROPERTY_1 SELECT * FROM PROPERTY
Benefits of using Select into to create the structure of the copy table:
You don't need to worry about the columns and their data types in the new table (PROPERTY_1) as those will be similar to the base table (PROPERTY). The new table schema will match the original schema, including identity columns.
There won't be any errors while inserting the records in the new table.

Is there any way in DB2 to find temp table from the session?

Is there any way in DB2 to find temp table from the session ?
I have created a temp table pertaining to session
DECLARE GLOBAL TEMPORARY TABLE SESSION.TEMP_TABLE_NAME
(
COL_1 VARCHAR(11) NOT NULL,
COL_2 VARCHAR(10)
) ON COMMIT PRESERVE ROWS;
When I am trying to create query
select * from sysibm.systables where owner='SESSION' and name='TEMP_TABLE_NAME'
yields 0 rows.
Am I looking at the incorrect table to find temp tables ?
Thanks !
A declared global temporary table ( DGTT) will not appear in the catalog, this is the design - so you will not find a DGTT in sysibm.systables. A DGTT cannot be used by any other program except the one that declares it - it is specific to that session, hence there's no value to having it in the catalogue.
If you are using Db2 for z/OS (v10 or higher), or Db2-LUW, you may need instead, a "CREATED global temporary table" (CGTT) which uses a different syntax create global temporary table ... These are catalogued, but you need relevant permissions to create them.
See the Db2-LUW documentation.
or for Db2 for z/OS here.
Look at the SYSIBMADM.ADMINTEMPTABLES administrative view.
If you want to see all the DGTTs created in your session, then:
SELECT TABNAME
FROM SYSIBMADM.ADMINTEMPTABLES
WHERE TEMPTABTYPE='D'
AND APPLICATION_HANDLE=mon_get_application_handle();

Check if table exists in created Database in U-SQL

I am trying to check whether the table exists in database or not in U-SQL. Currently syntax is
DROP TABLE IF EXISTS Logs;
CREATE TABLE Logs (
date DateTime,
eventType int,
eventTime DateTime,
INDEX Index_EventType CLUSTERED (eventType ASC)
DISTRIBUTED BY HASH(eventType) INTO 3);
In this example, I just want to check if table exists or not in current database, I don't want to drop the table if it exists.
Basically I want to add if..else statements in U-SQL script for Table.Such as below:
IF NOT EXISTS Logs
{
//Create table here
}
else
{
//Update table scripts
}
How to have this particular condition in U-SQL script?
Can you please file a feature request at http://aka.ms/adlfeedback for TABLE.EXISTS?
As a workaround, you could create a fake partitioned table that only contains a single partition, or use any of the workarounds maya specified in the comments to her answers.
You can specify IF NOT EXISTS within the CREATE statement to achieve such behavior:
CREATE TABLE IF NOT EXISTS Logs (
date DateTime,
eventType int,
eventTime DateTime,
INDEX Index_EventType CLUSTERED (eventType ASC)
DISTRIBUTED BY HASH(eventType) INTO 3);

How can I copy a Redshift table but add a sortkey to a column?

I'm currently working on a project that uses a Redshift table with 51 columns. However, the person who made the table forgot to add a sortkey to our time column which will hurt performance for our use case if we don't add it.
How can I make a version of the table with our time column as the sortkey? I'm aware that you can't make a column a sortkey if its a member of an existing table, but I was hoping there's a way to do it that doesn't involve writing out the CREATE TABLE syntax by hand; for example, something like this would be nice:
timecube=# CREATE TABLE foo (like bar) sortkey(time);
ERROR: CREATE TABLE LIKE is not supported with DISTSTYLE, DISTKEY(), or SORTKEY() clauses
but as you can see its not supported. Is there another way? As we're still developing we don't need any of existing data.
Using traditional tools like pgdump didn't work well because they don't include any of the Redshift extras like encoding.
Redshift supports specifying the DIST and SORT keys as part of CREATE TABLE AS statements, as per the docs.
CREATE TABLE table_name
DISTSTYLE KEY
DISTKEY ( column )
SORTKEY ( column )
AS
(SELECT *
FROM source_table)
;
First step you need to do use get create table statement for existing table. Then create new table this time add sort key to new table.
Check encoding for old table ( when you load data using copy command it automatically adds compression encodings)
select "column", type, encoding
from pg_table_def where tablename = 'old_table'
When creating new table add encoding type for each column. Create table with Sort key .
Once new table is created use below command
insert into new table ( select * from old table order by time asc)

creating table as select is dropping the not null constraints in postgresql

in postgres sql creating the table as select dropped the not null constraints on the table.
for example :
create table A (char a not null);
create table B as select * from a;
select * from B;-- no constraint is copied from A table
please let me know how to copy table data as well as constraints in postgres.
There is no single-command solution to this.
To create a table based on an existing one, including all constraints, use:
create table B ( like a including constraints);
Once you have done that, you can copy the data from the old one to the new one:
insert into b
select * from a;
If you do this in a single transaction, it looks like an atomic operation to all other sessions connected to the database.
very detailed and nicely explained tutorial for create table command in PostgreSQL 9.1
http://www.postgresql.org/docs/current/static/sql-createtable.html
Not null constraints are always copied (if creating table by giving reference of parent table in create table command) and even with including constraints, only check constraint will be copied.