본문 바로가기

DB

[DB][데이터베이스] 3. SQL / sql의 기본 문법

반응형
  • 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

 

[Fig 0]

- 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)

 

[Fig 1]

         


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 ~)

 

[Fig 2] S.salary 들 중 하나에서만 크면 만족한다 (some)

 

-

[Fig 3]

 

 


Subqueries in the From Clause

- Subqueries

  

[Fig 4]

 

- with Clause

 

[Fig 4] from 에서 사용할  Relation 을  with 으로 임시적으로 선언해준다

 


Subqueries in the Select Clause

- Scalar Subquery 는 단일 값(tuple)로 리턴되어야 한다

 

[Fig 5]

 


Modification of the Database (DML)

- delete

 

[Fig 6] 중간에 평균값이 변하지 않도록 ,  조건에 맞는  tuple 을 찾고 한 번에 삭제한다

 

- insertion

 

[Fig 7]

  

- update

-

[Fig 8]

-

[Fig 9] s_id 10 의  600, 900 course id 의  credit 을 더한 값  / s_id 20 의  700, 900 course id 의  credit 을 더한 값

 


 

연습문제

[Fig 10] practice problems

반응형