How to copy table from one database to other database in mysql - mysql

I have two databases, test1 & test2. In test1 there is a table attendence, I want to copy this attendece table in database test2. I am writing following code:
CREATE TABLE test1.attendence SELECT * FROM test2.attendence;
But it gives the error:
--Table 'test2.attendence' doesn't exist
So please provide a way to do it.

insert into table2 select * from table1
or if they dont have the same structure:
insert into table2 (col, col2, col5) select (x,y,z) from table1

CREATE TABLE x LIKE other_db.y;
INSERT INTO x SELECT * FROM other_db.y;

you're almost there :)
but you want to create the new table in test2, so, the correct command should be
CREATE TABLE test2.attendence LIKE test1.attendence;
INSERT INTO test2.attendence SELECT * FROM test1.attendence;

Related

SQL Workbench- Generational table reading

I am new to SQL, I have a bunch of tables test1 test2 test3----test100. Those tables are created on a daily basis and have same columns/format. I want to create a table that should have 1 month past data, in other words i have to combine test1-test30 into one single table. I can only thinks of the below code but i am looking for something efficient and easy to do like using loops or while conditions..or some other way
create table final as
select * from test1;
insert into final from test2
insert into final from test3
.
.
insert into final from test30..
Can you suggest me a simpler way to do this instead of using 30 insert statements
Here is an example:
Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
Another solution is to dump the data from all your database if it has the same tables then insert it into .csv or txt file and import it into your final table.
Mysql -u [username] -p [database_name] < /path/to/file.[extention]

How to copy-paste data from one table to another in MySQL?

I have crate a db in MySQL which has a lot of tables. I want the value of one table to be automatically saved on another table too.
For example I write something on: table1.lastname, I want this to be also stored in table2.lastname .
How is this called and how I can do that with PHP My Admin?
CREATE TABLE new_table_name LIKE old_table_name
Create trigger after_insert on new_table
like this
CREATE TRIGGER `AFTER_INSERT` AFTER INSERT ON `new_table_name` FOR EACH ROW BEGIN
insert into new_table_name (column_names) values (column_values) ;
END
For first you must create table for data store.
Then you must create trigger on wanted table for catch event and insert data in early created table.
This will do what you want:
INSERT INTO table2 (lastname)
SELECT lastname
FROM table1
If you want to include all rows from table1. Otherwise you can add a WHERE statement to the end if you want to add only a subset of table1.
I hope this helps.
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
Or If the tables have different structures you can also:
INSERT INTO table2 (`col1`,`col2`) SELECT `col1`,`col2` FROM table1;
EDIT: to constrain this..
INSERT INTO table2 (`col1_`,`col2_`) SELECT `col1`,`col2` FROM
table1 WHERE `foo`=1

MySQL Copying tables

I have an empty template table called Table1. I want to copy this table multiple times so I can have Table2 Table3 Table4 and so on, including the fields.
I have tried:
SELECT *
INTO Table2
FROM Table1
but I keep getting this error:
ERROR 1327 (42000): Undeclared variable: Table2
Am I doing this wrong?
If table doesn't exist do:
CREATE TABLE table2 LIKE table1;
After table is created do:
INSERT INTO table2 SELECT * FROM table1;
Since you said Table1 is empty you can create an identical empty table called Table2 like this:
create table table2 like table1
If Table1 has data in it and you want to copy the data as well, then you can do this:
create table table2 as select * from table1

mysql concatenate multiple existing tables into one new table

I've done some research but all of the examples that I've found seem too complicated given what I would like to do. I have multiple tables of archived data by year (e.g. archive_2013, archive_2012, etc.) and would like to create a new master table (archive_master) consisting of all of the data from all of the tables. The tables have no keys and only 2 columns, one varchar(120) and the other char(20). I'm hoping that this is as simple and straightforward as I think it is.
A simple UNION will do the trick:
SELECT col1, col2
FROM archive_2013
UNION ALL
SELECT col1, col2
FROM archive_2012
Combine it with an INSERT and you are done:
INSERT INTO full_archive
SELECT col1, col2
FROM archive_2013
UNION ALL
SELECT col1, col2
FROM archive_2012
So you want to create one new table?
You could use a simple INSERT INTO with a SELECT
See:
CREATE TABLE with SELECT
http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
INSERT INTO TABLE with SELECT
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
Example
You can create a new table:
create table 'xyz' select * from archive_2010;
insert into xyz select * from archive_2011;
INSERT INTO archive_master VALUES (SELECT * FROM archive_2013);

Transfering all data from one table to the other same database

I am trying to move a complete row of records which includes BLOB from one table (TABLE1) to another table (TABLE2) in the same database using SQL. I have tried what little I know but it failed.
What I have already used which failed is:
INSERT INTO TABLE2
SELECT * FROM TABLE1
WHERE
staff_id = '0010002';
How should I do this instead?
Try:
INSERT INTO TABLE2 (colname1, colname2)
SELECT colename1, colename2
FROM TABLE1
WHERE staff_id = '0010002'
And for more details.http://dev.mysql.com/doc/refman/5.0/en/insert-select.html