-
*데이터 삽입
insert into [테이블 명]([값을 넣을 컬럼 명, ...]) values ([컬럼에 넣을 값, ...]);
-> 컬럼 명 생략 가능(모든 컬럼에 데이터가 들어갈 경우) -> 추천하지 않음
*데이터 수정
update [테이블 명] set [컬럼 명]=[값], ... where [조건];
*데이터 삭제
delete from [테이블 명] where [조건];
*upsert
값이 없으면 insert, 있으면 update -> 반드시 unique 키나 기본키가 걸려있어야 한다.
insert into [테이블 명]([값을 넣을 컬럼 명, ...]) values ([컬럼에 넣을 값, ...])
on duplicate key update [컬럼]=[값], ...;
*특정 컬럼 조회
select [컬럼 명, ...] from [테이블 명];
-> 보여주는 컬럼 순서는 변경 가능하다.
desc employees; -- 1. 데이터 삽입 -- INSERT INTO [테이블 명]([값을 넣을 컬럼 명]) VALUES ([컬럼에 넣을 값, ...]); insert into employees ( emp_no, first_name, family_name, email, mobile, salary, depart_no, commission ) values ( 111, '길동', '홍', 'asd123@email.com', 01012345678, 9000000, 'dev001', 90 ); select * from employees; -- 컬럼 명 생략(모든 컬럼에 데이터가 들어갈 경우. 단, 자동으로 들어가는 컬럼은 제외) -- 추천하지 않음 insert into employees ( emp_no, first_name, family_name, email, mobile, salary, depart_no, commission ) values ( 112, '철수', '홍', 'asd123@email.com', 01012345678, 9000000, 'dev001', 90 ); -- 전체 데이터를 넣지않고 일부 데이터만 넣을 수 있다. insert into employees ( emp_no, family_name, email, mobile, salary ) values ( 113, '홍', 'asd123@email.com', 01012345678, 9000000 ); select * from employees; -- 2. 데이터 수정 -- UPDATE [테이블 명] SET [컬럼 명]=[값], ... WHERE [조건]; update employees set depart_no='dev002' where depart_no is null; -- commission이 null인 데이터를 찾아 commission을 10으로 수정 update employees set commission=10 where commission is null; -- 3. 데이터 삭제 -- DELETE FROM [테이블 명] WHERE [조건]; delete from employees where first_name is null; -- 4. UPSERT : 값이 없으면 INSERT, 있으면 UPDATE -- 반드시 unique 키나 기본 키가 걸려 있어야 한다. -- INSERT INTO [테이블 명]([컬럼 명, ...]) VALUES ([값, ...]) -- ON DUPLICATE KEY UPDATE [컬럼]=[값], ...; insert into employees (emp_no, first_name, family_name, email, mobile, salary) values (112, '태근', '김', 'email@naver.com', 01011112222, 7000000) on duplicate key update first_name='태곤', family_name='박';
'SQL' 카테고리의 다른 글
SQL - 제약조건 (0) 2022.12.10 SQL - Transaction (0) 2022.12.10 SQL - select문에서의 여러 사용법 (0) 2022.10.07 SQL - DDL (0) 2022.10.07 SQL - USER 관리, DCL (1) 2022.10.07