구문은 다음과 같습니다 -
insert into yourTableName values(yourValue1,yourValue2,..N) on duplicate key update yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N;
먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1539 -> ( -> EmployeeName varchar(20), -> EmployeeSalary int, -> UNIQUE KEY(EmployeeName) -> ); Query OK, 0 rows affected (0.62 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
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)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1539;
이것은 다음과 같은 출력을 생성합니다 -
+--------------+----------------+ | EmployeeName | EmployeeSalary | +--------------+----------------+ | Robert | 8500 | | David | 5500 | +--------------+----------------+ 2 rows in set (0.00 sec)