이를 위해 UNIQUE KEY를 사용할 수 있습니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1553 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeSalary int -> ); Query OK, 0 rows affected (0.47 sec)
다음은 고유 키를 추가하는 쿼리입니다 -
mysql> alter table DemoTable1553 add unique(EmployeeSalary); Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('Chris',45000); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('David',35000); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('Sam',25000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('Carol',45000); ERROR 1062 (23000): Duplicate entry '45000' for key 'EmployeeSalary'
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1553;
이것은 다음과 같은 출력을 생성합니다 -
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 45000 | | 2 | David | 35000 | | 3 | Sam | 25000 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)