SQL INSERT if username doesn't exist - mysql

what is wrong with this query:
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password)
WHERE NOT EXISTS (
SELECT `username` FROM `customers` WHERE `username`=:username
);

What's wrong with the query? It generates a syntax error.
If you want to prevent duplicates, then use a unique constraint on the table:
ALTER TABLE customers ADD CONSTRAINT unq_customers_username UNIQUE (username);
Then if you attempt to insert a duplicate value using
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password);
You will get an error of a constraint violation.
Often in this case, you want to actually update the password in the existing row. You can do that using the MySQL extension ON DUPLICATE KEY UPDATE:
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password)
ON DUPLICATE KEY UPDATE password = :password;

Related

INSERT ... ON DUPLICATE KEY UPDATE does not work in my DB

I created a table entries with UNIQUE KEY title. Now I want to execute the query INSERT ... ON DUPLICATE KEY UPDATE, but I get compilation errors in phpMyAdmin:
Query:
INSERT INTO `entriess` (`title`, `description`)
VALUES ("TEST", "TEST")
ON DUPLICATE KEY UPDATE `title`=`AAA`
What am I doing wrong?
You have a syntax error. Change the query to
INSERT INTO `entriess` (`title`, `description`)
VALUES ('TEST', 'TEST')
ON DUPLICATE KEY UPDATE `title`='AAA'
In MySQL we enclose varchars into apostrophes, not into quotes.

ON DUPLICATE KEY unrecognized in phpmyadmin

I have a mysql database hosted on a remote server, I'm using InnoDB. Everything else works fine but on phpmyadmin whenever I try to execute the following query
INSERT INTO User (user_id, location) VALUES (1, 'London') ON DUPLICATE KEY UPDATE location=VALUES('London')
It highlights ON, DUPLICATE, KEY because they're unrecognized keywords
Help please!
Please remove the VALUES(...) in your ON DUPLICATE KEY part.
INSERT INTO User (user_id, location) VALUES (1, 'London') ON DUPLICATE KEY UPDATE location = 'London'
If you want to update more than one column, you should use normal UPDATE syntax:
INSERT INTO User (firstColumn, secondColumn) VALUES (1, 'London') ON DUPLICATE KEY UPDATE firstColumn = 'ABC', secondColumn = 'DEF';
Before using ON DUPLICATE KEY you should add index for one or multiple column. You can use following command for adding new index.
ALTER TABLE `table_name` ADD UNIQUE `unique_index`(`column1`);
For multiple column index use following command.
ALTER TABLE `table_name` ADD UNIQUE `unique_index`(`column1`, `column2`);
Now you can INSERT/UPDATE one or more row together.
For inserting single row use following command.
INSERT INTO `table_name` (`column1`,`column2`,`column3`,`column4`) VALUES('value1','value2','value3','value4') ON DUPLICATE KEY UPDATE `column3`='value3', `column4`='value4';
OR
INSERT INTO `table_name` (`column1`,`column2`,`column3`,`column4`) VALUES('value1','value2','value3','value4') ON DUPLICATE KEY UPDATE `column3`=VALUES(`column3`), `column4`=VALUES(`column4`);
Using this command you can also INSERT/UPDATE multiple rows. Use following command for this purpose.
INSERT INTO `table_name` (`column1`,`column2`,`column3`,`column4`) VALUES('value11','value12','value13','value14'),('value21','value22','value23','value24') ON DUPLICATE KEY UPDATE `column3`=VALUES(`column3`), `column4`=VALUES(`column4`);

How to write MySQL query to find foreign key entity exists?

I need to update the customer details in my table
If client_id exists in the client table and client_id exists in customer table I will update if not insert, using the following query
INSERT INTO `customer` (`username`, `password`, `client_id`)
VALUES ('username', 'password', 1)
ON DUPLICATE KEY UPDATE `password` = 'user-password';
I need to skip update and insert when client_id is empty(id not present in client table).
How to write MySQL query to skip this?
Try using REPLACE instead of INSERT. If the PRIMARY KEY exists, it would replace the row, but if not, then it would do the insert as usual.
REPLACE INTO `customer` (`username`, `password`, `client_id`)
VALUES ('username', 'password', 1);

Mysql issue on form insert

When I insert thing in my table via phpmyadmin I have no problem, but when I try to do it in the form I have created as "admin-panel" in my site i get this message:
Error: Cannot add or update a child row: a foreign key constraint fails (`db467610239`.`articulo`, CONSTRAINT `fk_articulo_genero` FOREIGN KEY (`genero_id`) REFERENCES `genero` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Thing is the number I insert into my new table already exists in my other table as they are linked as genre. I don't know why this is happening, i'm using exactly the same insert as in phpmyadmin just with some php tweaks:
INSERT INTO articulo VALUES (id= null , nombre='$nombre', imagen='$imagen', text='$text', precio='$precio', popup='$popup', genero_id ='$genero_id')
Your query should look more like this:
$query = "
INSERT INTO articulo (
nombre, imagen, text, precio, popup, genero_id
) VALUES (
'$nombre', '$imagen', '$text', '$precio', '$popup', $genero_id
)";

MySQL bulk insert (ignoring primary key id, but on duplicate update another key)

On a bulk insert, how do I ignore a duplicate key update for primary key 'id' while only updating the entry for another key 'another_unique_id'?
INSERT INTO
some_table (`id`, `another_unique_id`, `raw_data)
VALUES (5 , 'ABCDEFG', 'blah')
ON DUPLICATE KEY UPDATE ...?
For example -
If there's a record with id: 5, I don't want to update it. I'll just insert a new record.
If there's a record with 'ABCDEFG' in the 'another_unique_id' field, then I'd like to update that entry to 'blah'
I think you should pass
INSERT INTO
some_table (`id`, `another_unique_id`, `raw_data)
VALUES (NULL , 'ABCDEFG', 'blah')
ON DUPLICATE KEY UPDATE ...?
otherwise it doesn't make sense
At least on mysql you can use:
INSERT IGNORE INTO
some_table (`id`, `another_unique_id`, `raw_data`)
VALUES (5 , 'ABCDEFG', 'blah')
If a field with id 5 exists it will do the same as an update,
and if it doesn't exist, it will do the same as the regular insert.