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
Related
Available Data
Name of Table1 : 'table1'
Join Column of Table1 : 'column1'
Other columns of Table1: Unknown
Name of Table2 : 'table2'
Join Column of Table2 : 'column2'
Other columns of Table2 : Unknown
Task : Create a new Table by joining two tables table1 and table2 based on table1.column1 = table2.column2. New table should have all the columns of table1 and table2 without duplicate columns(The schema of new table should be similar to schema of table1 and table2 combined). And also it should have only those rows that satisfy the condition given above .
What I tried :
CREATE TABLE newtable AS (
SELECT * FROM table1, table2
WHERE table1.column1=table2.column2
)
Result I got : I am getting proper results for the above query if all the columns are unique. But if there are columns with same names, i'm getting an error: column 'column3' specified more than once because column3 is common in both the tables. The main problem is i will not be given the schema of either table1 or table2.
Please provide me a query to remove the duplicate column ( like 'column3' mentioned above ) while creating new table. A complete new query is also accepted
I tried UNION ALL but it's not working
Thanks in advance
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;
How can we copy data from one table into another table which doesn't exist in the first one. In table one of the column is primary key.
INSERT INTO table SELECT * FROM db2.table;
ERROR 1062 (23000): Duplicate entry '100001' for key 'id_UNIQUE'
You can use the predicate NOT IN to do so like this:
INSERT INTO table1
SELECT *
FROM db2.table2
WHERE table1ReferenceID NOT IN(SELECT id_UNIQUE FROM table1);
This will checks whether this table1ReferenceID found in the first table or nor. Therefore, the SELECT clause will select all the rows from the second tables except those that is already presented in the first table table1.
Note that: the column table1ReferenceID is the reference of the id_UNIQUE in the second table.
Other alternatives for this, is to LEFT JOIN as suggested by #HamletHakobyan's answer and NOT EXISTS.
See it in action
Try this:
INSERT INTO table
SELECT T1.*
FROM db2.table T1
LEFT JOIN table T2
ON T1.Id = T2.Id
WHERE T2.Id IS NULL;
WHERE [primary_key_table1] not IN (SELECT [primary_key_table2] FROM [table2])
so i suggest something like
INSERT INTO table (SELECT * FROM db2.table WHERE id NOT IN (SELECT id FROM table));
I have two tables. table1 (source) and table2 (destination). I want to whole records in table2 to be copied to table1 if and only if the record is not already exists in table1. I use the following statement:
INSERT INTO test.dest
SELECT *
FROM test.source
WHERE NOT EXISTS(SELECT *
FROM test.dest
WHERE (source.DomainName=dest.hostnmae)
);
My question: Is there another better method to speed the process??
Is it possible to import the attributes of one table, then I put it into another table using a query in mysql?
For example I have table1 with attributes lname, fname, mname
And I want to put those attributes into table2.
Is there any query that could do that? I'm imagining that the table2 has one attribute that could later be dropped so that it will be the same as table1.
I am not entirely sure what you are asking.
If you want to copy the structure of table1 into a new table, do something like this:
CREATE TABLE table2 LIKE table1;
If you want to copy existing values from one table to another, you can then use the INSERT...SELECT syntax as follows:
INSERT INTO table2 (lname, fname, mname)
SELECT t1.lname,
t1.fname,
t1.mname
FROM table1 t1;
Do you want TABLE2 to look just like an empty TABLE1? If so, you could do
CREATE table2 LIKE table1;
If you use
SHOW CREATE TABLE table1
It will return most of the column syntax.
Then add ADD in front of all the columns you want to append to table 2, and change CREATE TABLE table1 to ALTER TABLE table2