¿Corregir MySQL INSERT... EN la sintaxis de CLAVE DUPLICADA?
La sintaxis es la siguiente:
insert into yourTableName values(yourValue1,yourValue2,..N)
on duplicate key update yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N;
Primero creemos una tabla:
mysql> create table DemoTable1539
-> (
-> EmployeeName varchar(20),
-> EmployeeSalary int,
-> UNIQUE KEY(EmployeeName)
-> );
Query OK, 0 rows affected (0.62 sec)
Inserte algunos registros en la tabla usando el comando insertar:
mysql> insert into DemoTable1539 values('Chris',4500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500;
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1539 values('David',5500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500;
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1539 values('Chris',4500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500;
Query OK, 2 rows affected (0.14 sec)
Muestre todos los registros de la tabla usando la declaración de selección:
mysql> select * from DemoTable1539;
Esto producirá el siguiente resultado:
+--------------+----------------+
| EmployeeName | EmployeeSalary |
+--------------+----------------+
| Robert | 8500 |
| David | 5500 |
+--------------+----------------+
2 rows in set (0.00 sec)