- Data Definition
- 기본 Query Structure
- 추가 Basic Operations
- Set Operations
- Null Values
- Aggregate Functions
- Subqueries in the Where Clause
- Subqueries in the From Clause
- Subqueries in the Select Clause
- Modification of the Database (DML)
by Prof. Chang Hwan Lee
Data Definition
- DDL; Data Definition Language
- 데이터 정의어 DDL은 데이터를 생성, 수정, 삭제 등 골격을 결정한다.
- Create table
- Domain Types
- not null / primary key / foreign key - references
- Update Tables
- insert into ~ values ~
- delete from ~
- drop table ~
- alter ~ add / modify(자료형 변경) / change(이름 변경) / drop(삭제) ~
- DML; Data Manipulation Language
- 데이터 조작어 DML은 데이터베이스에 입력된 레코드를 조회, 수정, 삭제 등을 한다.
- Updates to tables (Deletion / Insertion / Updating / Deleting) : CRUD
- 아래 Modification of the Database 에서 설명된다.
기본 Query Structure
- Attribute(select) / Relation(from) / Predicate(where)
select (distinct, 중복제거 / all) A (as ~)
from R (as ~)
where P (and / or / not / between ~ and ~)
- +, -, *, / 로 수식표현도 가능
- Nested Subqueries 표현이 가능하다
추가 Basic Operations
- (natural) join
- String Operations
- substring ‘%’ / character ‘_’ / escape ‘\’ / concatenation 연쇄 “||” / …
where name like '%dar%'
- order by ~ (desc)
- Tuple comparison
where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);
Set Operations
-
or: (select from where) union (select from where)
and: (select from where) intersect (select from where)
but not: (select from where) except (select from where)
Null Values
ex)
where salary is null
- null 과의 비교연산은 unknown을 리턴한다
- false < (!)unknown < true
- null 을 쓸 때의 문제점
null 값을 자동으로 검색하는 함수를 사용할 수 있지만, null을 제거하기 위해서는 사용자 정의 함수를 만들어야 하기 떄문에 복잡하다. 또한, null이 가변 길이 값으로 인식되어 불필요한 메모리 공간을 사용한다. 하지만, 하드 드라이브 공간 확보가 쉬워졌고, null 값으로 논리가 향상되므로 일관되게 사용하는 것이 좋다.
- NULL이 포함된 비교 연산에 대한 결과 값이 NULL이다.
- 산술연산에 대한 결과 값이 NULL이다.
- Aggregate function을 이용한 평균 연봉을 구할 때, avg를 이용하는 경우와 sum과 count를 이용하는 경우에 한해 결과 값이 다를 수 있다.
Aggregate Functions
- avg / min / max / sum / count
- count(*)를 제외하고는 null은 빼고 계산한다
- group by: 특정 칼럼 그룹화
- select에 그대로 가져와야 한다
- having: 그룹화 한 칼럼에 특정 조건 (그룹화의 where)
Subqueries in the Where Clause
- (not) in / some / all / (not) exists / unique
-
ex)
where salary > some (select ~ from ~ where ~)
-
Subqueries in the From Clause
- Subqueries
- with Clause
Subqueries in the Select Clause
- Scalar Subquery 는 단일 값(tuple)로 리턴되어야 한다
Modification of the Database (DML)
- delete
- insertion
- update
-
-
연습문제
'DB' 카테고리의 다른 글
[DB][데이터베이스] 7-1. DB Design / DB 설계 (0) | 2021.11.29 |
---|---|
[DB][데이터베이스] 5. Advanced SQL / 고급 sql (0) | 2021.11.26 |
[DB][데이터베이스] 4. Intermediate SQL / 중간 단계의 sql (0) | 2021.11.26 |
[DB][데이터베이스] 2. E-R / Entity와 Relationship (0) | 2021.10.08 |
[DB][데이터베이스] 1. Database Systems / 데이터베이스 시스템 (0) | 2021.10.07 |