- Functinality
- Massive, Convenient, Multi-user, Safe, Efficient, Reliable
- safe; transaction
- Massive, Convenient, Multi-user, Safe, Efficient, Reliable
Structured Query Language(SQL)
- SQL : Structured Query Language
- principal language
- describe and manipulate relational databases
- Very high-level
- c, java, basic보다 high level이다.
- what to do >> how to doo
- not specifying data-manipulation details
- DBMSs figure out the'best' way to execute queries
- query optimizatiton
- DDL
- Data definition
- for declaring database
- DML
- for querying databases and for modifying the database
- DML
- provide query information
- insert tuples, delete tuples, modify tuples
- Integrity
- DDL → specifying integrity constraints
- View definition
- DDL → defining views
- Transaction control
- transaction; unit of tasks
- specifying the beginning and ending of transactions
- Embedded SQL and dynamic SQL
- 범용 프로그래밍 언어에 SQL statement를 추가할 수 있는 방법에 대한 논의
- Authorization
- set pw, grant permission
- access rights to relations and views
- principal language
Basic Query Structure
SQL data manipulation language (DML)
- SELECT
- FROM
- WHERE
- A → attribute
- R → relation
- P → predicate
SELECT
- lists the attributes desired in the result of a query
- relation algebra 에서 projection에 해당함.
- case insensitive함.
- name = Name = NAME
- 보통 명령어들은 그냥 대문자로 적음 편의상.
- 쿼리 결과에서 중복을 허용함.
- 중복을 없애려면 SELECT DISTINCT
- ALL을 넣으면 무조건 중복이 그대로 나오도
- asterisk (*) → all attributes
- FROM 없이 문자 그대로를 뽑아올 수 있다.
- SELECT '437';
- AS로 해당 칼럼에 이름을 줄 수 있다.
- FROM으로 문자 그대로를 가져올 수 있다.
- SELECT 'A' FROM instructor
- result; one column and n rows (number of tuples in the instructor table, 각각의 row가 A인)
- SELECT 'A' FROM instructor
- arithmetic expression; +, -, *, /
WHERE
- Correspond to the selection predicate of the relational algebra
- 조건문.
- AND, OR, NOT와 함께 사용 가능
- logical connectives; <, ≤, ≥, =, <>
- <> 은 mysql에서 not equal을 의미한다 (≠ in sql)
FROM
- lists the relations involved in the query
- Cartesian Product와 대응
- Find the Cartesian-product instructor X teaches
- 모든 가능한 instructor-teaches쌍을 생성한다.
- 공통 속성에 대하여(ex. ID), 그 속성은 relation name을 사용하여 renamed된다.
JOIN
- Cartesian-product는 직접적으로는 유용하지 않지만, WHERE조건절을 이용해서 유용하게 결합할 수 있다.
- Cartesian-product + selection = join
Rename operator AS
- 안적어도 됨.
Find the names of all instructors who have taught some course and the course_id
- Query : SELECT name, course_id FROM instructor, teaches WHERE Instructor.ID = teaches.ID;
Find the names of all instructors in the Art department who have taught some course and the course_id
- Query: SELECT name, course_id FROM instructor, teaches WHERE instructor.ID = teaches.ID AND instructor.dept_name = 'Art';
NULL Values
- unknown/ does not existt
- 5 + NULL = NULL
- IS NULL
- IS NOT NULL
Set Operations
- Union, Intersect, Except
- 자동으로 중복을 지운다.
- 중복을 지우고 싶지 않다면, ALL을 이용한다.
- mysql 에서는 intersect, except를 지원하지 않는다.
String Operations
- string-matching
- LIKE
- %; match any substring
- _; march any character
- ESCAPE \를 사용해보자
- LIKE '100\%' ESCAPE '\' or LIKE '100\%'
- case sensitive
Glossary
- SQL command의 4가지 종류
- DDL: Data Definition Language
- Create, Alter, Drop, Rename, Truncate, Comment
- DML: Data Manipulation Language
- Insert, Update, Delete, Merge, Call, Explain Plain, Lock Table
- DQL: Data Query Language
- Select
- DCL: Data Control Language
- Grant, Revoke
- DDL: Data Definition Language
- c.f: TCL - Transaction Control Language
DDL (Data Definition Language)
- define the database schema
- database 스키마의 description을 다룬다.
- database 안에 있는 database object의 구조를 create하고 modify한다.
- CREATE
- database나 object를 생성하기 위해 사용. (like table, index, functions, views, store procedure and triggers)
- DROP
- database로 부터 object를 삭제
- ALTER
- database로 부터 structure를 변경
- TRUNCATE
- table내에 있는 모든 records를 삭제
- record를 할당하기 위해 사용한 모든 spaces까지 삭제
- table내에 있는 모든 records를 삭제
- COMMENT
- data dictionary에 comment를 더하기 위함.
- RENAME
- database에 존재하는 object를 rename
- query가 넘겨주는 것에 기초한 몇몇 some schema relation
- DQL은 part of DML이라고 생각하기도 함.
- SELECT
- database로 부터 data를 retrieve하기
- SELECT
- Example
- database에 존재하는 데이터들을 조작하기 위함.
- INSERT
- data를 table에 넣기 위함.
- UPDATE
- 테이블 내에 존재하는 데이터를 update
- DELETE
- database table로 부터 records를 삭제
- INSERT
- example
- CREATE
- Examples
DCL (Data Control Language)
- rights, permission, 다른 database system의 control을 처리하는 명령어
- GRANT
- user가 database에 접근할 수 있는 권리
- REVOKE
- GRANT 명령어에 의해 생겨난 권리를 철회함.
- GRANT
- Example
TCL (Transaction Control Language)
- database내의 transaction을 처리
- COMMIT
- ROLLBACK
- SAVEPOINT
- SET TRANSACTION
- Examples
'Lecture Note > [DB] Database Theory' 카테고리의 다른 글
Structured Query Language (0) | 2023.10.27 |
---|---|
Handshaking with an R-DBMS (0) | 2023.10.27 |
Relational Algebra (0) | 2023.10.26 |
Database Systems (0) | 2023.10.26 |
Database Theory (0) | 2022.08.04 |