◆ data definition language ( DDL )
DDL allows the specification of information about relations, including :
- the schema for each relation
- the domain of values associated with each attribute.
- integrity constraints : 제약조건 (Ex. key)
◆ domain types in SQL
char(n) : 고정길이 문자열. 무조건 n만큼
varchar(n) : 가변 길이. 최대 n만큼
int : integer
smallint : small integer
numeri(p,d) : pixed point number ~ p: 숫자 전체 길이, q: 소수점 자리수
real, double precision : floating point and double-precision floating point number
float(n) : float point number, at least n자리 (참고로 real은 n=24경우)
◆ create table
create table r (a1 d1, a2 d2, ..., an dn,
(integrity constraint),
...
(integrity constraint) )
- integrity constraints
not null
primary key ( a1, ...,an )
foreign key ( a1, ...,an ) references r
(참고) primary key는 자동으로 not null
(참고) null - unknown value
◆ update table
- insert : 튜플 삽입
insert into r ( a1, ...,an ) values ( v1, ...,vn )
- delete : 튜플 삭제
delete from r
where 절을 이용해서 특정 튜플만 지울 수도 있음
- drop table : 테이블 삭제
drop table r
table 삭제 (scheme도 삭제)
- alter : 테이블 변경
alter table r add A D
alter table r drop A
◆ basic structure of SQL queries
- A :attribute / R : relation / P : predicate
- select : select A from R
distinct : 결과중복 x / all : 결과중복o
* : all attributes
arithmetic expression 가능
- where : where condition
and, or, not 과 같은 논리적 연결 가능
- from : from R
cartesian product A x B -> from A, B
- join : where A.id = B.id -> join 연산
◆ additional basic operation
- natural join : select A from R natural join R
자동적으로 join key를 찾아 join
- rename : old_name as new_name
select salary as s
from instruct as T
* as는 옵션이기때문에 생략 가능.
즉, select salary s 가능, from instruct T 가능
- string operation : like
percent(%) : any substring
underscore(_) : any character
\ ~ escape character . ex. \% 하면 % 인식가능
예) select A from R where A like '%abc%'
- ordering : order by a1, ...,an
- default : asc
- desc , asc (오름차 : 작은것~>큰것)
- 기준은 a1, ..., an 순으로
- where predicates : between 비교 연산자
where salary between 9000 and 100000
튜플 비교
where (R1.A1, A2) = (R2.A1, 'ABC');
- duplicates : multiset version ob some of the relational algebra operator
중복되는 값 파악
◆ set operation : union / intersect / except
---> 집합연산 ~ 중복안됨... (select distinct 와 같다)
중복 보이려면 union all / intersect all / except all
◆ null values : unknown value / does not exist
* arithmetic expression involving null is null
* is null can be used to check for null values
-> 반대는 is not null
* unknown comparison
false < unknown < true
OR :
unknown or true = true
unknown or false = unknown
unknown or unknown = unknown
AND :
unknown and true = unknown
unknown and false = flase
unknown and unknown = unknown
NOT:
(not) unknown = unknown
◆ aggregate functions
- avg
- min
- max
- sum
- count : count(*)인 경우에는 null값 포함. 그외에 aggregate는 다 null 제외 연산
collection 이 전부 null일 때, count만 0 리턴. 그외에는 null 리턴
- group by ... having
having : group by의 제약조건
◆ subquery : select-from-where 절이 또다른 쿼리 내에 끼워진 것.
select ~~~ from ~~~ where ~~~ in ( select-from-where )
not in
◆ set comparison
some (at least) : 하나라도 있으면 ok
in과 같음.
그러다 not in과 ≠some은 다르다.
all : 전부 만족해야 ok
예)
select -from - where salary > some ( select-from-where)
◆ test for empty relations
exists
not exists
---> 쿼리에 대한 결과가 empty인지 아닌지
correlated subquery
correlated name / variable
◆ test for absence of duplicate tuples
unique - subquery의 결과 튜플에 중복 튜플이 있는지!
◆ subqueries in the from clause
select - from ( select- from- where) - where
다른 방법 : from R lateral . .
◆ with :
with max_budget(value) as (select salary from r)
select - from - where
◆ scalar subquery
◆ modification of the database
- delete - from - where in (select - from - where)
튜플 삭제 cf. table 삭제 : drop
delete - consistency !!! aggregate function - 질의에 영향
- insert into r values ( a1, ...,an )
scheme 순서에 맞게 / domain에 맞게!
insert into r1 select ( a1, ...,an ) from : r2 두개의 릴레이션을 이용하여 insert 가능
- update r set salary=salary * 1.03 where salary >1000
case문을 이용한 업데이트
update r
set salary = case
when (condition) then salary 1.05
when (condition) then (stmt)
else (stmt)
end
같지않다 <>
'Computers > Databases' 카테고리의 다른 글
seoul accord - day1 (0) | 2012.10.21 |
---|---|
ch5. advanced SQL (0) | 2012.10.21 |
ch4. intermediate SQL (0) | 2012.10.21 |
ch2. intro to relational model (0) | 2012.10.21 |
ch1. introduction (0) | 2012.10.21 |