Chapter 3. SQL 소개

샤이니·2021년 10월 29일
0

데이터베이스

목록 보기
1/1

SQL Query Language 개요

  • Data-definition language(DDL)
    • SQL DDL은 relation schema를 정의하고, relation을 삭제하고, relation schema를 modification하는 명령어를 제공함
  • Data-manipulation language(DML)
    • SQL DML은 데이터베이스에 정보를 질의하고 튜플을 삽입, 수정하는 기능을 제공함
  • Integrity 무결성
    • SQL DDL은 데이터베이스에 저장될 데이터가 만족해야하는 Integrity 제약조건들을 명시하는 명령어를 포함함. 이런 무결성 제약 조건을 위반하는 갱신은 허용되지 않음
  • View definition
    • SQL DDL은 view를 정의할 수 있는 명령어들을 포함함
  • Transaction control
    • SQL은 transaction의 시작과 끝을 명시하는 명령어를 포함함
  • Embedded SQL과 Dynamic SQL
    • 내장과 동적 SQL은 C, C++, JAVA와 같은 범용 프로그래밍 언어에서 내장되어 사용될 수 있음.
  • Authorization
    • SQL DDL은 relation과 view에 access하는 권한을 명시하는 명령어를 포함함
  • Schema Diagram for University Database

SQL Data 정의

SQL data-definition language(DDL)은 relation의 set 뿐만아니라 다음을 포함하는 각 relation들에 대한 정보도 명시할 수 있게 함.

  • 각 relation schema
  • 각 속성들과 관련된 값들의 type
  • Integrity constraints
  • 각 relaion에서 유지해야할 index들의 집합
  • 각 relation의 Security와 authorization
  • 각 relation의 Disk에서의 physical storage structure

Domain Types in SQL

  • char(n) : 사용자가 지정하는 길이 n을 갖는 고정 길이 문자열
  • varchar(n) : 사용자가 지정하는 최대 길이 n을 갖는 가변 길이 문자열
  • int : 정수(기계 종속적인 정수의 유한 부분 집합)
  • smalint : 작은 정수(int domain type의 기계 종속적인 subset)
  • numeric(p,d) : 사용자가 지정하는 precision(정확도)를 갖는 fixed-point number. 수는 p개의 숫자(부호 포함), p개의 숫자에서 d개는 소수점의 오른쪽에 있음.
  • real, double precision : 기계 종속적인 정확도를 가지는 Floating point number과 기계 종속 수준 두배의 정확도를 지닌 Floating point number
  • float(n) : 적어도 n개의 숫자의 정확도를 가지는 floating point number

기본 Schema 정의

create Table Construct

create table 명령을 사용해 SQL relation을 정의할 수 잇음.

create table r
	(A1 D1, 
    	A2 D2, 
    	..., 
    	An Dn,
	(integrity-constraint 1),
        ...,
        (integrity-constraint k)

  • r : relation의 이름
  • 각 Ai : relation r의 schemad의 attribute 이름
  • 각 Di : attribute Ai의 domain

Integrity Constraints in Create Table

  • not null

    • attribute에서의 not null constraint는 그 attribute에 대해 null값이 허락되지 않음을 명시.
    • 즉, 그 attribute의 domain에서 null값을 제외
  • primary key (Aj1, ..., Ajn )

    • primary key declaration은 attribute가 relation의 primary key를 구성함을 나타냄
    • Primary key는 null값을 가지지 않고 유일함
    • 즉 attribute에 대한 primary key declaration(선언)은 'not null'을 자동으로 보장함.
  • foreign key (Ak1, ..., Akn ) references r

    • 외래 키 declaration은 relation에서 어떤 tuple에 대한 attribute의 값(Ak1, ..., Akn)이 반드시 relation s의 몇 tuple의 primary key attribute value로 상응되어야 함

    • foreign key (dept_name) references department); :
      각 수업 tuple에 대해 그 tuple에 명시된 학과 이름은 반드시 departmanet relation의 primary attribute인(dept_name)에 존재해야 함

Updates to tables

  • Insert
insert into instructor values (10211, ’Smith’, ’Biology’, 66000);
  • Delete
delete from student
// student relation의 모든 tuple 삭제
  • Drop Table
 drop table r 
 // database에서 제거될 relation과 관련된 모든 정보 삭제
 // r의 모든 tuple 뿌만 아니라 r의 shcema 까지 삭제
  • Alter
alter table r add A D
// 이미 존재하는 relation에 attribute 추가
// A: 추가될 속성 이름, D: 그 속성의도메인
//해당 relation의 모든 tuple은 null이 새 attribute value로 할당
alter table r drop A
// relation에서 attribute 삭제

SQL Basic Query Structure

SQL query의 기본 구조는 :

select A1, A2, ..., An
from r1, r2, ..., rm
where P

query는 from 절에 나열된 relation들을 input으로 받고, where과 select 절에 명시된 동작을 수행해 결과로 relation을 만들어 냄.

select Clause (slect 절>

query 결과에서 원하는 attribute가 나열함.

//instructor의 이름 찾기
select name
from instructor

Additional Basic Operations

Set Operations

Null Value

Aggregate Functions 집계 함수

Nested Subqueries 중첩 하위 질의

Modification of the Database

0개의 댓글