SQL Command for copying table - sql

What is the SQL command to copy a table from one database to another database?
I am using MySQL and I have two databases x and y. Suppose I have a table in x called a and I need to copy that table to y database.
Sorry if the question is too novice.
Thanks.

If the target table doesn't exist....
CREATE TABLE dest_table AS (SELECT * FROM source_table);
If the target table does exist
INSERT INTO dest_table (SELECT * FROM source_table);
Caveat: Only tested in Oracle

If your two database are separated, the simplest thing to do would be to create a dump of your table and to load it into the second database. Refer to your database manual to see how a dump can be performed.
Otherwise you can use the following syntax (for MySQL)
INSERT INTO database_b.table (SELECT * FROM database_a.table)

Since your scenario involves two different databases, the correct query should be...
INSERT INTO Y..dest_table (SELECT * FROM source_table);
Query assumes, you are running it using X database.

If you just want to copy the contents, you might be looking for select into:
http://www.w3schools.com/Sql/sql_select_into.asp. This will not create an identical copy though, it will just copy every row from one table to another.

At the command line
mysqldump somedb sometable -u user -p | mysql otherdb -u user -p
then type both passwords.
This works even if they are on different hosts (just add the -h parameter as usual), which you can't do with insert select.
Be careful not to accidentally pipe into the wrong db or you will end up dropping the sometable table in that db! (The dump will start with 'drop table sometable').

insert blah from select suggested by others is good for copying the data under mysql.
If you want to copy the table structure you might want to use the show create table Tablename; statement.

Related

how to copy only data from one database table to another database existing table in sql server

how to copy only data from one database table to another database existing table in sql server query?
Copy one database existing table to another database existing table.
anyone know, Please tell me Sql query
Open image
insert into <target table name>(columns)
select columns
from <source table name>
Try this ...
INSERT INTO DataBase2.dbo.table2
SELECT * FROM DataBase1.dbo.table1
Generate scripts in SQL Server Management Studio from one database's table and run the generated script in another database.
Broadly speaking:
INSERT INTO [databaseName].[schemaName].[table2]
SELECT * FROM [databaseName].[schemaName].[table1]
If you are more specific in your question, I can provide a more detailed answer.
I suppose you want to copy data from all columns keeping the same order in two tables.
If both tables exist in your database use
insert into targetTable
select * from sourceTable
If not, use
select * into targetTable
from sourceTable
In the second case, the targetTable will inherit the datatypes from the sourceTable (not the keys, indexes, etc..)

How do I copy data from one table to another in postgres using copy command

We use copy command to copy data of one table to a file outside database.
Is it possible to copy data of one table to another table using command.
If yes can anyone please share the query.
Or is there any better approach like we can use pg_dump or something like that.
You cannot easily do that, but there's also no need to do so.
CREATE TABLE mycopy AS
SELECT * FROM mytable;
or
CREATE TABLE mycopy (LIKE mytable INCLUDING ALL);
INSERT INTO mycopy
SELECT * FROM mytable;
If you need to select only some columns or reorder them, you can do this:
INSERT INTO mycopy(colA, colB)
SELECT col1, col2 FROM mytable;
You can also do a selective pg_dump and restore of just the target table.
If the columns are the same (names and datatypes) in both tables then you can use the following
INSERT INTO receivingtable (SELECT * FROM sourcetable WHERE column1='parameter' AND column2='anotherparameter');
Suppose there is already a table and you want to copy all records from this table to another table which is not currently present in the database then following query will do this task for you:
SELECT * into public."NewTable" FROM public."ExistingTable";

pg_dump vs COPY (SELECT * FROM my_table)

I need to copy the contents of a table from one database to another database with an identical table which is currently empty.
I plan to dump the table data from the old table and then simply import it to the empty table in the new database. However, I ran into some behavior I don't understand using pg_dump.
I try to dump the table data to a file with the command:
pg_dump -a -t '"my_table"' my_database > /tmp/my_table.sql
This works, but I only get 8 records and there are over 1000 records in the table if I view the table like so:
SELECT * FROM my_table;
So, I tried to use the COPY command to generate a .csv file and I see similar behavior:
COPY my_table TO '/tmp/my_table.csv' WITH CSV HEADER;
I get the same 8 records as pg_dump. But, with:
COPY (SELECT * FROM my_table) TO '/tmp/my_table.csv' WITH CSV HEADER;
I get all 1266 records.
I would assume these commands should all return the same data, but obviously, I'm wrong. What is the difference?
Is it possible that my_table is part of an inheritance hierarchy? I ask because http://www.postgresql.org/docs/9.0/interactive/sql-copy.html#AEN58984 has this:
COPY only deals with the specific table named; it does not copy data to or from child tables. Thus for example COPY table TO shows the same data as SELECT * FROM ONLY table. But COPY (SELECT * FROM table) TO ... can be used to dump all of the data in an inheritance hierarchy.
You should be able to check by running:
SELECT * FROM ONLY my_table;
If that returns just the 8 records then we're on the right track, and we just need to find the child tables (for which How to find child tables that inherit from another table in PSQL will be helpful).
If not then I'm not sure - I wondered if maybe Rules or Triggers were getting involved, but I can't see how at the moment. Still, maybe it gives someone else an idea...?

Where temp tables are located?

If I create a temporary table using # sign:
SELECT * INTO #temp FROM dbo.table
Where is this table located? I can't find this from tempdb.
Those tables are created in your tempDB - but the table name might not be exactly as you defined.
In my case, I get:
#temp______________________________000000000003
Try this:
SELECT * INTO #temp FROM dbo.table
SELECT * FROM tempdb.sys.tables
You should see an entry for that temp table you've just created....
When you declare a temporary table, SQL Sever adds some additional characters on its name in order to provide a unique system name for it and then it stores it in tempDB in the sysobjects table. Even though you can query the temporary table with its logical name, internally is known with the exact name SQL Server has set.
How are you looking for them?
If you do a select you'll get the data.
But the table is only available in the session, just for the user who created it (you can have global temp tables).
They are stored in temp db.
Local temp tables can be created using hash (#) sign prior to table name.
They are visible only in current connection. When connection is dropped its scope ends as well.
It is possible to create and use local temp table with the same name simultaneously in two different connections.
Read More
http://sqlnetcode.blogspot.com/2011/11/there-is-already-object-named-temp-in.html
I suspect this issue rose from the fact that if you don't right click and refresh the 'Temporary Tables' folder, SSMS will not show you the temp table immediately.

Copy data from table of one db into table of another db using query (both tables have same structure)

I have two database in the same schema. My db is in Postgres. I want to copy data of any table (i.e product) of my 1st db into the same table of the 2nd db.
Is it possible to do so using query?
Can't do it as a single SQL command (at least not without dblink), but the easiest way is probably to just use a pipe between two psql's - use COPY on both ends, one sending the data out in CSV format the other one receiving it.
try
insert into db1.table1 select * from db2.table2
It's not possible in vanilla PostgreSQL installation.
If you are able to install contrib modules, use dblink:
INSERT
INTO product
SELECT *
FROM dblink
(
'dbname=sourcedb',
'
SELECT *
FROM product
'
) AS p (id INT, column1 INT, column2 TEXT, …)
This should be run in the target database.