• Mindscape πŸ”₯
    • Playlist 🎧
  • πŸ€– Artifical Intelligence

    • 1. Basics; Linear Algebra
    • 2. Basics; Linear Algebra (2), Search (1)
    • 3. Search (2)
    • 4. Knowledge and Logic (1)
    • 5. Knowledge and Logic (2)
    • 6. Probability
    • 7. Information Theory
    • 8. Probabilitc Reasoning (2)
    • 9. Probabilitc Reasoning (3)
    • 10. Machine Learning (1)
    • 11. Machine Learning (2)
    • 12. Machine Learning (3)
    • 13. Linear Models
    • 14. Other Classic ML Models (1)
    • 15. Other Classic ML Models (2)
  • πŸ”’ Computer Security

    • 01. Overview
    • 02. μ •λ³΄λ³΄μ•ˆμ •μ±… 및 λ²•κ·œ
    • 03. Cryptographic Tools
    • 04. User Authentication
    • 05. Access Control
    • 06. Database Security
    • 07. Malicious Software
    • 08. Firmware Analysis
  • πŸ—„οΈ Database System

    • 1. Introduction
    • 2. Relational Model
    • 3. SQL
    • 6. E-R Model
    • 7. Relational Database Design (1)
    • 7. Relational Database Design (2)
    • 13. Data Storage Structures
    • 14. Indexing
    • 15. Query Processing
  • πŸ“ Software Engineering

    • 2. Introduction to Software Engineering
    • 3. Process
    • 4. Process Models
    • 5. Agile
    • 6. Requirements
    • 7. Requirements Elicitation and Documentation
    • 8. Architecture
    • 9. Unified Modelling Language
    • 10. Object-Oriented Analysis
    • Object-Oriented Design
  • 🧠 Algorithm

    • Python μ‹œκ°„ 초과 λ°©μ§€λ₯Ό μœ„ν•œ 팁
    • C++ std::vector μ‚¬μš©λ²• 정리
    • Vim μ‚¬μš© 맀뉴얼
    • 1018번: 체슀판 λ‹€μ‹œ μΉ ν•˜κΈ°
    • 1966번: ν”„λ¦°ν„° 큐

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): μ‚¬μš©μž μ§€μ • 길이 nnn의 κ³ μ • 길이 λ¬Έμžμ—΄
  • varchar(n): μ‚¬μš©μž μ§€μ • μ΅œλŒ€ 길이 nnn의 κ°€λ³€ 길이 λ¬Έμžμ—΄
  • int: μ •μˆ˜ (기계에 μ˜μ‘΄ν•˜λŠ” μ •μˆ˜μ˜ μœ ν•œ λΆ€λΆ„ μ§‘ν•©)
  • smallint: μž‘μ€ μ •μˆ˜ (μ •μˆ˜ 도메인 μœ ν˜•μ˜ 기계 의쑴적 λΆ€λΆ„ μ§‘ν•©)
  • numeric(p,d): ppp자리의 μ‚¬μš©μž μ§€μ • 정밀도와 μ†Œμˆ˜μ  μ΄ν•˜ ddd자리λ₯Ό κ°€μ§„ κ³ μ • μ†Œμˆ˜μ  숫자 (예: numeric(3,1)은 44.5λŠ” μ •ν™•νžˆ μ €μž₯ κ°€λŠ₯ν•˜λ‚˜, 444.5λ‚˜ 0.32λŠ” λΆˆκ°€)
  • real, double precision: 기계 의쑴적 정밀도λ₯Ό κ°€μ§„ 뢀동 μ†Œμˆ˜μ  및 배정밀도 뢀동 μ†Œμˆ˜μ  숫자
  • float(n): μ΅œμ†Œ nnn자리의 μ‚¬μš©μž μ§€μ • 정밀도λ₯Ό κ°€μ§„ 뢀동 μ†Œμˆ˜μ  숫자
  • 좔가적인 λ‚΄μš©μ€ 4μž₯μ—μ„œ λ‹€λ£Έ

