I have two tables in the same database. With same constraint and same column name. Both tables have primary key with auto-increment and I want insert data directly from one table to the other by using following query.
insert into table_name select * from table_name
all data get inserted into table one but auto-increment is not happening.
in image their is same problem(table in image is created for test)
You can't use * you should use the column name without the id (therwise you insert the selected id and is not performed the autoincrement)
insert into table_name ( col1, col2)
select col1, col2 from table_name;
Related
I need to create an identical copy of many records in a table. The table has a PK id, which of course will be different, in the freshly-copied records.
For example, let's say i have a scrum_card table, with the following non-unique columns: name, description, board_id
I have a dynamic array of id's of records, which i wish to duplicate: [34,56,32,3445,...]
How do i tell MYSQL, to fetch the data from all those records, and make a batch-insert of those same records?
In "human" syntax it would look something like this: "Select all columns(besides id) from scrum_card where the id's are [array of id's], then duplicate each found record".
Use INSERT INTO ... SELECT... and in the list of columns do not include the id:
insert into scrum_card(name, description, board_id)
select name, description, board_id
from scrum_card
where id in (34,56,32,3445,...)
You can use INSERT using a SELECT result set as the source, instead of a set of literal row tuples with VALUES(...).
INSERT INTO new_table (id, col1, col2, col3...)
SELECT NULL, col1, col2, col3...
FROM old_table
WHERE id IN (34,56,32,3445,...)
Using NULL in place of the id column in the SELECT will return NULL for each row, which will cause new_table to generate a new id value.
But SQL does not have any way to do a wildcard like "all columns except id," besides you typing the column names in.
I am attempting to insert a daily table to a main table that has primary keys in it. I have the following architecture:
A python scripts that generates data in a pandas dataframe-> drops the daily table -> pushes to the daily table -> pushes the data from the daily table to the main table.
I cannot drop the main table but I can drop the daily table. At the moment I am using the following query to perform this:
REPLACE INTO
mydb.{to_table}
SELECT
*
FROM
mydb.{from_table};
but I am getting the following error message:
ERROR (1062, "Duplicate entry '2147483647' for key 'PRIMARY'") with sql
REPLACE INTO
mydb.book_authors_v2
SELECT
*
FROM
mybd.books_authors_v2_daily;
I've tried using INSERT ... UPDATE ON DUPLICATE KEY but it did not manage to get it to work.
Is there any way of inserting the new rows from the daily table to the main table and updating the existing rows in the main table? There are nearly daily changes to the structure of the table, I would like to avoid having to manually define the columns.
Yes, this looks like a good use case for the the INSERT ... ON DUPLICATE KEY syntax.
Assuming that the primary key of your table is called id, and you have 3 other columns col1, col2, col3 that you want to update on duplicate id, that would look like:
insert into mydb.book_authors_v2(id, col1, col2, col3)
select id, col1, col2, col3
from mybd.books_authors_v2_daily d
on duplicate key update set col1 = d.col1, col2 = d.col2, col3 = d.col3
i got 2 tables that i want to combine its data.
the id is my key field (incremental and distinct).
table 1 and table 2 field description for example:
id - name - value
i want to insert all of table 2 data into table 1, they have different data but in some rows the same id.
so when i try to:
INSERT INTO ladatabase.table2 SELECT * from ladatabase.table1;
i get duplicate entry error.
please help me to suggest how can i solve it and combine the data with different id values for the new data.
Alex.
here are 2 ways:
first you can use if the id AUTO INCREMENT
INSERT INTO ladatabase.table2
SELECT '', name, value from ladatabase.table1;
or you find the first free ID and add it
INSERT INTO ladatabase.table2
SELECT id+555 , name, value from ladatabase.table1;
No insert all fields (exclude 'id'-field from SELECT):
INSERT INTO ladatabase.table2 SELECT name,value from ladatabase.table1;
You need to copy all but the ID column, and it will assign new IDs. You need to list all the other columns explicitly. Leaving out the ID column will cause it to get its default value, which are sequential IDs for an auto_increment field.
INSERT INTO table2 (col2, col3, col4, ...)
SELECT col2, col3, col4, ...
FROM table1
I am working on redesigning of a legacy db and I have set new names to columns of old db. So, for instance, if olddb.oldtable under dbold has column descr, I have set it as description in new newdb.netable for column.
How can I mention individual columns in my query?
I am using MYSQL
Update: Both Databases are on different IP Addresses and I am using Navicat to transfer data.
You can try like this:
INSERT INTO newtable (col1, col2, ..., )
SELECT col1, col2, ..., FROM oldtable
By trying the above query you can insert the specific column. So for example if your newtable has a column as description and old table as descr then you can mention it like:
INSERT INTO newtable (col1, col2, `description`, ..., )
SELECT col1, col2, `descr` ,..., FROM oldtable
Also if the table column list is large and you want to copy all the columns and its data then you can simply use the wildcard charater * as:
INSERT INTO newtable
SELECT * FROM oldtable;
You can insert all columns at once without the need to mention the names using this:
INSERT INTO newtable (SELECT * FROM oldtable);
It will make an 1x1 match independently of column names.
If types don't match then will insert default values (not checked for all the type combination).
Note that column number must be the same on both tables otherwise an error like this will occur:
#1136 - Column count doesn't match value count at row 1
I have a table A filled with records. I created a table B with same columns, and I want to copy all contents of A to B. However, table A has an auto incremented key, so if i had first three records (1,'itemA') (2,'itemB') (5,'itemE') (assuming that 3,4,5 where deleted later). Those recors will be inserted into table B as (1,'itemA') (2,'itemB') (3,'itemE').
Is there a way to insert them exactly the same ?
Another thing is, table A is on mySql, and table B is on MS SQL Server
AFAIK mysql allow inserting into auto_increment field, so you can use statement like
insert into table2 (id, name) select id, name from table1
but later, if you need insert into table values with generated auto_inc, you need set auto_increment in table2 with value of auto_inc of table1
alter table table2 AUTO_INCREMENT = (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = $dbName AND TABLE_NAME = 'table1')
Yes.
create table b like a;
insert into b select * from a;
http://sqlfiddle.com/#!2/a344a/1
The answers above are good, but on MS SQL you can't insert auto increment value unless if you execute turn identiy_insert off:
SET IDENTITY_INSERT stock OFF;
INSERT INTO stock ( stock_id,stock_item) VALUES (5,'itemE');
SET IDENTITY_INSERT stock ON;
This is EXCATLY what I was looking for. Thank you all :)
At table with name test and columns (id autoinc, col1, col2, col3)
At table with name test2 and columns (id autoinc, col4, col5, col6)
INSERT INTO test(col4, col5, col6) SELECT col4, col5, col6 FROM test2;