table1 : bo_indexable_attribute
id version mcs name search_id
285 3 13 name1 16
286 3 13 name2 16
287 3 13 name3 16
what i want is copying the content of this table and insert it into it again but this time with different mcs column
so my tryings is
CREATE TEMPORARY TABLE bo_scenario_indexable_attribute_temp (SELECT * FROM bo_scenario_indexable_attribute WHERE mcs = #sales );
UPDATE bo_scenario_indexable_attribute_temp SET mcs = #sales_master;
INSERT INTO bo_scenario_indexable_attribute SELECT * FROM bo_scenario_indexable_attribute_temp;
but this gives me Duplicate entry '285' for key 'PRIMARY'
any suggestions ??
You have copied your table into temporary one by the first statement. Then by the second you do an update and in the third statament you are trying to insert into the first table the same records. Maybe your id column has unique key so you get a "duplicate entry for key primary".
Take a look here: SQL UNIQUE Constraint
As #Franky pointed out, your id column has a unique constraint. Use this:
INSERT INTO bo_scenario_indexable_attribute SELECT version, mcs, name, search_id FROM bo_scenario_indexable_attribute_temp;
By leaving the id value out of insertion it the column will be assigned a new, unused id value which satisfies the unique constraint placed upon the column.
Edit based on first comment:
Use below queries:
CREATE TEMPORARY TABLE bo_scenario_indexable_attribute_temp (SELECT version, mcs, name, search_id FROM bo_scenario_indexable_attribute WHERE mcs = #sales );
UPDATE bo_scenario_indexable_attribute_temp SET mcs = #sales_master;
INSERT INTO bo_scenario_indexable_attribute SELECT version, mcs, name, search_id FROM bo_scenario_indexable_attribute_temp;
You got that error because you fetched more columns in your SELECT than you are inserting in your INSERT.
Related
i have a table with much data in it. the id is set as a primary with AUTO_INCREMENT function. how can i insert a new row like
INSERT INTO `medikamente`(`id`, `Augmentin`, `Ciproxin`, `Klacid`, `Voltaren`, `Seractil`, `Mexalen`, `Aspirin`, `Thomapyrin`, `Esomeprazol`, `Omeprazol`, `Nexium`, `Pantoloc`, `Guttalax`, `indikator`, `indikator2`)
VALUES (NULL,'','','','','','','','','','','','','','','');
lets say between id 5 and id 6? i don't want to change the id's manualy to have a free row between that id's. how can i do that over an command?
br
You can get free place in id sequence by query:
UPDATE medikamente SET id = id + 1 WHERE id > 5 ORDER BY id DESC;
After you can use id 6 for insert new row:
INSERT INTO `medikamente`(
`id`, `Augmentin`, `Ciproxin`, `Klacid`, `Voltaren`, `Seractil`, `Mexalen`, `Aspirin`, `Thomapyrin`, `Esomeprazol`, `Omeprazol`, `Nexium`, `Pantoloc`, `Guttalax`, `indikator`, `indikator2`
) VALUES (
6,'','','','','','','','','','','','','','',''
);
Be carefully if you table have foreign keys.
I have a table to store client's answers.I want to use one mysql query to insert or update this table.
My Table name : questionform_answer
and columns > ClientID QuestionID OptionID
Each client can only have one same question id.For example
ClientID QuestionID OptionID
1 1 1
1 2 5
2 1 3
I want to update OptionID if already exist ClientID and QuestionID.I don't want to use select query so taking so time.
I tried
ON KEY UPDATE
Replace Into
But I could not.
I use php so I tried first update query and if mysqli return fail insert row but it is also slow.
MY insert and update code :
Insert Into questionform_answer (ClientID,QuestionID,OptionID) values
('$ClientID','$soruid','$cevapid')
Update questionform_answer set OptionID='$cevapid' where
ClientID='$ClientID' and QuestionID='$soruid'
One way around this is to add a unique key over (ClientID, QuestionID) and use an INSERT ... ON DUPLICATE KEY UPDATE query:
ALTER TABLE table1
ADD UNIQUE INDEX (ClientID, QuestionID);
INSERT INTO table1
VALUES (1, 1, 4)
ON DUPLICATE KEY UPDATE
OptionID = VALUES(OptionID)
Demo on dbfiddle
First of all, you should use prepared statements to avoid SQL injections.
If you have a unique key on (ClientID,QuestionID), you can do INSERT INTO ... ON DUPLICATE KEY like this:
INSERT INTO questionform_answer (ClientID,QuestionID,OptionID)
values ('$ClientID','$soruid','$cevapid')
on duplicate key update OptionID='$cevapid'
I have my table schema in H2 db as follows:
create table if not exists Test ( id bigint not null,name varchar(255), primary key (id) );
alter table Test add constraint if not exists Test_NAME UNIQUE (name);
I want to insert a value for the name attribute as 'Default' if it does not exist in the table by selecting the latest id value from the table and increment it by one.
Example:
Do not insert if an entry for name = Default already exists.
ID | Name
1 | Default
Insert if an entry for name = Default does not exists.
ID | Name
1 | ABC
2 | XYZ
For the id column, find the max id and increment it by one. In this case, insert id=3 and name=Default.
My query is as follows:
INSERT INTO Test (id , name)
SELECT max(id) + 1, 'Default' from Test
WHERE NOT EXISTS (SELECT * FROM Test where name='Default');
However, it gives me an error saying:
NULL not allowed for column "ID"; SQL statement
as it applies the where condition on the inner select statement.
I also tried:
MERGE INTO Test KEY(name) VALUES (SELECT MAX(id) + 1 from Test, 'Default');
It gives an error because, merge tries to update with the new values.
If it finds 'Default', it will update the row with new id causing primary key violation.
Is there a better way to do this? How can I make the query work?
You are massively overcomplicating this. Define the id field as auto increment and place a unique index on the name field. The unique index prevents duplicate names to be inserted, while the auto increment increases the value of the id field by 1 (by default) if the insert is successful.
I updated id to auto increment and the following query work flawlessly
INSERT INTO Test (name) select * from (select 'Default') as tmp WHERE NOT EXISTS (SELECT name from Test where name='Default');
when you run your query first time, no record found in table so, it give error 'null' there, so if you add IFNULL() function there as below
INSERT INTO Test (id , name)
SELECT **IFNULL**(max(id),0) + 1, 'Default'
FROM Test
WHERE NOT EXISTS (SELECT * FROM Test where name='Default');
I want to insert a row into my database table if the value of the first column does not exist in the table.
Example:
Name Value1 Value2
------------------------
John 2 3
Max 4 6
Alex 0 0
Now I want to insert a new person with the values 0 and 0 but only if the person does not exist. For example if I tried to insert John it would not do anything. All of this should happen in one single query.
Can anyone help?
Regards, Max
You can create a unique index on table(name) and then use insert ignore or insert on duplicate key update:
create unique index unq_t_name on t(name);
insert into t(name, value1, value2)
values ($Name, $value1, $value2)
on duplicate key update name = values(name);
The on duplicate key is a non-operation -- it does nothing if the name is already in the database.
Create an index on Name, make it unique. Thereafter you will not be able to add records where the name is already in there.
I have CSV files and I need to do something like this before inserting data in my table:
table fields
id = primary id and auto-increment
house_no
city_code
prv_code
cty_code
if (house_no,city_code,prv_code,cty_code) exists = ignore insert
else if (house_no,city_code,prv_code,cty_code) is null = ignore insert
else (house_no,city_code,prv_code,cty_code) !exist = insert
My original code just re-inserts the same values because the primary key id is just creating a new id for it and as a result I have duplicates.
I need to do this to avoid duplicates. I tried INSERT IGNORE and REPLACE but I need a unique key and all fields may have a value which is the same (like they may have different house_no but the same prv_code or cty_code or something like that). I just want to check if the record exist before I insert it.
You can create a unique key over more than one column. In your case you need an unique key containing of the four columns house_no, city_code, prv_code and cty_code.
In your case:
ALTER TABLE fields
ADD CONSTRAINT uc_fieldsUnique UNIQUE (house_no,city_code,prv_code, cty_code);
Load data from CSV-file into second table, and then use INSERT like this to add rows -
INSERT INTO t1(id, house_no, city_code, prv_code, cty_code)
SELECT NULL, t2.house_no, t2.city_code, t2.prv_code, t2.cty_code FROM t2
LEFT JOIN t1 ON t1.house_no = t2.house_no AND t1.city_code = t2.city_code AND t1.prv_code = t2.prv_code AND t1.cty_code = t2.cty_code
WHERE t1.id IS NULL
(rename table names)