본문 바로가기
DB

[DB] SQL개념과 분류(DML/DDL/DCL/TCL)

by hotdog7778 2023. 10. 13.

SQL

Structured Query Language 의 약자,

관계형 데이터베이스에 정보를 저장하고 처리하기 위한 프로그래밍 언어,

실행 순서가 없는 비절차적인 언어

 

K21Academy

 

DML (Data Manipulation Language)

 - 데이터 조작어

 - 데이터 조회, 삽입, 수정, 삭제 등의 명령어

 - SELECT, INSERT, UPDATE, DELETE

 

 

DDL (Data Definition Language)

 - 데이터 정의어

 - 테이블이나 릴레이션의 구조를 생성하는데 사용하는 명령어

 - CREATE, ALTER, DROP, TRUNCATE

 

 

DCL (Data Control Language)

 - 데이터 제어어

 - DB에 접근하고 객체들을 사용하도록 권한을 주거나 회수하는 명령어

 - GRANT, REVOKE

 

 

TCL (Transaction Control Language)

 - 데이터 조작어

 - 트랜잭선을 제어하는 명령어

 - COMMIT, ROLLBACK, SAVEPOINT

 

 

 

자주 사용하는 간단한 명령어

-- 1. CREATE

-- DB 생성
create database sesac default character set utf8 collate utf8_general_ci;

-- TABLE 생성
create table orders (
		orderid int primary key auto_increment,
    custid char(10) not null,
    prodname char(6) not null,
    price int not null,
    amount smallint not null,
    foreign key (custid) references customer(custid) on update cascade on delete cascade
);


-- 2. DROP & TRUNCATE

-- DB 삭제
drop database sesac;
-- 테이블의 튜플의 구조와 데이터 삭제
truncate table product;


-- 3. ALTER

-- 컬럼 추가
alter table product add new_column date;

-- 컬럼 자료형 수정
alter table product modify new_column int;

-- 컬럼명 변경
alter table product change new_column changed_column int;

-- 컬럼 삭제
alter table product drop changed_column;


-- 4. INSERT
insert into [테이블명] (컬럼명, 컬럼명, .. 컬럼명) 
	values ('데이터1', '데이터2', .. '데이터7');

-- 속성을 지정하지 않고 순서 맞춰서 insert 
insert into customer 
	values ('happy', '최시은', '일본 오키나와');
    
-- 여러행 동시 추가
insert into customer values 
	('happyA', '길동홍', '대한민국 서울'),
	('happyB', '김철수', '대한민국 대구'),
    ('happyC', '김영희', '대한민국 울산');
    
    
-- 5. UPDATE
-- 컬럼명2가 happy 인 고객을 찾고 그 튜플에서 컬럼명1 의 데이터를 대한민국 서울로 업데이트
update [테이블명] set [변경할 컬럼명1] = '대한민국 서울' 
where [컬럼명2] = 'happy';


-- 6. DELETE
DELETE FROM 테이블명 WHERE 컬럼명 = '블라블라';

-- custid가 happy 인 고객의 데이터를 삭제
delete from customer where custid = 'happy';


-- 7. SELECT

-- 테이블의 모든 필드 조회
select * from orders

-- 테이블에서 여러개 필드 선택 조회
select order_no, created_at from orders

-- (where) orders 테이블에서 결제수단이 카카오페이인 데이터만
select * from orders where payment_method = 'kakaopay';