💡 SQL 언어 중 중첩 SQL 기능을 제외한 기본적인 SQL 언어 살펴보기
Pure language
→ 질의어 표현력 동일, 상호 변환 가능
→ 이론적 개발, 상용 시스템에서 구현X
real system language
2023.09.25
CREATE/ALTER/DROP
→ 자체적으로 새로운 데이터 타입명을 정의하여 사용하기도 함
Create table 테이블명 {(속성명, 도메인명) 데이터무결성제약}
Create table professor (
pID char(5),
name varchar(20) not null,
deptName varchar(20),
salary numeric(8,2));
not null : 무결성 제약
문자열은 작은따옴표
관계대수는 큰따옴표
Insert into professor values('10', 'Lee', 'CS', 7500);
Create table professor (
...,
primary key (pID),
foreign key (deptName) references department(deptName));
Create table professor (
...,
constraint myForeignKey foreign key(deptName) references department;
Create table student (
sID char(5) primary key,
name varchar(20) not null,
gender char(1)
deptName varchar(20)
foreign key (deptName) references department),
check (gender in ('F', 'M')));
Drop table student;
Alter table r add A D;
//새로운 속성명(A) 및 속성 타입(D) 명시 필요
//null 값으로 채워짐
Alter table r drop A;
//relation r에서 속성 A 제거
NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK
Drop table student
Delete from student
Insert into course values ('437', 'Database', 'CS', 4);
Insert into course (cID, title, deptName, credit) values ('437', 'Database', 'CS', 4);
Insert into course values ('777', 'no', 'CS', null);
Insert into professor
select * from professor;
//professor 테이블은 tuple 개수가 2배가 된다. 무한루프 X
Delete from professor;
//professor내 모든 tuple 삭제, schema는 존재
Delete from professor where deptName='EE';
Delete from professor
where deptName in (select deptName
from department
where building = 'Vision Hall');
Delete from professor
where salary < (select avg(salary) from professor);
Update professor
set salary = salary * 1.03
where salary > 7000;
Update professor
set salary = salary * 1.05
where salary <= 7000;
//순서 중요
Update professor
set salary = case
when salary <= 7000 then salary*1.05
else salary*1.03
end;
Update student S
set S.totalCredit=
(select sum(credit) //scalar subquery
from takes natural join course
where S.sID = takes.sID and grade <> 'F' and grade is not null);
select
Statementsselect <atrribute list> //필수
from <relation list> //필수
where <selection predicate>
group by <grouping attributes>
having <conditions> //group by절 필수
order by <ordering attributes>;
Select * //모든 속성 의미
from professor;
Select pID, name, salary/12
from pofessor;
distinct
키워드 사용Select distinct deptName //기본값은 all
from professor;
Select name
from professor
where deptName = 'CS' and salary > 8000; //논리연산자 가능
Select *
from professor, teaches;
from
절에 명시된 각 테이블에서 한 개의 tuple 추출where
조건 적용where
절에 명시된 조건에 적용where
조건 우선 적용 후 having
조건 적용group by
절로 전송group by
절 처리 전에, true인 tuple 조합 구하기where
조건 적용 마친 후 group by
적용group by
절에 명시된 속성을 이용 → 중간 결과를 서브그룹으로 분리order by
적용 → 그 결과를 결과 테이블로Select name, cID
from professor, teaches
where professor.pID = teaches.pID; //equi-join
Select *
from professor natural join teaches; //결과 테이블 속성 8개
Select name, title
from (professor natural join teaches) join course using(cID);
Select name, title
from professor natural join teaches, course
where teaches.cID = course.cID;
Select name, title
from teaches, course, professor
where teaches.cID=course.cID and teaches.pID=professor.pID;
professor as T
professor T //as 생략 가능
Select distinct T.name
from professor as T, professor as S //테이블 재명명
where T.salary > S.salary and S.deptName = 'CS';
Select sID, name myName, deptName //속성 재명명
from student;
like
Select name
from professor
where name like '%j_';
//_ : 길이가 1인 임의 스트링(한 문자)
//% : 길이에 무관한 임의 스트링
where title like '100\%' escape '\';
||
+) 대소문자 변환, 스트링 길이, 일부 스트링 추출 등의 기능 제공
order by
: 결과 테이블의 tuple 정렬 → 다른 결과 테이블 생성 X, 기존의 결과 테이블 순서 변경Select distinct name
from professor
order by name;
order by deptName desc, name;
between
값 구간 (경계값 포함)Select name
from professor
where salary between 5000 and 6000;
Select name, cID
from professor, teaches
where (professor.pID, deptName) = (teaches.pID, 'CS');
//where p.pID=t.pID and deptName='CS';
2023.09.28
relational algebra 연산에 대한 multi-set 버전 적용
multi-set 버전으로 처리
중복에 상관없이 모든 tuple에 대해 연산
→ 결과 tuple이 중복에 상관없이 결과 테이블에 포함
union
, intersect
, except
(:= set difference) → 중복 제거(select ... from ... where ...)
union
(select ... from ... where ...)
union all
, intersect all
, except all
→ 결과 테이블에 중복 허용is null
: null 이면 참이다.where salary is null;
unknown
리턴 ex. 21 < null, null <> null, null = null