In MySQL, how to copy the content of one table to another table within the same database? - sql

I am new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert to a table from another table. Is there easy way of doing this?

If the tables have the same structure:
INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE;
If the tables have different structures:
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
You can also add conditions:
INSERT INTO TARGET_TABLE (`col1_`,`col2_`) SELECT `col1`,`col2` FROM SOURCE_TABLE WHERE `foo`=1

If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1

If table1 is large and you don't want to lock it for the duration of the copy process, you can do a dump-and-load instead:
CREATE TABLE table2 LIKE table1;
SELECT * INTO OUTFILE '/tmp/table1.txt' FROM table1;
LOAD DATA INFILE '/tmp/table1.txt' INTO TABLE table2;

This worked for me,
CREATE TABLE newtable LIKE oldtable;
Replicates newtable with old table
INSERT newtable SELECT * FROM oldtable;
Copies all the row data to new table.

If you want to create and copy the content in a single shot, just use the SELECT:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;

This worked for me. You can make the SELECT statement more complex, with WHERE and LIMIT clauses.
First duplicate your large table (without the data), run the following query, and then truncate the larger table.
INSERT INTO table_small (SELECT * FROM table_large WHERE column = 'value' LIMIT 100)
Super simple. :-)

CREATE TABLE target_table SELECT * FROM source_table;
It just create a new table with same structure as of source table and also copy all rows from source_table into target_table.
CREATE TABLE target_table SELECT * FROM source_table WHERE condition;
If you need some rows to be copied into target_table, then apply a condition inside where clause

Try this. Works well in my Oracle 10g,
CREATE TABLE new_table
AS (SELECT * FROM old_table);

Related

Removing entirely duplicate rows from all tables in an Oracle database

I'm looking for a query that will remove duplicates from a table, while at the same time being column-agnostic, so it can be iterated over all tables in the database.
Currently, I'm thinking of something like this:
CREATE TABLE TMP_TABLE AS SELECT * FROM EXISTING_TABLE;
TRUNCATE EXISTING_TABLE;
INSERT INTO EXISTING_TABLE (SELECT DISTINCT * FROM TMP_TABLE);
DROP TMP_TABLE;
Is this a good way to do it? Is there a way to do this in-place?
You can directly delete the duplicates in the original table rather than creating a new temp table and inserting back again.
DELETE * FROM EXISTING_TABLE where row_id not in
(select max(row_id) from EXISTING_TABLE group by col1); -- Include any one column in group by

Create clone copy of table without data in SQL Server

How can I create clone copy of table without data? Because I just want to copy a definition of table not data.
I tried below but it is copying data as well
Select *
into Clone_Supplier
from Supplier
Copy all columns from selected table
Select Top 0 * into NewTable from OldTable
Copy some columns from selected table
Select Top 0 Col1,Col2 into NewTable from OldTable
Copy all(Data and Structure)
Select * into NewTable from OldTable
You could add WHERE 1=2 to get structure only:
Select *
into Clone_Supplier
from Supplier
where 1=2;
It won't be exact copy though:
no constraints
no indexes
no triggers
It is useful table to generate and new table
Select Top 0 * into tblNew from tblOld
Solution Query :
SELECT TOP 0 * INTO newTable1 FROM oldTable1;
You just need to add one false condition. So that it wont return any data and you will create clone copy of table without data. you can use below query
Select * into Clone_Supplier from Supplier WHERE 1=2
Create Table new_table LIKE old_table
Will create an empty copy of original table with original attributes etc.

Need to add a constant value in a column while loading a hive table

I created a table named table1 in hive and i need to insert data from table2 into table1. I used the below statemnt to get the output.
Also i need to add a new column with some constant value -- colx = 'colval' along with the columns in table2 but am not sure how to add it.. Thanks!
INSERT INTO TABLE table1 select * FROM table2;
If you are willing to drop table1 and recreate it from scratch, you could do this:
-- I'm using Hive 0.13.0
DROP TABLE IF EXISTS table1;
CREATE TABLE table1 AS SELECT *, 'colval' AS colx FROM TABLE2;
If that is not an option for some reason, you can use INSERT OVERWRITE:
ALTER TABLE table1 ADD COLUMNS (colx STRING); -- Assuming you haven't created the column already
INSERT OVERWRITE TABLE table1 SELECT *, 'colval' FROM table2;

Keep Top 1000 rows from table by dropping and inserting new table?

I am paring down a database for a small project. I heard the most efficient way to get a table down to just 1000 rows was to copy those 1000 rows to a new table drop the old and rename the new.
Could someone let me know if this is right and roughly how to do it?
I know how to select top 1000 but not how to drop and create a table by using a temp table or the like.
INSERT INTO NEW_TABLE
SELECT TOP 1000 * FROM OLD_TABLE
DROP TABLE OLD_TABLE
You can run this to create your new table:
SELECT TOP 1000 * INTO NewTable FROM OldTable
USING SELECT INTO will create a new table. Drawbacks of this method are you lose your keys on your table.
Alternatively, if the table exists, use
INSERT INTO NewTable SELECT * FROM OldTable
However, if you have an Identity field, this won't work -- you'll need to specify the column names
INSERT INTO NewTable (Field1, Field2,...) SELECT Field1, Field2...
As you can see, it really depends on your situation.
To drop a table, it's pretty easy:
DROP TABLE OldTable
Good luck.

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table.
Is it possible to insert data into any existing table using select * into query.
I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union to insert all records into same table
eg.
select * into #tblExisting from tblExisting
drop table tblExisting
select * into tblExisting from #tblExisting union tblActualData
Here tblExisting is the table where I actually want to store all data
tblActualData is the table from where data is to be appended to tblExisting.
Is it right method.
Do we have some other alternative ?
You should try
INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable
Have a look at INSERT
and SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
No, you cannot use SELECT INTO to insert data into an existing table.
The documentation makes this very clear:
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
You generally want to avoid using SELECT INTO in production because it gives you very little control over how the table is created, and can lead to all sorts of nasty locking and other performance problems. You should create schemas explicitly and use INSERT - even for temporary tables.
#Ryan Chase
Can you do this by selecting all columns using *?
Yes!
INSERT INTO yourtable2
SELECT * FROM yourtable1
Update from CTE? http://www.sqlservercentral.com/Forums/Topic629743-338-1.aspx