Join 2 tables with same PRIMARY key that is autoincrementing too - mysql

I have 2 tables with both of them having the first column as PRIMARY which is also an auto incrementing. First table has 67 entries starting from 1 to 67 and the second table has 48 entries. Both of them have same columns. I want to take the content from Table2 and insert them into Table1 such that the next entry in Table1 starts from 68. Finally I will have 115 entries in Table1 and the PRIMARY column will also show that. I tried this:
INSERT INTO `Table1` SELECT * FROM `Table2`
But it said
#1062 - Duplicate entry '1' for key 'PRIMARY'
What do I do?

Name the columns you want to insert and leave the auto_incrment columns from the insert
INSERT INTO `Table1` (col2, col3, col4)
select col2, col3, col4 from `table2`

You need to specify the columns you wish to enter, without the identity column.
Something like
INSERT INTO `Table1` (column1, column2,...,columnn)
SELECT column1, column2,...,columnn FROM `Table2`

SET IDENTITY_INSERT database_name.schema_name.table ON
--Do Insert Here
SET IDENTITY_INSERT database_name.schema_name.table OFF
Make sure there is no duplicate id thought.
See this page

try out this...
INSERT INTO Table1 (col2, col3,...)
select col2, col3,... from Table2
you can do it with n number of columns...
Here you need to specify column name on that you wish to insert data other than identity column(Auto Increment Column).

Related

MYSQL Duplicate entry 1 for key 'PRIMARY' when importing to another table

I am trying to import some value from a table to another table, the problem is that the 2 tables have a primary column id
So when I do INSERT INTO tab1 (SELECT * FROM tab2)
Duplicate entry 1 for key 'PRIMARY'
It says I can't because the id 1 already exists,
I don't really care about conserving the id, I just want to insert it at the end of tab1
So I had the id to do this (without the id column):
INSERT INTO tab1 (SELECT col2,col3 FROM tab2)
...etc but it says Column count doesn't match value count at row 1
How can I do to just insert evrything with a new id?
thanks in advance
This should work:
INSERT INTO table1 (col2, col3) SELECT table2.anothercol2, table2.anothercol3 FROM table2;
https://dev.mysql.com/doc/refman/5.0/en/insert-select.html
If you can alter the stucture of table 'tab1' you could set in 'tab1' an auto-increment field for primary and the other two fields accordingly to 'tab2'.
INSERT INTO tab1 (SELECT col2,col3 FROM tab2) should work then.

INSERT Query for complete row expect id

I tried to insert a complete row to a table. But the problem is, that I use the same IDs. Is there a easy way to insert a complete row expect the one value (ID)?
INSERT INTO new_table SELECT * FROM old_table WHERE q_value = 12345
I would not insert every single value, because there are hunderds of columns.
Thanks for your help in advance,
Yab86
May be Something like this
You can do without getting column2
Insert Into new_table (Column1,Column3,Column4 ...)
Select Column1,Column3,Column4 ... From old_table
WHERE q_value = 12345
If ID is part of a unique key the only (good) way is to write out all the other columns, like this
insert into new_table
values (col1, col2, col3)
select col1, col2, col3
from old_table
where q_value = 12345;
If ID isn't part of the unique key there might be some ways to do it in two queries (easier to write but perhaps not better)
insert into new_table
select *
from old_table
where q_value = 12345;
update new_table
set ID = null
where q_value = 12345;
If the first column in new_table is your auto_increment primary id, you could use this, but then you cannot use the asterisk and have to list all columns except the first one (0 will do the trick for your auto_increment, that means it will insert the incremented value of the autoindex):
INSERT INTO new_table
SELECT 0, column2, column3, ...
FROM old_table WHERE q_value = 12345

How to insert records from table to another without duplicate

I have two tables t1 and t2. t1 has duplicated values. I need to insert all records from t1 to t2, but I don't want duplicates to occur in t2. I tried the following command which seems to me correct and no syntax error when I run it but the problem, it has 0 effect. No records from t1 inserted in t2.
insert into test.t2 (name2)
select name1 from test.t1 where NOT EXISTS (select name2 from test.t2);
Can anybody help ?
insert into test.t2(name2)
select distinct name1 from test.t1 where name1 NOT IN(select name2 from test.t2);
OR
insert into test.t2(name2)
select distinct name1 from test.t1 t1 where NOT EXISTS(select name2 from test.t2 t2 where t1.name1=t2.name2);
You can create a unique index (of one or more columns) and then use the MySQL replace command.
CREATE UNIQUE INDEX unique_name2 ON t2 (name2);
REPLACE INTO t2 (name2) values ($name1);
...
You've go two options here, one involves not duplicating the data on your insert, the second being to ignore duplicates when inserting.
To de-duplicate from the SELECT call you'd use `DISTINCT:
INSERT INTO test.t2 (name2) SELECT name1 FROM test.t1 WHERE name1 NOT IN (SELECT name2 FROM test.t2)
You can also add a UNIQUE index to avoid this problem in the first place:
ALTER TABLE t2 ADD UNIQUE INDEX index_name2 (name2)
Note that you will get an error if there is already duplicate data in t2, so you may have to clean it up beforehand.
Then you can add data with the IGNORE option:
INSERT IGNORE INTO test.t2 (name2) SELECT name1 FROM TEST.t1
The unique index approach will guarantee uniqueness.
I need to insert data from user to another user and when write this statement
.....
insert into trep12.jobhead
select
*
from
wsfin04.jobhead
where
wsfin04.jobhead.job_no not in (select job_no from trep12.jobhead)
and wsfin04.jobhead.CHASS_NO not in (select CHASS_NO from trep12.jobhead)
and rdate between '01-jul-15'
and '01-oct-15'
and job_type = 1;
.....
the result is 0 rows created.
this should do it:
INSERT IGNORE INTO test.t2 SELECT name2 FROM test.t1
Selects from one table and inserts into another.

MySQL Single Table Scan Update

Is there a way to accomplish a single table scan in MySQL with an UPDATE? The following is a standard example:
IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
This is the ideal situation I'd like to happen in MySQL (But this is MsSQL):
UPDATE user SET (name = 'jesse') WHERE userid ='10001'
IF ##ROWCOUNT=0
INSERT INTO user (name) VALUES('jeeeeee')
It's sort of reversed in MySQL. You perform the insert, and if the key already exists, then update the row:
INSERT INTO Table1 (col1,col2,col3) VALUES (val1,val2,val3)
ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2, col3 = val3;
This is predicated on you having a unique key for the table (which you do, right?)

Copying a table into another table but preserving the same auto increment key

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;