3. SQL
SQL Parts
- DDL (Data Definition Language, λ°μ΄ν° μ μμ΄)
- λ°μ΄ν°λ² μ΄μ€ μ€ν€λ§(schema) μ μ μλ¨ μ 곡
- DML (Data Manipulation Language, λ°μ΄ν° μ‘°μμ΄)
- λ°μ΄ν°λ² μ΄μ€ μ 보 μ§μ λ° νν(tuple) μ½μ , μμ , μμ κΈ°λ₯ μ 곡
- Integrity (무결μ±)
- DDLμ λ¬΄κ²°μ± μ μ½ μ‘°κ±΄(integrity constraints) λͺ μ λͺ λ Ήμ΄ ν¬ν¨
- View definition (λ·° μ μ)
- DDLμ λ·°(view) μ μ λͺ λ Ήμ΄ ν¬ν¨
- Transaction control (νΈλμμ
μ μ΄)
- νΈλμμ (transaction)μ μμκ³Ό λμ λͺ μνλ λͺ λ Ήμ΄ ν¬ν¨
- Embedded SQL and dynamic SQL (embedded SQL λ° λμ SQL)
- λ²μ© νλ‘κ·Έλλ° μΈμ΄ λ΄μ SQL λ¬Έμ₯μ μ½μ νλ λ°©λ² μ μ
- Authorization (κΆν λΆμ¬)
- κ΄κ³(relation) λ° λ·°μ λν μ κ·Ό κΆν λͺ μ λͺ λ Ήμ΄ ν¬ν¨
Data Definition Language
- SQL DDLμ κ΄κ³μ λν μ 보 λͺ
μ νμ©, λ€μ ν¬ν¨:
- κ° κ΄κ³μ μ€ν€λ§
- κ° μμ±(attribute)κ³Ό κ΄λ ¨λ κ°μ μ ν
- λ¬΄κ²°μ± μ μ½ μ‘°κ±΄
- κ° κ΄κ³μ λν΄ μ μ§λ μΈλ±μ€(index) μ§ν©
- κ° κ΄κ³μ λν 보μ λ° κΆν μ 보
- λμ€ν¬ μμ κ° κ΄κ³μ λν 물리μ μ μ₯ ꡬ쑰
DDL: Domain Types in SQL
char(n): μ¬μ©μ μ§μ κΈΈμ΄ μ κ³ μ κΈΈμ΄ λ¬Έμμ΄varchar(n): μ¬μ©μ μ§μ μ΅λ κΈΈμ΄ μ κ°λ³ κΈΈμ΄ λ¬Έμμ΄int: μ μ (κΈ°κ³μ μμ‘΄νλ μ μμ μ ν λΆλΆ μ§ν©)smallint: μμ μ μ (μ μ λλ©μΈ μ νμ κΈ°κ³ μμ‘΄μ λΆλΆ μ§ν©)numeric(p,d): μ리μ μ¬μ©μ μ§μ μ λ°λμ μμμ μ΄ν μ리λ₯Ό κ°μ§ κ³ μ μμμ μ«μ (μ:numeric(3,1)μ 44.5λ μ νν μ μ₯ κ°λ₯νλ, 444.5λ 0.32λ λΆκ°)real,double precision: κΈ°κ³ μμ‘΄μ μ λ°λλ₯Ό κ°μ§ λΆλ μμμ λ° λ°°μ λ°λ λΆλ μμμ μ«μfloat(n): μ΅μ μ리μ μ¬μ©μ μ§μ μ λ°λλ₯Ό κ°μ§ λΆλ μμμ μ«μ- μΆκ°μ μΈ λ΄μ©μ 4μ₯μμ λ€λ£Έ
DDL: Create Table Construct
- SQL κ΄κ³λ
create tableλͺ λ Ήμ΄λ₯Ό μ¬μ©νμ¬ μ μ:create table r (A1 D1, A2 D2, ..., An Dn, (integrity-constraint1), ..., (integrity-constraintk));
- μ κ΄κ³μ μ΄λ¦
- κ° λ κ΄κ³ μ μ€ν€λ§μ μλ μμ± μ΄λ¦
- λ μμ± μ λλ©μΈ(domain)μ μλ κ°μ λ°μ΄ν° μ ν
- μμ:
create table instructor( ID char(5), name varchar(20), dept_name varchar(20), salary numeric(8,2) );
DDL: Integrity Constraints in Create Table
- λ¬΄κ²°μ± μ μ½ μ‘°κ±΄μ μ ν
primary key(A1, ..., An)- μ΄ κ΄κ³μ λͺ¨λ ννμ μλ‘ λ€λ₯Έ κΈ°λ³Έ ν€ κ°μ κ°μ ΈμΌ ν¨
- μμ Primary-key constraint (κΈ°λ³Έ ν€ μ μ½ μ‘°κ±΄) κ°μ
foreign key (Am, ..., An) references r- Foreign-key constraint (μΈλ ν€ μ μ½ μ‘°κ±΄) κ°μ
not null- μ΄ μμ±μ΄ NULL κ°μ κ°μ§ μ μλλ‘ κ°μ
- SQLμ λ¬΄κ²°μ± μ μ½ μ‘°κ±΄μ μλ°νλ λ°μ΄ν°λ² μ΄μ€ μ λ°μ΄νΈ λ°©μ§
- μμ:
create table instructor( ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2), primary key (ID), foreign key (dept_name) references department );
Updates to tables
- Insert
insert into instructor values('10211', 'Smith', 'Biology', 66000);
- Delete
studentκ΄κ³μμ λͺ¨λ νν μ κ±°delete from student;
- Drop Table
drop table r;
- Alter
alter table r add A D;- μ¬κΈ°μ λ κ΄κ³ μ μΆκ°λ μμ±μ μ΄λ¦μ΄κ³ λ μ λλ©μΈ
- κ΄κ³μ λͺ¨λ κΈ°μ‘΄ ννμ μ μμ±μ λν΄
nullκ°μΌλ‘ ν λΉλ¨
alter table r drop A;- μ¬κΈ°μ λ κ΄κ³ μ μμ± μ΄λ¦
- μμ± μμ λ λ§μ λ°μ΄ν°λ² μ΄μ€μμ μ§μνμ§ μμ
Basic Query Structure
- μΌλ°μ μΈ SQL μ§μ νν:
select A1, A2, ..., An from r1, r2, ..., rm where P;
- λ μμ±μ λνλ
- λ κ΄κ³λ₯Ό λνλ
- λ μ μ΄(predicate)
- λμνλ κ΄κ³ λμ(relational algebra)
- SQL μ§μμ κ²°κ³Όλ κ΄κ³
The select Clause
selectμ μ μ§μ κ²°κ³Όμμ μνλ μμ±μ λμ΄- κ΄κ³ λμμ νλ‘μ μ (projection) μ°μ°μ ν΄λΉ
- μμ: λͺ¨λ κ°μ¬μ μ΄λ¦ μ°ΎκΈ°
select name from instructor;
- μ°Έκ³ : SQL μ΄λ¦μ λμλ¬Έμλ₯Ό ꡬλΆνμ§ μμ (μ:
NameNAMEname) - SQLμ κ΄κ³λΏλ§ μλλΌ μ§μ κ²°κ³Όμμλ μ€λ³΅μ νμ©
- μ€λ³΅ μ κ±°λ₯Ό κ°μ νλ €λ©΄
selectλ€μdistinctν€μλ μ½μ- λͺ¨λ κ°μ¬μ νκ³Ό μ΄λ¦μ μ°Ύκ³ μ€λ³΅ μ κ±°
select distinct dept_name from instructor;
allν€μλλ μ€λ³΅μ΄ μ κ±°λμ§ μμμΌ ν¨μ λͺ μselect all dept_name from instructor;
selectμ μ λ³ν(*)λ "λͺ¨λ μμ±"μ μλ―Έselect * from instructor;select instructor.* from instructor, teaches;
- μμ±μ
fromμ μμ΄ λ¦¬ν°λ΄(literal)μ΄ λ μ μμselect '437';- κ²°κ³Όλ "437" κ°μ κ°μ§ λ¨μΌ νκ³Ό νλμ μ΄μ κ°μ§ ν μ΄λΈ
- λ€μμ μ¬μ©νμ¬ μ΄μ μ΄λ¦ λΆμ¬ κ°λ₯:
select '437' as FOO;
- μμ±μ
fromμ κ³Ό ν¨κ» 리ν°λ΄μ΄ λ μ μμselect 'A' from instructor;- κ²°κ³Όλ νλμ μ΄κ³Ό κ°μ ν(
instructorν μ΄λΈμ νν μ)μ κ°μ§ ν μ΄λΈμ΄λ©°, κ° νμ κ°μ "A"
selectμ μ+,-,*,/μ°μ°μ ν¬ν¨νλ μ°μ ννμμ ν¬ν¨ν μ μμΌλ©°, μμ λλ ννμ μμ±μ λν΄ μ°μ°- μ§μ:
select ID, name, dept_name, salary/12 from instructor;instructorκ΄κ³μ λμΌνμ§λ§salaryμμ± κ°μ΄ 12λ‘ λλμ΄μ§ κ΄κ³λ₯Ό λ°ν- μΌλ°νλ νλ‘μ μ μ°μ°μ ν΄λΉ
asμ μ μ¬μ©νμ¬ "salary/12"μ μ΄λ¦ λ³κ²½ κ°λ₯:select ID, name, dept_name, salary/12 as monthly_salary;
The where Clause
whereμ μ κ²°κ³Όκ° λ§μ‘±ν΄μΌ νλ 쑰건μ λͺ μ- κ΄κ³ λμμ μ ν(selection) μ μ΄μ ν΄λΉ
- μ»΄ν¨ν° 곡ν(Comp. Sci.)κ³Όμ λͺ¨λ κ°μ¬ μ°ΎκΈ°
select name from instructor where dept_name = 'Comp. Sci.';
- SQLμ λ
Όλ¦¬μ μ°κ²°μ¬
and,or,notμ¬μ©μ νμ© - λ
Όλ¦¬μ μ°κ²°μ¬μ νΌμ°μ°μλ λΉκ΅ μ°μ°μ
<,<=,>,>=,=,<>λ₯Ό ν¬ν¨νλ ννμμ΄ λ μ μμ - λΉκ΅λ μ°μ ννμμ κ²°κ³Όμ μ μ© κ°λ₯
- κΈμ¬κ° 70000λ³΄λ€ ν° μ»΄ν¨ν° 곡νκ³Όμ λͺ¨λ κ°μ¬ μ°ΎκΈ°
select name from instructor where dept_name = 'Comp. Sci.' and salary > 70000;
The from Clause
fromμ μ μ§μμ κ΄λ ¨λ κ΄κ³λ₯Ό λμ΄- κ΄κ³ λμμ μΉ΄ν°μ νλ‘λνΈ(Cartesian product) μ°μ°μ ν΄λΉ
instructorteachesμ μΉ΄ν°μ νλ‘λνΈ μ°ΎκΈ°select * from instructor, teaches;
- κ°λ₯ν λͺ¨λ
instructor-teachesμμ μμ±νλ©°, λ κ΄κ³μ λͺ¨λ μμ±μ ν¬ν¨ - κ³΅ν΅ μμ±(μ:
ID)μ κ²½μ°, κ²°κ³Ό ν μ΄λΈμ μμ±μ κ΄κ³ μ΄λ¦μ μ¬μ©νμ¬ μ΄λ¦μ΄ λ³κ²½λ¨ (μ:instructor.ID) - μΉ΄ν°μ νλ‘λνΈλ μ§μ μ μΌλ‘λ κ·Έλ€μ§ μ μ©νμ§ μμ§λ§,
whereμ 쑰건(κ΄κ³ λμμ μ ν μ°μ°)κ³Ό κ²°ν©νλ©΄ μ μ©
Examples
- μ΄λ€ κ³Όλͺ©μ κ°λ₯΄μΉ λͺ¨λ κ°μ¬μ μ΄λ¦κ³Ό
course_idμ°ΎκΈ°select name, course_id from instructor, teaches where instructor.ID = teaches.ID;
- μμ (Art) νκ³Όμμ μ΄λ€ κ³Όλͺ©μ κ°λ₯΄μΉ λͺ¨λ κ°μ¬μ μ΄λ¦κ³Ό
course_idμ°ΎκΈ°select name, course_id from instructor, teaches where instructor.ID = teaches.ID and instructor.dept_name = 'Art';
- SQLμ μ€λ³΅μ νμ©ν¨
The Rename Operation
- SQLμ
asμ μ μ¬μ©νμ¬ κ΄κ³μ μμ±μ μ΄λ¦ λ³κ²½μ νμ©:old-name as new-name
- λ§μ½
new-nameμ΄ μμ±μ΄ μλ κ΄κ³λ₯Ό μν κ²μ΄λΌλ©΄, 'μκ΄ μ΄λ¦(correlation name)'(SQL νμ€), 'ν μ΄λΈ λ³μΉ(table alias)', 'μκ΄ λ³μ(correlation variable)' λλ 'νν λ³μ(tuple variable)'λΌκ³ λΆλ¦Ό - 'Comp. Sci.'μ μλ μ΄λ€ κ°μ¬λ³΄λ€ λμ κΈμ¬λ₯Ό λ°λ λͺ¨λ κ°μ¬μ μ΄λ¦ μ°ΎκΈ°:
select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Comp. Sci.';
asν€μλλ μ ν μ¬νμ΄λ©° μλ΅ κ°λ₯ (instructor as Tinstructor T)
String Operations
- SQLμ λ¬Έμμ΄ λΉκ΅λ₯Ό μν λ¬Έμμ΄ λ§€μΉ μ°μ°μ
likeλ₯Ό ν¬ν¨νλ©°, λ κ°μ νΉμ λ¬Έμλ₯Ό μ¬μ©ν ν¨ν΄μ μ΄μ©:- νΌμΌνΈ (
%): λͺ¨λ νμ λ¬Έμμ΄κ³Ό μΌμΉ - λ°μ€ (
_): λͺ¨λ λ¨μΌ λ¬Έμμ μΌμΉ
- νΌμΌνΈ (
- μ΄λ¦μ "dar" νμ λ¬Έμμ΄μ ν¬ν¨νλ λͺ¨λ κ°μ¬μ μ΄λ¦ μ°ΎκΈ°
select name from instructor where name like '%dar%';
- λ¬Έμμ΄ "100%"μ μΌμΉμν€κΈ°
like '100\%' escape '\'- μ μμμμ λ°±μ¬λμ(
\)λ₯Ό μ΄μ€μΌμ΄ν λ¬Έμλ‘ μ¬μ© (λ€λ₯Έ μ΄λ€ λ¬Έμλ μ΄μ€μΌμ΄ν λ¬Έμλ‘ μ¬μ© κ°λ₯)
- ν¨ν΄μ λμλ¬Έμλ₯Ό ꡬλΆ
- ν¨ν΄ λ§€μΉ μμ:
'Intro%': "Intro"λ‘ μμνλ λͺ¨λ λ¬Έμμ΄κ³Ό μΌμΉ'%Comp%': "Comp"λ₯Ό νμ λ¬Έμμ΄λ‘ ν¬ν¨νλ λͺ¨λ λ¬Έμμ΄κ³Ό μΌμΉ'_ _ _': μ νν μΈ λ¬Έμλ‘ λ λͺ¨λ λ¬Έμμ΄κ³Ό μΌμΉ'_ _ _ %': μ΅μ μΈ λ¬Έμλ‘ λ λͺ¨λ λ¬Έμμ΄κ³Ό μΌμΉ
- SQLμ λ€μν λ¬Έμμ΄ μ°μ° μ§μ
- μ°κ²° (
||μ¬μ©) - λμλ¬Έμ λ³ν
- λ¬Έμμ΄ κΈΈμ΄ μ°ΎκΈ°, νμ λ¬Έμμ΄ μΆμΆ λ±
- μ°κ²° (
Ordering the Display of Tuples
- λͺ¨λ κ°μ¬μ μ΄λ¦μ μνλ²³ μμΌλ‘ λμ΄
select distinct name from instructor order by name;
- κ° μμ±μ λν΄ λ΄λ¦Όμ°¨μμ
desc, μ€λ¦μ°¨μμascλ₯Ό λͺ μ κ°λ₯νλ©°, μ€λ¦μ°¨μμ΄ κΈ°λ³Έκ°- μμ:
order by name desc
- μμ:
- μ¬λ¬ μμ±μ λν΄ μ λ ¬ κ°λ₯
- μμ:
order by dept_name desc, name asc
- μμ:
Where Clause Predicates
- SQLμ
betweenλΉκ΅ μ°μ°μλ₯Ό ν¬ν¨ - μμ: κΈμ¬κ° $90,000μμ $100,000 μ¬μ΄μΈ λͺ¨λ κ°μ¬μ μ΄λ¦ μ°ΎκΈ° (μ¦, $90,000 μ΄κ³ )
select name from instructor where salary between 90000 and 100000;
- νν λΉκ΅
select name, course_id from instructor, teaches where (instructor.ID, dept_name) = (teaches.ID, 'Biology');
- μλ μλμ λμΌ
select name, course_id from instructor, teaches where instructor.ID = teaches.ID and dept_name = 'Biology';
Set Operations
- μ§ν© μ°μ°
union,intersect,except - κ° μ°μ°μ μλμΌλ‘ μ€λ³΅μ μ κ±°
- λͺ¨λ μ€λ³΅μ μ μ§νλ €λ©΄
union all(λλintersect allνΉμexcept all) μ¬μ© - 2017λ
κ°μ λλ 2018λ
λ΄μ κ°μ€λ κ³Όλͺ© μ°ΎκΈ°
(select course_id from section where sem = 'Fall' and year = 2017) union (select course_id from section where sem = 'Spring' and year = 2018);
- 2017λ
κ°μκ³Ό 2018λ
λ΄μ λͺ¨λ κ°μ€λ κ³Όλͺ© μ°ΎκΈ°
(select course_id from section where sem = 'Fall' and year = 2017) intersect (select course_id from section where sem = 'Spring' and year = 2018);
- 2017λ
κ°μμλ κ°μ€λμμ§λ§ 2018λ
λ΄μλ κ°μ€λμ§ μμ κ³Όλͺ© μ°ΎκΈ°
(select course_id from section where sem = 'Fall' and year = 2017) except (select course_id from section where sem = 'Spring' and year = 2018);
Aggregate Functions
- μ΄ ν¨μλ€μ κ΄κ³ λ΄ κ°λ€μ 컬λ μ
(μ§ν© λλ λ€μ€ μ§ν©)μ λν΄ μλνλ©° λ¨μΌ κ°μ λ°ν
avg: νκ· κ° (μ«μ κ°μλ§ ν΄λΉ)min: μ΅μκ° (μ«μ λ° λΉμ«μ κ° λͺ¨λ ν΄λΉ)max: μ΅λκ° (μ«μ λ° λΉμ«μ κ° λͺ¨λ ν΄λΉ)sum: κ°μ ν©κ³ (μ«μ κ°μλ§ ν΄λΉ)count: κ°μ κ°μ (μ«μ λ° λΉμ«μ κ° λͺ¨λ ν΄λΉ)
- SQLμμ
countλ₯Ό μ μΈν μ§κ³ ν¨μ(aggregate functions)λnullκ°μ 무μ nullκ°μ΄ 무μλ κ²°κ³Όλ‘ κ°λ€μ 컬λ μ μ΄ λΉμ΄ μμ μ μμ- λͺ¨λ μ§κ³ ν¨μ(
countμ μΈ)λ λΉ μ»¬λ μ μ μ μ©λ λnullμ λ°ν (λΉ μ»¬λ μ μcountλ 0μΌλ‘ μ μλ¨) countν¨μλ λ€λ₯Έnon-nullκ°μ²λΌ λͺ¨λnullκ°μ κ³μ°
Aggregate Functions Examples
- μ»΄ν¨ν° κ³Όν νκ³Ό κ°μ¬λ€μ νκ· κΈμ¬ μ°ΎκΈ°
select avg(salary) from instructor where dept_name = 'Comp. Sci.';
- 2018λ
λ΄ νκΈ°μ κ³Όλͺ©μ κ°λ₯΄μΉλ μ΄ κ°μ¬ μ μ°ΎκΈ°
select count(distinct ID) from teaches where semester = 'Spring' and year = 2018;
courseκ΄κ³μ νν μ μ°ΎκΈ°select count(*) from course;
Aggregate Functions β Group By
group byμ μ μ¬μ©νμ¬ λ¨μΌ νν μ§ν©μ΄ μλ, κ·Έλ£Ήνλ μ¬λ¬ νν μ§ν©μ κ°λ³μ μΌλ‘ μ§κ³ ν¨μ μ μ© κ°λ₯- κ° νκ³Όμ κ°μ¬ νκ· κΈμ¬ μ°ΎκΈ°
select dept_name, avg(salary) as avg_salary from instructor group by dept_name;
group byμ μ¬μ© μ, κ° κ·Έλ£ΉλΉ νλμ ννλ§ μΆλ ₯λ¨
Aggregation
- μ§κ³ ν¨μ μΈμ
selectμ μ μλ μμ±λ€μgroup byλͺ©λ‘μ λνλμΌ ν¨/* erroneous query */select dept_name, ID, avg(salary) from instructor group by dept_name;
- νΉμ κ·Έλ£Ή(
dept_nameμΌλ‘ μ μλ¨)μ κ° κ°μ¬λ λ€λ₯Έ IDλ₯Ό κ°μ§ μ μμ§λ§, κ° κ·Έλ£Ήμ λν΄ νλμ ννλ§ μΆλ ₯λ¨
Aggregate Functions β Having Clause
havingμ μ μ¬μ©νμ¬ μΆλ ₯λ κ° κ·Έλ£Ήμ λν 쑰건 λͺ μ κ°λ₯havingμ μ μ μ΄λ κ·Έλ£Ή νμ± νμ μ μ©λλ λ°λ©΄,whereμ μ μ μ΄λ κ·Έλ£Ή νμ± μ μ μ μ©λ¨- νκ· κΈμ¬κ° 42000λ³΄λ€ ν° λͺ¨λ νκ³Όμ μ΄λ¦κ³Ό νκ· κΈμ¬ μ°ΎκΈ°
select dept_name, avg(salary) as avg_salary from instructor group by dept_name having avg(salary) > 42000; - νκ° μμ
fromμ μ΄ λ¨Όμ νκ°λ¨whereμ μ μ μ΄(μ‘΄μ¬ μ)κ°fromμ μ κ²°κ³Όμ μ μ©λ¨whereμ μ΄λ₯Ό λ§μ‘±νλ ννλ€μ΄group byμ μ μν΄ κ·Έλ£ΉμΌλ‘ λ°°μΉλ¨havingμ (μ‘΄μ¬ μ)μ΄ κ° κ·Έλ£Ήμ μ μ©λ¨;havingμ μ μ΄λ₯Ό λ§μ‘±νμ§ μλ κ·Έλ£Ήλ€μ μ κ±°λ¨selectμ μ λ¨μ κ·Έλ£Ήλ€μ μ¬μ©νμ¬, μ§κ³ ν¨μ(μ‘΄μ¬ μ)λ₯Ό μ μ©ν ν κ° κ·Έλ£Ήμ λν λ¨μΌ κ²°κ³Ό ννμ μ»μ
Nested Subqueries
- SQLμ μλΈμΏΌλ¦¬(subquery)μ μ€μ²© λ©μ»€λμ¦ μ 곡
- μλΈμΏΌλ¦¬λ λ€λ₯Έ 쿼리 λ΄μ μ€μ²©λ
select-from-whereννμ - μ€μ²©μ λ€μ SQL 쿼리μμ μνλ μ μμ:
select A1, A2, ..., An from r1, r2, ..., rm where P - λ€μκ³Ό κ°μ΄:
fromμ : λ μ ν¨ν μλΈμΏΌλ¦¬λ‘ λ체 κ°λ₯whereμ : λB <operation> (subquery)ννμ ννμμΌλ‘ λ체 κ°λ₯. μ¬κΈ°μ λ μμ±selectμ : λ λ¨μΌ κ°μ μμ±νλ μλΈμΏΌλ¦¬(μ€μΉΌλΌ μλΈμΏΌλ¦¬, scalar subquery)λ‘ λ체 κ°λ₯
Nested Subqueries: Set Membership
- 2017λ
κ°μκ³Ό 2018λ
λ΄μ μ 곡λ κ³Όλͺ© μ°ΎκΈ°
select distinct course_id from section where semester = 'Fall' and year = 2017 and course_id in (select course_id from section where semester = 'Spring' and year = 2018); - 2017λ
κ°μμλ μ 곡λμμ§λ§ 2018λ
λ΄μλ μ 곡λμ§ μμ κ³Όλͺ© μ°ΎκΈ°
select distinct course_id from section where semester = 'Fall' and year = 2017 and course_id not in (select course_id from section where semester = 'Spring' and year = 2018); - μ΄λ¦μ΄ "Mozart"λ "Einstein"λ μλ λͺ¨λ κ°μ¬ μ΄λ¦ μ§μ
select distinct name from instructor where name not in ('Mozart', 'Einstein');
- IDκ° 10101μΈ κ°μ¬κ° κ°λ₯΄μΉ κ³Όλͺ© μΉμ
μ μκ°ν (κ³ μ ν) νμλ€μ μ΄ μ μ°ΎκΈ°
select count(distinct ID) from takes where (course_id, sec_id, semester, year) in (select course_id, sec_id, semester, year from teaches where teaches.ID = '10101');- μ°Έκ³ : μ 쿼리λ ν¨μ¬ κ°λ¨ν λ°©μμΌλ‘ μμ± κ°λ₯νλ©°, μ 곡μμ SQL κΈ°λ₯ μ€λͺ λͺ©μ μ
Nested Subqueries: Set comparison (βsomeβ)
- μλ¬Όν(Biology) νκ³Όμ μλ μ΄λ€ (μ μ΄λ ν λͺ
μ) κ°μ¬λ³΄λ€ κΈμ¬κ° λμ κ°μ¬λ€μ μ΄λ¦ μ°ΎκΈ°
select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Biology';
someμ μ μ¬μ©ν λμΌν 쿼리select name from instructor where salary > some (select salary from instructor where dept_name = 'Biology');
Definition of βsomeβ Clause
F <comp> some rΨ¨ΨΩΨ« Ψ£Ω- μ¬κΈ°μ
<comp>λ<,<=,>,=,!=κ° λ μ μμ (5 < some {0, 5, 6})=true(5 < some {0, 5, 0})=false(5 = some {0, 5, 0})=true(5 != some {0, 5, 5})=true(β΅ )(= some)in(!= some)μnot inκ³Ό λμΉκ° μλ
Nested Subqueries: Set comparison (βallβ)
- μλ¬Όν νκ³Όμ λͺ¨λ κ°μ¬μ κΈμ¬λ³΄λ€ λ λ§μ κΈμ¬λ₯Ό λ°λ λͺ¨λ κ°μ¬μ μ΄λ¦ μ°ΎκΈ°
select name from instructor where salary > all (select salary from instructor where dept_name = 'Biology');
Definition of βallβ Clause
F <comp> all r(5 < all {0, 5, 6})=false(5 < all {6, 10, 4})=false(5 = all {4, 5, 6})=false(5 != all {4, 6})=true(β΅ μ΄κ³ )(= all)μinκ³Ό λμΉκ° μλ(!= all)not in
Nested Subqueries: Test for Empty Relations
existsꡬ문μ μΈμ μλΈμΏΌλ¦¬κ° λΉμ΄ μμ§ μμΌλ©΄trueλ₯Ό λ°νexists rnot exists r- "2017λ
κ°μ νκΈ°μ 2018λ
λ΄ νκΈ° λͺ¨λμ κ°λ₯΄μΉ λͺ¨λ κ³Όλͺ© μ°ΎκΈ°" 쿼리λ₯Ό μ§μ νλ λ λ€λ₯Έ λ°©λ²
select course_id from section as S where semester = 'Fall' and year = 2017 and exists (select * from section as T where semester = 'Spring' and year = 2018 and S.course_id = T.course_id); - SQLμμ, μΈλΆ 쿼리μ μκ΄ μ΄λ¦(λ³μ
S)μwhereμ μ μλΈμΏΌλ¦¬μμ μ¬μ©λ μ μμ - μΈλΆ 쿼리μ μκ΄ μ΄λ¦μ μ¬μ©νλ μλΈμΏΌλ¦¬λ₯Ό μκ΄ μλΈμΏΌλ¦¬(correlated subquery)λΌκ³ ν¨
Use of βnot existsβ Clause
- μλ¬Όν νκ³Όμμ μ 곡νλ λͺ¨λ κ³Όλͺ©μ μκ°ν λͺ¨λ νμ μ°ΎκΈ°
- μ°Έκ³
select distinct S.ID, S.name from student as S where not exists ( (select course_id from course where dept_name = 'Biology') except (select T.course_id from takes as T where S.ID = T.ID) ); - 첫 λ²μ§Έ μ€μ²© 쿼리λ μλ¬Όνμμ μ 곡νλ λͺ¨λ κ³Όλͺ©μ λμ΄
- λ λ²μ§Έ μ€μ²© 쿼리λ νΉμ νμμ΄ μκ°ν λͺ¨λ κ³Όλͺ©μ λμ΄
Nested Subqueries: Test for Absence of Duplicate Tuples
uniqueꡬ문μ μλΈμΏΌλ¦¬ κ²°κ³Όμ μ€λ³΅ ννμ΄ μλμ§ ν μ€νΈ- μ£Όμ΄μ§ μλΈμΏΌλ¦¬μ μ€λ³΅μ΄ μμΌλ©΄
trueλ‘ νκ°λ¨ - 2017λ
μ μ΅λ ν λ² μ 곡λ λͺ¨λ κ³Όλͺ© μ°ΎκΈ°
select T.course_id from course as T where unique (select R.course_id from section as R where T.course_id = R.course_id and R.year = 2017);
Subqueries in the βfromβ Clause
- SQLμ
fromμ μμ μλΈμΏΌλ¦¬ ννμμ μ¬μ©νλλ‘ νμ© - νκ· κΈμ¬κ° $42,000 μ΄μμΈ νκ³Όλ€μ κ°μ¬ νκ· κΈμ¬ μ°ΎκΈ°
select dept_name, avg_salary from (select dept_name, avg(salary) as avg_salary from instructor group by dept_name) where avg_salary > 42000; havingμ μ μ¬μ©ν νμκ° μλ€λ μ μ μ μ- μ 쿼리λ₯Ό μμ±νλ λ λ€λ₯Έ λ°©λ²
select dept_name, avg_salary from (select dept_name, avg(salary) from instructor group by dept_name) as dept_avg(dept_name, avg_salary) where avg_salary > 42000;
Nested Subqueries: βWithβ Clause
withμ μwithμ μ΄ λ°μνλ 쿼리μμλ§ μ μκ° μ¬μ© κ°λ₯ν μμ κ΄κ³(μμ λ·°)λ₯Ό μ μνλ λ°©λ² μ 곡- νκ· μμ°λ³΄λ€ λ ν° μμ°μ κ°μ§ λͺ¨λ νκ³Ό μ°ΎκΈ°
with avg_budget(value) as (select avg(budget) from department) select department.name from department, avg_budget where department.budget > avg_budget.value;
Complex Queries using βWithβ Clause
- μ΄ κΈμ¬κ° λͺ¨λ νκ³Όμ μ΄ κΈμ¬ νκ· λ³΄λ€ ν° λͺ¨λ νκ³Ό μ°ΎκΈ°
with dept_total (dept_name, value) as (select dept_name, sum(salary) from instructor group by dept_name), dept_total_avg(value) as (select avg(value) from dept_total) select dept_name from dept_total, dept_total_avg where dept_total.value > dept_total_avg.value;
Nested Subqueries: Scalar Subquery
- μ€μΉΌλΌ μλΈμΏΌλ¦¬λ λ¨μΌ κ°μ΄ μμλλ κ³³μ μ¬μ©λλ μλΈμΏΌλ¦¬
- μ€μΉΌλΌ μλΈμΏΌλ¦¬λ μΈλΆ 쿼리μ
selectμ μμ μ¬μ©λ μ μμ - κ° νκ³Όμ κ°μ¬ μμ ν¨κ» λͺ¨λ νκ³Ό λμ΄
select dept_name, (select count(*) from instructor where department.dept_name = instructor.dept_name) as num_instructors from department; - μλΈμΏΌλ¦¬κ° νλ μ΄μμ κ²°κ³Ό ννμ λ°ννλ©΄ λ°νμ μ€λ₯ λ°μ
Null Values
- ννμ΄ μΌλΆ μμ±μ λν΄
nullλ‘ νμλλnullκ°μ κ°μ§ μ μμ nullμ μ μ μλ κ°μ΄κ±°λ κ°μ΄ μ‘΄μ¬νμ§ μμμ μλ―Έnullμ ν¬ν¨νλ λͺ¨λ μ°μ ννμμ κ²°κ³Όλnull- μ§κ³ ν¨μλ
nullκ°μ 무μ - μ€λ³΅ μ κ±° λ° κ·Έλ£Ήνμ κ²½μ°,
nullμ λ€λ₯Έ κ°μ²λΌ μ·¨κΈλλ©°, λnullμ λμΌν κ²μΌλ‘ κ°μ£Όλ¨ is nullμ μ΄λnullκ°μ νμΈνλ λ° μ¬μ©λ μ μμ- μμ: κΈμ¬κ°
nullμΈ λͺ¨λ κ°μ¬ μ°ΎκΈ°select name from instructor where salary is null;
is not nullμ μ΄λ μ μ©λ κ°μ΄nullμ΄ μλ κ²½μ° μ±κ³΅- SQLμ
nullκ°μ ν¬ν¨νλ λΉκ΅μ κ²°κ³Όλ₯ΌunknownμΌλ‘ μ²λ¦¬ (is nullλ°is not nullμ μ΄ μ μΈ) - μμ:
5 < nullλλnull <> nullλλnull = null whereμ μ μ μ΄λ λΆλ¦¬μΈ(boolean) μ°μ°(and,or,not)μ ν¬ν¨ν μ μμΌλ―λ‘,unknownκ°μ μ²λ¦¬νκΈ° μν΄ λΆλ¦¬μΈ μ°μ°μ μ μλ₯Ό νμ₯ν΄μΌ ν¨and:(true and unknown)=unknown,(false and unknown)=false,(unknown and unknown)=unknownor:(unknown or true)=true,(unknown or false)=unknown,(unknown or unknown)=unknownnot:(not unknown)=unknown
- SQLμμ
P is unknownμ μ μ΄Pκ°unknownμΌλ‘ νκ°λλ©΄trueλ‘ νκ°λ¨ whereμ μ μ΄μ κ²°κ³ΌλunknownμΌλ‘ νκ°λλ©΄falseλ‘ μ²λ¦¬λ¨
Modification of the Database
- μ£Όμ΄μ§ κ΄κ³μμ νν μμ
- μ£Όμ΄μ§ κ΄κ³μ μ νν μ½μ
- μ£Όμ΄μ§ κ΄κ³μ μΌλΆ νν κ° μ λ°μ΄νΈ
Deletion
- λͺ¨λ κ°μ¬ μμ
delete from instructor;
- μ¬λ¬΄(Finance) νκ³Όμ λͺ¨λ κ°μ¬ μμ
delete from instructor where dept_name = 'Finance';
'ITBT'건물μ μμΉν νκ³Όμ κ΄λ ¨λinstructorκ΄κ³μ λͺ¨λ νν μμ delete from instructor where dept_name in (select dept_name from department where building = 'ITBT');
- κΈμ¬κ° κ°μ¬ νκ· κΈμ¬λ³΄λ€ μ μ λͺ¨λ κ°μ¬ μμ
- λ¬Έμ μ :
instructorμμ ννμ μμ ν¨μ λ°λΌ νκ· κΈμ¬κ° λ³κ²½λ¨ - SQLμ ν΄κ²°μ±
:
- λ¨Όμ
avg(salary)λ₯Ό κ³μ°νκ³ μμ ν λͺ¨λ ννμ μ°Ύμ - μμμ μ°Ύμ λͺ¨λ ννμ μμ (νκ· μ μ¬κ³μ°νκ±°λ ννμ λ€μ ν μ€νΈνμ§ μμ)
- λ¨Όμ
delete from instructor where salary < (select avg(salary) from instructor); - λ¬Έμ μ :
Insertion
courseμ μ νν μΆκ°insert into course values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
- λλ λλ±νκ²
insert into course (course_id, title, dept_name, credits) values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
tot_credsκ°nullλ‘ μ€μ λ μ ννμstudentμ μΆκ°insert into student values ('3003', 'Green', 'Finance', null);
- 144 νμ μ΄μμ μ΄μν μμ
(Music) νκ³Όμ κ° νμμ κΈμ¬ $18,000μ μμ
νκ³Ό κ°μ¬λ‘ λ§λ¦
insert into instructor select ID, name, dept_name, 18000 from student where dept_name = 'Music' and total_cred > 144;
studentκ΄κ³μ μ€ν€λ§:(ID, name, dept_name, tot_cred)instructorκ΄κ³μ μ€ν€λ§:(ID, name, dept_name, salary)select from whereλ¬Έμ κ·Έ κ²°κ³Ό μ€ μ΄λ€ κ²μ΄λΌλ κ΄κ³μ μ½μ λκΈ° μ μ μμ ν νκ°λ¨
Updates
- λͺ¨λ κ°μ¬μκ² 5% κΈμ¬ μΈμ
update instructor set salary = salary * 1.05;
- 70000 λ―Έλ§μ λ²λ κ°μ¬μκ² 5% κΈμ¬ μΈμ
update instructor set salary = salary * 1.05 where salary < 70000;
- νκ· λ³΄λ€ κΈμ¬κ° μ μ κ°μ¬μκ² 5% κΈμ¬ μΈμ
update instructor set salary = salary * 1.05 where salary < (select avg(salary) from instructor);
- κΈμ¬κ° $100,000 μ΄μμΈ κ°μ¬μ κΈμ¬λ₯Ό 3% μΈμνκ³ , λλ¨Έμ§λ 5% μΈμ
- λ κ°μ
updateλ¬Έ μμ± (μμκ° μ€μν¨):update instructor set salary = salary * 1.03 where salary > 100000; update instructor set salary = salary * 1.05 where salary <= 100000; caseλ¬Έμ μ¬μ©ν λ λμ λ°©λ²:update instructor set salary = case when salary <= 100000 then salary * 1.05 else salary * 1.03 end;
