How to clone table in db2? - db2

I want to clone a table in DB2 . My requirement is to create the new table using base table and In the new table I need same number of columns and I don't need the data of old table.please help me how to achieve this.
Thanks in Advance ,
Raj

In DB2, you can use this syntax:
CREATE TABLE new_table LIKE old_table
This will create a table of the same type as the original table. I doubt that indexes/keys are copied, too, though.
Here's some documentation on that topic

EXCHANGE DATA BETWEEN BaseTable AND NewTable;

Related

Adding a table to HDB by using dbmaint function

I would like to backfill a table to all dates in HDB. but the table has like 100 columns. What's the fastest way to backfill with the existing table?
I tried to get the schema from the current table and use the schema to backfill but doesn't work.
this is what I tried:
oldTable:0#newTable;
addtable[dbdir;`table;oldTable]
but this doesn't work. Any good way?
Does the table exist within the latest date partition of the HDB?
If so .Q.chk will add tables to partitions in which they are missing.
https://code.kx.com/q/ref/dotq/#qchk-fill-hdb
And with regards to addtable, what specific error are you getting when trying the above?

postgresql copy from one table to another when the table updates

What I would like is when one row of a table is updated and a new table that will duplicate the original table will update as well but the problem is the original table is a master table that depends on other tables. Any idea how to do this? I'm very new to postgresql.
This is what triggers are for, assuming that the source and destination tables are in the same DB. In this case I think you need an AFTER INSERT OR UPDATE OR DELETE trigger.
http://www.postgresql.org/docs/current/static/plpgsql-trigger.html

how can I rename a table / move to a different schema in sql DB2?

I am trying to rename a table in db2 like so
rename table schema1.mytable to schema2.mytable
but getting the following error message:
the name "mytable" has the wrong number of qualifiers.. SQLCODE=-108,SQLSTATE=42601
what is the problem here.... I am using the exact syntax from IBM publib documentation.
You cannot change the schema of a given object. You have to recreate it.
There are severals ways to do that:
If you have only one table, you can export and import/load the table. If you use the IDX format, the DDL will be included in the generated file. If using another format, the table has be created.
You can recreate the table by using:
Create table schema2.mytable like schema1.mytable
You can extract the DDL with the db2look tool
If you are changing the schema name for a schema given, you can use ADMIN_COPY_SCHEMA
These last two options only create the table structure, and you still need to import the data. After having create the table, you insert the data by different ways:
Inserting directly
insert into schema2.mytable select * from schema1.mytable
Via load from cursor
Via a Load or import from file (The file exported in the previous step)
The problem is the foreign relations, because they have to be recreated.
Finally, you can create an alias. It is easier, and you do not have to deal with relations.
You can easily rename a table with this statement:
RENAME TABLE SCHEMA.TABLENAME TO NEWTABLENAME;
You're not renaming table in provided example, you're trying to move to different schema, it's not the same thing. Look into db2move tool for this.
if you want to rename a table in the same schema, you can use like this.
RENAME TABLE schema.table_name TO "new_table_name";
Otherwise, you can use tools like DBeaver to rename or copy tables in a db2 db.
What if you leave it as is and create an alias with the new name and schema.
Renaming a table means to rename a table within same schema .To rename in other schema ,db2 call its ALIAS:
db2 create alias for

Sqlite data Migration

What is best possible way for in-app Sqlite Data Migration. As I am new to iphone development , I created my app using Sqlite. Please help the concern
You create a database upgrade procedure and make it execute the first time when the application starts.
So this will create a new table through a create statement when application loads for the first time.
And also it will help you save all the data that user has in his current database. This is what is most recommended as there is no additional requirement to migrate your existing database anywhere.
Also please refer to my answer in this link:
Don't wan't to replacing the old database when app is updated
Hope this helps you.
If you need more help then please feel free to contact me.
EDIT:
In case when you need to make changes to an exiting table which contains some data then you need to do some steps as follows:
1) Rename the existing table
Say TableName was 'TestTable', So Rename the table to 'TestTableOld'
2) Create a new table called 'TestTable' which has the new columns you want and other altercations made.
3) Copy data from 'TestTableOld' to 'TestTable' with the query like:
Insert into TestTable('col1',col2'...'coln') Values Select col1, col2, col3,...'coln' From TestTableOld;
4) Drop the table 'TestTableOld'
Follow the above four step procedure as this would work as an alter statement and also it would preserve all your data from your original table.
Hope you get it.

how to insert value from one table to other table in sqlite3 database

I am new in iphone i am developing a application with data base i am create a database and have to table we want to insert one table value to other. I am not understand how do this.
if any body know help me with coding.
Nested query: INSERT INTO Table VALUES (SELECT blah...)