DDL: Create Table Construct

  • SQL κ΄€κ³„λŠ” create table λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μ˜:
    • create table r (A1 D1, A2 D2, ..., An Dn, (integrity-constraint1), ..., (integrity-constraintk));
  • rrr은 κ΄€κ³„μ˜ 이름
  • 각 AiA_iAiβ€‹λŠ” 관계 rrr의 μŠ€ν‚€λ§ˆμ— μžˆλŠ” 속성 이름
  • DiD_iDiβ€‹λŠ” 속성 AiA_iAiβ€‹μ˜ 도메인(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;
      • μ—¬κΈ°μ„œ AAAλŠ” 관계 rrr에 좔가될 μ†μ„±μ˜ 이름이고 DDDλŠ” AAA의 도메인
      • κ΄€κ³„μ˜ λͺ¨λ“  κΈ°μ‘΄ νŠœν”Œμ€ μƒˆ 속성에 λŒ€ν•΄ null κ°’μœΌλ‘œ 할당됨
    • alter table r drop A;
      • μ—¬κΈ°μ„œ AAAλŠ” 관계 rrr의 속성 이름
      • 속성 μ‚­μ œλŠ” λ§Žμ€ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μ§€μ›ν•˜μ§€ μ•ŠμŒ

Basic Query Structure

  • 일반적인 SQL 질의 ν˜•νƒœ:
    • select A1, A2, ..., An from r1, r2, ..., rm where P;
  • AiA_iAiβ€‹λŠ” 속성을 λ‚˜νƒ€λƒ„
  • rir_iriβ€‹λŠ” 관계λ₯Ό λ‚˜νƒ€λƒ„
  • PPPλŠ” μˆ μ–΄(predicate)
  • λŒ€μ‘ν•˜λŠ” 관계 λŒ€μˆ˜(relational algebra)

    Ξ A1,A2,…,An(Οƒ_P(r1Γ—r2Γ—β‹―Γ—rm))\Pi _{A_1, A_2, \dots, A_n}(\sigma\_P(r_1 \times r_2 \times \dots \times r_m)) Ξ A1​,A2​,…,An​​(Οƒ_P(r1​×r2​×⋯×rm​))

  • SQL 질의의 κ²°κ³ΌλŠ” 관계

The select Clause

  • select μ ˆμ€ 질의 κ²°κ³Όμ—μ„œ μ›ν•˜λŠ” 속성을 λ‚˜μ—΄
  • 관계 λŒ€μˆ˜μ˜ ν”„λ‘œμ μ…˜(projection) 연산에 ν•΄λ‹Ή
  • μ˜ˆμ‹œ: λͺ¨λ“  κ°•μ‚¬μ˜ 이름 μ°ΎκΈ°
    • select name from instructor;
  • μ°Έκ³ : SQL 이름은 λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•ŠμŒ (예: Name ≑\equiv≑ NAME ≑\equiv≑ name)
  • 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;
    • κ²°κ³ΌλŠ” ν•˜λ‚˜μ˜ μ—΄κ³Ό NNN개의 ν–‰(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) 연산에 ν•΄λ‹Ή
  • instructor Γ—\timesΓ— teaches의 μΉ΄ν‹°μ „ ν”„λ‘œλ•νŠΈ μ°ΎκΈ°
    • 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 T ≑\equiv≑ instructor 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 사이인 λͺ¨λ“  κ°•μ‚¬μ˜ 이름 μ°ΎκΈ° (즉, β‰₯\geβ‰₯ $90,000 이고 ≀\le≀ 100,000100,000100,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;
    
  • 평가 μˆœμ„œ
    1. from 절이 λ¨Όμ € 평가됨
    2. where 절의 μˆ μ–΄(쑴재 μ‹œ)κ°€ from 절의 결과에 적용됨
    3. where μˆ μ–΄λ₯Ό λ§Œμ‘±ν•˜λŠ” νŠœν”Œλ“€μ΄ group by μ ˆμ— μ˜ν•΄ 그룹으둜 배치됨
    4. having 절(쑴재 μ‹œ)이 각 그룹에 적용됨; having 절 μˆ μ–΄λ₯Ό λ§Œμ‘±ν•˜μ§€ μ•ŠλŠ” 그룹듀은 제거됨
    5. select μ ˆμ€ 남은 그룹듀을 μ‚¬μš©ν•˜μ—¬, 집계 ν•¨μˆ˜(쑴재 μ‹œ)λ₯Ό μ μš©ν•œ ν›„ 각 그룹에 λŒ€ν•œ 단일 κ²°κ³Ό νŠœν”Œμ„ μ–»μŒ

Nested Subqueries

  • SQL은 μ„œλΈŒμΏΌλ¦¬(subquery)의 쀑첩 λ©”μ»€λ‹ˆμ¦˜ 제곡
  • μ„œλΈŒμΏΌλ¦¬λŠ” λ‹€λ₯Έ 쿼리 내에 μ€‘μ²©λœ select-from-where ν‘œν˜„μ‹
  • 쀑첩은 λ‹€μŒ SQL μΏΌλ¦¬μ—μ„œ μˆ˜ν–‰λ  수 있음: select A1, A2, ..., An from r1, r2, ..., rm where P
  • λ‹€μŒκ³Ό 같이:
    • from 절: rir_iriβ€‹λŠ” μœ νš¨ν•œ μ„œλΈŒμΏΌλ¦¬λ‘œ λŒ€μ²΄ κ°€λŠ₯
    • where 절: PPPλŠ” B <operation> (subquery) ν˜•νƒœμ˜ ν‘œν˜„μ‹μœΌλ‘œ λŒ€μ²΄ κ°€λŠ₯. μ—¬κΈ°μ„œ BBBλŠ” 속성
    • select 절: AiA_iAiβ€‹λŠ” 단일 값을 μƒμ„±ν•˜λŠ” μ„œλΈŒμΏΌλ¦¬(슀칼라 μ„œλΈŒμΏΌλ¦¬, 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 β‡”βˆƒt∈r\Leftrightarrow \exists t \in rβ‡”βˆƒt∈r بحيث Ψ£Ω† (F<comp>t)(F <comp> t)(F<comp>t)
  • μ—¬κΈ°μ„œ <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 (∡ 0β‰ 50 \ne 50ξ€ =5)
  • (= some) ≑\equiv≑ 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 β‡”βˆ€t∈r(F<comp>t)\Leftrightarrow \forall t \in r (F <comp> t)β‡”βˆ€t∈r(F<comp>t)
  • (5 < all {0, 5, 6}) = false
  • (5 < all {6, 10, 4}) = false
  • (5 = all {4, 5, 6}) = false
  • (5 != all {4, 6}) = true (∡ 5β‰ 45 \ne 45ξ€ =4 이고 5β‰ 65 \ne 65ξ€ =6)
  • (= all)은 inκ³Ό λ™μΉ˜κ°€ μ•„λ‹˜
  • (!= all) ≑\equiv≑ not in

Nested Subqueries: Test for Empty Relations

  • exists ꡬ문은 인수 μ„œλΈŒμΏΌλ¦¬κ°€ λΉ„μ–΄ μžˆμ§€ μ•ŠμœΌλ©΄ trueλ₯Ό λ°˜ν™˜
  • exists r ⇔rβ‰ βˆ…\Leftrightarrow r \ne \emptyset⇔rξ€ =βˆ…
  • not exists r ⇔r=βˆ…\Leftrightarrow r = \emptyset⇔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

  • 생물학 ν•™κ³Όμ—μ„œ μ œκ³΅ν•˜λŠ” λͺ¨λ“  κ³Όλͺ©μ„ μˆ˜κ°•ν•œ λͺ¨λ“  학생 μ°ΎκΈ°
  • Xβˆ’Y=βˆ…β‡”XβŠ†YX - Y = \emptyset \Leftrightarrow X \subseteq YXβˆ’Y=βˆ…β‡”XβŠ†Y μ°Έκ³ 
    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) = unknown
    • or: (unknown or true) = true, (unknown or false) = unknown, (unknown or unknown) = unknown
    • not: (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의 ν•΄κ²°μ±…:
      1. λ¨Όμ € avg(salary)λ₯Ό κ³„μ‚°ν•˜κ³  μ‚­μ œν•  λͺ¨λ“  νŠœν”Œμ„ 찾음
      2. μœ„μ—μ„œ 찾은 λͺ¨λ“  νŠœν”Œμ„ μ‚­μ œ (평균을 μž¬κ³„μ‚°ν•˜κ±°λ‚˜ νŠœν”Œμ„ λ‹€μ‹œ ν…ŒμŠ€νŠΈν•˜μ§€ μ•ŠμŒ)
    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;
    
졜근 μˆ˜μ •: 25. 11. 6. μ˜€ν›„ 12:07
Contributors: kmbzn
Prev
2. Relational Model
Next
6. E-R Model

BUILT WITH

CloudflareNode.jsGitHubGitVue.jsJavaScriptVSCodenpm

All trademarks and logos are property of their respective owners.
Β© 2025 kmbzn Β· MIT License