SQLite create table from table - sql

I need to create a copy of the table in SQLite. Result table should have only one difference. One of the rows called 'Desired_row' must have different type(int -> bigint).
If I knew full source table structure I could just create a new table with identical fields.
CREATE TABLE MyTableCopy(... , )
As I have different versions of the table, I do not exactly know its full structure to create a new one with hardcoded columns. I need something which will do something like this CREATE TABLE Destination AS SELECT * FROM Source, but will not create Desired_row in the Destination table.
Any ideas? Thanks in advance!

with
SELECT sql FROM sqlite_master WHERE type='table' AND name='mytable'
you can get the the structure. This you can modify and create your new table. And finally you can
INSERT INTO 'MyTableCopy' (*) SELECT * FROM 'mytable'

In python you can clone/copy a sqlite table named "mytable" to a new table "mynewtable" using:
conn = sqlite3.connect(file)
cur = conn.cursor()
cur.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='mytable'")
txt=(cur.fetchone()[0])
txt=txt.replace("mytable","mynewtable")
cur.execute(txt)
cur.execute("INSERT INTO mynewtable SELECT * FROM mytable")
i believe this method will work similarly in other languages by: 1) getting the create mytable statement from the sqlite_master table, 2) changing only the table name, 3)execute the new create mynewtable statement and finally 4)populate the new table by inserting all data from old table

Related

How to generate a script to create all tables with different schema

I have about 200 tables in a schema.
I need to replicate these tables in a new backup schema with an automatic procedure.
I would like to create a procedure to dynamically recreate all the Tables in a Schema (potentially dynamic number of tables and columns) on a different schema.
I can cycle all the tables and create the SELECT * INTO dbo_b.TABLE FROM dbo.TABLE statement, but I get the error:
Column 'AMBIENTE' has a data type that cannot participate in a columnstore index.
I created a view that simply SELECT * FROM TABLE, and tried to perform the SELECT * INTO dbo_b.TABLE from dbo.VIEW but I got the same issue.
It works only if I create the dbo_b.Table and INSERT INTO it: so I would need to generate a script to automatically cycle all the tables in my schema and generate a script to create the tables in the new schema.
It's not a one time job, it should run every day so I cannot do it manually.
Seams we get the same issue.
You can try to loop on all table and create table in the new schema in this way:
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'YYYY' AND TABLE_SCHEMA = 'XXXX')
drop table [ZZZZ].[YYYY]
CREATE TABLE [ZZZZ].[YYYY]
WITH ( DISTRIBUTION = ROUND_ROBIN
, HEAP ) as
( SELECT * FROM XXXX.YYYY )
Let me know. BR

How to copy table by spark-sql

Actually, I want to move one table to another database.
But spark don't permit this.
Then, how to copy table by spark-sql?
I already tried this.
SELECT *
INTO table1 IN new_database
FROM old_database.table1
But it was not working.
maybe try:
CREATE TABLE new_db.new_table AS
SELECT *
FROM old_db.old_table;
To preserve partitioning and storage format do the following-
Get the complete schema of the existing table by running-
show create table db.old_table
The above query will output the table schema which you can just execute after changing the path name and table name.
Then insert all the rows into the new blank table using-
insert into db.new_table select * from db.old_table
The following snippet will create a new table while preserving the definition of the "old" table.
CREATE TABLE db.new_table LIKE db.old_table;
For more info, check the doc's CREATE TABLE.

SAP HANA create table / insert into new table from select

How to create a new table and insert content of another table?
create column table my_new_table as
(select * from my_existing_table)
Another, more SAP HANA specific solution is to use the
CREATE TABLE ... LIKE <TABLE_NAME> WITH [NO] DATA ...
syntax (https://help.sap.com/saphelp_hanaplatform/helpdata/en/20/d58a5f75191014b2fe92141b7df228/content.htm#loio20d58a5f75191014b2fe92141b7df228__sql_create_table_1create_table_like_clause).
This allows more control over the physical properties of the new table.
Like SQL Server you can create a temp table right from your select, the way is a little bit different.
Just execute:
temp_table = select 1 as col1, 'lorem ipsum' as col2 from dummy;
After that, you are able to use this temp table to query data from.
Like so:
select * from :temp_table;
Table Variable Type Definition
Unfortunately, there is some limitations using that. For example, you cannot simply insert new data. For that, exists some tricks.

Dynamically creating tables using information held in another table

I want to create a table in sql using the columns details (name, data type etc.) stored in anther table in the database.
Depending on the database you can use the information schema tables. They hold the information you are looking for. Look for the table that describes the columns.
Postgres: http://www.postgresql.org/docs/8.4/interactive/information-schema.html
MySQL: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
You can query these tables and use 'select into' to insert the results into your other table.
One opinion is to create CREATE TABLE query and execute it in ADO.NET like shown here this
Try out this code
CREATE TABLE new_table
AS
SELECT *
FROM old_table
WHERE 1 = 2;

Create a replica of a sql table

I need a query to create a table which is the exact replica but with different table name and without any data from the source table using a sql query!
You can try this
SELECT * INTO Table_Copy
FROM Table
where 1=2
It will create a empty table with the same structure.
SQL Server Management Studio
Object Explorer
Connect -> Your server
Databases -> Choose Database
Tables
Right Click Your Table
Script Table as -> Create To -> New Query Editor Window
Jonathan has it (upvoted), and you should probably go with that because it's more portable. I normally use something similar:
SELECT TOP 0 * INTO [New_Table] FROM [Old_Table]
I think this better expresses what you're doing, but I like Jonathan's because 'TOP 0' is SQL Server specific, and so his is more portable.
For MySQL, you can call SHOW CREATE TABLE table_name;
It will display a CREATE TABLE query. Simply change the table name in that query and you're good to go.
http://dev.mysql.com/doc/refman/5.1/en/show-create-table.html
If you use Postgresql:
CREATE TABLE LIKE table_name
http://www.postgresql.org/docs/8.1/static/sql-createtable.html
SELECT * INTO Table_Copy
FROM Table
where 1=2
This worked very well, when i tried to create a replica of the table without any data's.
SELECT * INTO Table_Copy
FROM Table
This will create a replica with the data's too.
This can help you:
CREATE TABLE foo AS SELECT...
Read more here
select * into newtablename from sourcetablename
go
truncate newtablename
go
That will result in an exact copy but it also copies the data at first which you remove with the truncate statement.
create table <new table name> as select * from <old tale name from which you would like to extract data>
It will create a new table with a different name but will copy all existing data from the old table to new table.
in postgres you can use INHERITS or LIKE keyword to make replica of a table(only copies structure of the table)
CREATE TABLE client_new (LIKE client);
or
CREATE TABLE client_new () INHERITS (client)
Use of INHERITS creates a persistent relationship between the new child table and its parent table(s). Schema modifications to the parent(s) normally propagate to children as well, and by default the data of the child table is included in scans of the parent(s).
LIKE clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints.Unlike INHERITS, the new table and original table are completely decoupled after creation is complete. Changes to the original table will not be applied to the new table, and it is not possible to include data of the new table in scans of the original table.