Transcript tbl_name

MySQL Tutorial
[email protected]
1
(ver. 1.0)
1. MySQL 설치
2
Linux에 설치
– MySQL 제공의 RPM 을 사용할 것.
• MySQL binary가 빠르게 발전/변경
• 파일 위치상에 다소간 차이
– RPM 파일:
• MySQL-server-VERSION.i386.rpm,
• MySQL-Max-VERSION.i386.rpm, and
• MySQL-client-VERSION.i386.rpm.
– 설치:
• rpm -i MySQL-server-VERSION.i386.rpm MySQLclient-VERSION.i386.rpm
•  mysqld (MySQL server)를 start하고, 동시에
/etc/init.d/ 에 적절한 항목을 생성시킴 (시스템이 켜
지면 자동으로 서버를 start)
3
• 정리:
Windows에 설치
– IF (기존의 MySQL이 있으면) THEN 정지시킬 것:
mysqladmin -u root -p shutdown
– ElseIF (MySQL이 서비스로서 설치) THEN 정지시킬 것:
mysqld --remove
– ElseIF (Graphical tool을 사용하고자 한다면) 정지시킬 것:
• 설치
– control panel>Admin Tool SVC> SVCs Manager에서 서비스 제거
– setup.exe 또는 MSI installer를 이용
• options file을 생성시킬 것.
• 명령어 줄에서
– MySQL 서버의 디렉토리 (c:\mysql\bin)로 간 후 Type:
mysqld-max --standalone
•  startup 메시지. 별 문제가 없다면 MySQL을 서비스로서 설치
– 이용하고자 하는 server executable을 입력:
• 참고
mysqld-max --install (추천)
– Perl engine을 설치할 것.
• www.activestate.com/Products/ActivePerl/
• ActivePerl comes as an MSI file. Download 후 설치.
4
Installing on OS X
• (생략)
5
Configuring Your System
– (recommend)
• InnoDB table 사용
• binary logging 을 켜 놓을 것.
• slow query logging을 켜 놓을 것.
– help you optimize your applications.
– (options file: configuration value 저장)
• Windows에서,
– Global options file can be located either in your
Windows directory and named my.ini or in c:\my.cnf.
– (권고) my.ini 사용 (.cnf는 간혹 다른 프로그램에서 이용)
• Unix-like 운영체제의 경우,
– Global options file은 보통 /etc/my.cnf.
– 하나의 시스템에 여러 서버 운영 시: 서버 별 정보를 my.cnf
의 data directory에 지정.
– 개별 사용자별로 다르게 저장하는 경우: ~/.my.cnf
6
Listing 1.1 Suggested Options File
[mysqld]
# turn on binary logging and slow query logging
log-bin
log-slow-queries
# InnoDB config
# This is the basic config as suggested in the manual
# Datafile(s) must be able to hold your data and indexes.
# Make sure you have enough free disk space.
메모리 x 50%~80%
innodb_data_file_path = ibdata1:10M:autoextend = buffer pool size x 25%
# Set buffer pool size to
-- additional memory pool size
===> log file size
# 50 - 80 % of your computer's memory
-- log buffer-size
set-variable = innodb_buffer_pool_size=70M
set-variable = innodb_additional_mem_pool_size=10M
# Set the log file size to about
# 25 % of the buffer pool size
set-variable = innodb_log_file_size=20M
set-variable = innodb_log_buffer_size=8M
# Set ..flush_log_at_trx_commit to 0 if you can afford losing
# some last transactions
innodb_flush_log_at_trx_commit=1
7
Checking Your System Works
• 기본사항
• - mysql -u root
• PATH 지정
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL
connection id is 4 to server version: 4.1.0-alpha-max-debug-logType 'help;'
or '\h' for help. Type '\c' to clear the buffer.
– If you get a message similar to
ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)
– = “MySQL server가 수행되지 않는다”
–  시스템을 재 시동하거나 다음과 같이 수동 시동:
• - mysqld --standalone
8
– Root Password 설정
– set password for root@localhost=password('your password');
• Log out (\q) 후 다시 log-in 단, 다음과 같이:
– mysql -u root -p
– Anonymous Account 삭제
use mysql;
delete from user where User='';
delete from db where User='';
flush privileges;
9
일상 사용 (Basic use) Account 설정
– 일반 사용을 위한 account 생성:
grant (create, create temporary tables,
delete, execute, index, insert, lock tables,
select, show databases, update)
on *.*
to username identified by 'password';
10
2. Quick Tour
11
MySQL Directory 구조
• (MySQL 파일의 default 위치)
– Base location
• Unix :
/usr/local/mysql
• Windows: C:\mysql.
– File system
•
•
•
•
•
bin: MySQL server, client 및 기타의 유용한 compiled program 수록
scripts: Perl scripts 수록
data: 실제의 데이터베이스 데이터 수록
docs (Linux) or Docs (Windows)
sql-bench (Linux) or bench (Windows): benchmarking suite.
– 사용자가 방문하지 않는 다른 디렉토리
• include : header files,
• lib :
libraries used by MySQL,
• share : MySQL's error messages, and examples (only in
Windows, libmysql DLL을 이용하는 예제).
• ** MySQL에 link되는 다른 프로그램 (예: PHP) 설치 시에 그 위치를 아
는 것이 유용.
12
Executables 개요
• MySQL executables란?
– bin and scripts directories
– bin directory
• mysqld (또는 관련 변형 모듈)= MySQL server program
• mysql = MySQL monitor = command-line client.
• 기타의 주요 프로그램
–
–
–
–
–
mysqladmin: 관리 기능
myisamchk: damaged MyISAM table을 check/repair.
mysqldump: database의 backup
mysqlbinlog: binary log의 내용을 read. (disaster recovery 등).
mysqlshow: databases 및 table 정보를 보여줌
– scripts directory
• = interpreted Perl scripts (cf.bin 디렉토리: compiled program)
• 중요한 script
– Mysqlhotcopy ; database의 back up.
13
User Interface 개요
• 3가지 주요 UI
– mysql (=MySQL monitor),
• command-line interface
• 기본 설치 시에 생성됨
– MySQL Control Center (MySQLCC),
• (Qt windowing toolkit으로 작성된) GUI
• cross-platform
• 쉽게 download해서 설치
– phpMyAdmin.
• Web 기반 interface. (Web application 개발에 편리)
• 쉽게 download 설치
14
MySQL Monitor 맛보기
• MySQL에 연결:
• mysql -u username -p
– (스위치)
• mysql -h hostname -u username -p /* 호스트 지정 */
• mysql --i-am-a-dummy -u root -p  명령오류에 따른 damage 예방
• --i-am-a-dummy option = --safe-updates.
– SHOW command:
•
•
•
•
show databases; /* semicolon 유의 */
use databasename;
show tables;
describe tablename;
– \ (backslash)로 시작되는 명령어
• \q, \h (for help)
• Command script
– 수행:
• source filename
/* MySQL monitor에서 */
• mysql -u username -p < filename
/* MySQL monitor에 log-in되지 않은 경우 */ 15
3. DB Design Crash Course
16
Database 용어와 개념
• Entities and Relationships
– Entities
• = 실제 세계에서의 사물 (things in the real world).
 그 정보는 데이터베이스에 저장한다.
• 예: employees, departments
– Relationships
• = entity들 간의 link
• 예: Works-for; an employee works for a department.
• different degrees of relationship
– one-to-one, one-to-many many-to-many
17
• Relations or Tables
– relational database management system (RDBMS)란
– = supports databases that consist of a set of relations.
– 여기서 relation = a table of data. (예: excel파일, Fig 3.2)
• Columns or Attributes
– column and attribute 이 종종 혼용되지만
– a column is really part of a table, whereas an attribute
relates to the real-world entity that the table is modeling.
• In Figure 3.2 …
• Rows = Records = Tuples
– …
18
• Keys
– A superkey
• = table 내에서 row를 식별해 내는데 사용되는 a column (or set of
columns) .
• (예) employee table에서 <employeeID, name> 또는 <employeeID,
name, job, departmentID>를 통해 row를 구별 가능하다. 이들 각각이
super keys
– A key = a minimal superkey.
• employee table에서 employee를 name 또는 employeeID에 의해 구별할
수 있으므로 이들 모두 key가 된다.
– Candidate keys
• = keys from which we will choose the primary key.
• 그러나 실제로는 We need only the employeeID to identify a row. 즉,
minimal superkey (a minimized set of columns that can be used to
identify a single row)인 employeeID가 key가 된다.
– primary key
• = the column or set of columns that we will use to identify a single
row from within a table. 여기서는 employeeID를 primary key로 한다.
– Foreign keys
• represent the links between tables.
19
• Functional Dependencies
– normalization process 이해를 위해 필요한 개념
– 테이블 상에서 column A와 column B 사이에 functional dependency
(A B) = the value of column A determines the value of column B.
• 예: employee table에서 employeeID functionally determines the name
(and all the other attributes in this particular example).
• Schemas
– = the structure (=design/ blueprint/ form) of the database without
any data in it.
– 다음과 같이 표현:
• employee(employeeID, name, job,
– 본서에서의 convention:
)
• a solid underline = the attributes that represent the primary key
• a broken underline = any attributes that represent foreign keys.
• a solid and a broken underline = Primary keys that are also foreign
keys
20
Database 설계 원칙
• Database설계 시 고려할 중점사항:
–
–
–
–
Entity; 대상이 되는 정보는 무엇? 즉, things or entities?
Relation; …
원하는 (query하려는 )정보? =“모델링 하려는 사업의 business rules”
정규화; redundancy and data anomalies와 같은 구조적 문제의 해결!!
• 효율성 (tuning/optimizing)과 관리성
• Redundancy Versus Loss of Data
– Redundancy = 여러 row or table에서 반복되는 data.
– (Imagine:)
• employeeDepartment(employeeID, name, job, departmentID,
departmentName)
• Figure 3.3. ; This schema design leads to redundantly storing the
department name over and over.
• We can change this design as shown here:
employee(employeeID, name, job,
department(departmentID, name)
)
•  저장 공간을 줄이고 기타의 몇 가지 문제를 피하기 위함.
– Schema 개선 시 염두에 둘 goal;
• “reducing repetition of data without losing any information”.
21
• Anomalies
– (3 types)
–  flawed schema에서 데이터를 insert/delete/update할 때 발생 . (Fig 3.3.)
– Insertion Anomalies
• 기존의 항목과 상충되는 (match되지 않는) 데이터를 insert하는 경우
• (예) enter an employee as working for Department 42, Development? 어느 row가 맞는
지 확실치 않음.
– Deletion Anomalies
• (예) all the employees of Department 128 leave on the same day. 이때 이들 모든 record
를 delete 하면 더 이상 Department 128가 존재하지 않게 된다.
– Update Anomalies
• (예) Department 128 decides to change its name to Emerging Technologies.
자 모두를 수정해야 하는데 만약 누락되는 자가 생길 때 update anomaly.
이때 관련
• Null Values
– = avoid schema designs that have large numbers of empty attributes.
– 예: 100명중 1~2명만 해당되는 사항이 있을 때 대부분이 NULL이므로 오히려 그 사항을
기술하는 새로운 table을 추가하는 편이 낫다.
22
정규화
• Normalization
– = DB설계 결함 제거 절차  breaking tables into smaller tables
– 1차 normal form > 2차 NF > …
• "Normalization is about the key, the whole key, and nothing
but the key."
• 2NF; attributes must depend on the whole key.
• 3NF; attributes must depend on nothing but the key.
• First Normal Form (1NF)
–
–
–
–
each attribute/column value must be atomic.
(= 즉, a single value, not a set of values or another database row).
Figure 3.4은 skill column에 여러 개의 값을 가지므로 1NF가 아니다.
(해결2) Figure 3.5; 그러나 redundancy가 많다  Figure 3.6;
23
• Second Normal Form
– 조건:
• all attributes that are not part of the primary key are fully functionally
dependent on the primary key, AND
• the schema is already in first normal form.
(의미) each non-key attribute must be functionally dependent on all parts of the
key. 즉, if the primary key is made up of multiple columns, every other attribute
in the table must be dependent on the combination of these columns.
– 예: Figure 3.5. 1NF이지만 2NF은 아니다.  Primary key가 특정 row를
uniquely identify해야 하므로.
– 이 예에서는 employeeID and skill의 combination에 의해서만 가능하므로
• 즉, employeeID, skill  name, job, departmentID
• 또한 employeeID  name, job, departmentID에서 name, job, departmentID가
(fully가 아닌) partially functionally dependent on the primary key.
• Skill을 이런 식으로 set up하면 (row를 uniquely identify하는데) employeeID만으로는
충분치 않다. (예: the employeeID 7513 identifies three rows. However, the
combination of employeeID and skill will identify a single row  따라서 이들 2개
를 합하여 primary key로 이용.)
•  이에 따라: employee(employeeID, name, job,
, skill)
– "What are the functional dependencies here?"
• We have employeeID, skill name, job, departmentID but we also have
employeeID
name, job, departmentID
• 즉, we can determine the name, job, and departmentID from employeeID alone.
– "How can we put it into second normal form?"
•
•
•
•
break the table into two tables!!, to wit:
employee(employeeID, name, job,
)
employeeSkills(
, skill)
== Figure 3.6의 schema. 이것은 1NF이면서 동시에 2NF ( each non-key attribute
24
is now functionally dependent on all parts of the keys.)
• Third Normal Form
– Formally, for a schema to be in 3NF,
• remove all transitive dependencies, AND
• the schema must already be in second normal form.
(transitive dependency?) Figure 3.3:
–
–
–
–
–
–
» employeeDepartment(employeeID, name, job, departmentID, departmentName)
This schema contains the following functional dependencies:
» employeeID name, job, departmentID, departmentName
» departmentID departmentName
Primary key is employeeID, and all the attributes are fully functionally dependent on it
However, we can see that we have
» employeeID  departmentName
» employeeID  departmentID
and
» departmentID  departmentName
Note also that the attribute departmentID is not a key. This relationship means that the
functional dependency employeeID departmentName is a transitive dependency. 여기서
departmentID는 key가 아니면서도 departmentID  departmentName의 dependency를 가지
는데 이를 transitive dependency라고 한다.
Effectively, it has a middle step (the departmentID departmentName dependency).
To get to third normal form, we need to remove this transitive dependency.
즉, decompose this
» employee(employeeID, name, job,
)
» department(departmentID, departmentName)
– (3NF의 다른 표현) For every functional dependency in every table, either
•
The left side of the functional dependency is a superkey (that is, a key that is
not necessarily minimal). or
• The right side of the functional dependency is part of any key of that table.
25
– 대부분 functional dependencies will be covered by the first rule!
• Boyce-Codd Normal Form (BCNF)
– = a variation on third normal form.
– Relation이 BCNF이려면 ;
• 3NF +
• come under the first of the two rules. 즉, all the functional
dependencies must have a superkey on the left side.
– 대부분 별다른 조치를 취하지 않아도 충족된다. 이에 위배되는
dependency가 있다면 앞 서와 같이 다시 decompose 작업!
• Higher Normal Forms
– practical database design이라기 보다는 주로 학술적 대상.
– 3NF (or BCNF) is sufficient to avoid the data redundancy
problems you will encounter.
26
요약
•
Concepts
–
–
–
–
–
–
Entities are things, and relationships are the links between them.
Relations or tables hold a set of data in tabular form.
Columns belonging to tables describe the attributes that each data item possesses.
Rows in tables hold data items with values for each column in a table.
Keys are used to identify a single row.
Functional dependencies identify which attributes determine the values of other
attributes.
– Schemas are the blueprints for a database.
•
Design Principles
– Minimize redundancy without losing data.
– Insertion, deletion, and update anomalies are problems that occur when trying to
insert, delete, or update data in a table with a flawed structure.
– Avoid designs that will lead to large quantities of null values.
•
Normalization
–
–
–
–
–
Normalization is a formal process for improving database design.
1NF means atomic column or attribute values.
2NF means that all attributes outside the key must depend on the whole key.
3NF means no transitive dependencies.
Boyce-Codd normal form (BCNF) means that all attributes must be functionally
determined by a superkey.
27
4. Database, Table, Index의 생성
28
Creating a database
• 샘플 database schema:
•
•
•
•
•
employee(employeeID, name, job,
)
department(departmentID, name)
employeeSkills(
, skill)
client(clientID, name, address, contactPerson, contactNumber)
assignment(
,
, workdate, hours)
• SQL (structured query language)
– DDL (data definition language)
– DML (data manipulation language)
• Case Sensitivity
– SQL keywords
• are not case sensitive.  standard across database systems.
– Identifiers
• Database에 따라 다른데 MySQL에서는 운영환경(OS)에 따라
– Windows; database/table name은 not case sensitive
– Unix-like operating system; case sensitive
– MacOS X; 파일시스템 설정에 따라 either case insensitive (HFS+, the
default,) or case sensitive (UFS).
– 혼란 피하기 위해 모든 identifier를 case sensitive로 취급!  platform간
29
portability 증진
– Column 명, index, aliases는 never case sensitive in MySQL.
Identifiers
• 객체 (object)를 unique하게 식별 (identify)하는 것 (= name of an
alias, a database, a table, a column, or an index. )
• 사용가능 문자 = 모든 문자. 단,
– can't contain quote characters, ACSII(0) and ASCII(255).
– Database 명은 Directory명에 사용되는 모든 문자가 가능하지만 ‘/, \ 과 .’
는 안 된다.
– Table 명은 filename에 사용하는 모든 문자가 가능하지만 ‘. 과 /’는 안 된다.
• 길이 = 64 문자 max (단,Alias 명은 255 문자 max)
• 특이점: MySQL에서는 reserved word를 identifier로 이용가능.
– (quote로 묶기만 하면) reserved word를 identifier로 이용할 수 있다. (예:
TABLE이라는 이름의 table도 가능하다. 단, 권고되지는 않음)
– (Quote로 묶지 않아도 사용가능한 것) (예: DATE, TIMESTAMP를 column
명으로 사용하는 것) == ANSI SQL표준에는 맞지 않는데 일상에서는 간혹
사용
30
Create and Select a Database
• Create a Database
• create database employee;
• show databases;
• Select a Database
• use employee;
31
Create Tables
윤형기:
• TEMPORARY; 현재의 DB session에서만
visible하고 connection이 종료 시 삭제됨.
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)][table_options] [select_statement]
• LIKE old_table_name ; create a
new table with the same schema as
old_table_name.
• Other options:
• NOT NULL or NULL,
or
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name LIKE
old_table_name;
create_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT]
[PRIMARY KEY] [reference_definition]
or PRIMARY KEY (index_col_name,...)
or KEY [index_name] (index_col_name,...)
or INDEX [index_name] (index_col_name,...)
or UNIQUE [INDEX] [index_name] (index_col_name,...)
or FULLTEXT [INDEX] [index_name] (index_col_name,...)
or [CONSTRAINT symbol] FOREIGN KEY [index_name]
(index_col_name,...)
[reference_definition] or CHECK (expr)
• DEFAULT ; default value
• AUTO_INCREMENT ;  자동 index
됨. 또한 Indexes are
automatically created for
columns that are declared as
PRIMARY KEY(본 sample)
• REFERENCES ; foreign key
• PRIMARY KEY = a unique, indexed
column that cannot contain nulls.
• INDEX = KEY (단, unique value일 필요
는 없다.)
• UNIQUE ; … (단, UNIQUE columns will
also be indexed.)
• FULLTEXT ; create full-text indexes
on a TEXT, CHAR, or VARCHAR column
type. (MyISAM table only)
• FOREIGN KEY ; …
•
32
윤형기:
• Comma-separated list of
column declarations
enclosed in parentheses. (참
고) column의 attribute는
comma separated일 필요 없음.
• White space와 ; (생략)
drop database if exists employee;
create database employee;
use employee;
create table department
(
departmentID int not null auto_increment primary key,
name varchar(30)
) type=InnoDB;
• varchar와 char = 가변길이, 고
정길이
• not null, auto_increment
• type=InnoDB (Default는
MyISAM. InnoDB는 foreign
key와 transaction을 지원함)
•단, MyISAM이 다소 빠름
•References 의미 ;
departmentID should be
referenced back to the
departmentID column in the
department table. (foreign
key) –employee table이
InnoDB table이기 때문에 가능
create table employee
(
employeeID int not null auto_increment primary key,
name varchar(80),
job varchar(30),
departmentID int not null references department(departmentID)
) type=InnoDB;
33
create table employeeSkills
(
employeeID int not null references employee(employeeID),
skill varchar(15) not null,
primary key (employeeID, skill)
) type=InnoDB;
윤형기:
•
primary key
•
2-column primary key
create table client
(
clientID int not null auto_increment primary key,
name varchar(40),
address varchar(100),
contactPerson varchar(80),
contactNumber char(12)
) type=InnoDB;
34
create table assignment
(
clientID int not null references client(clientID),
employeeID int not null references employee(employeeID),
workdate date not null,
hours float,
primary key (clientID, employeeID, workdate)
) type=InnoDB;
35
show tables;
(output:)
+----------------------------+
| Tables_in_employee
|
+---------------------------+
| assignment
|
| client
|
| department
|
| employee
|
| employeeSkills
|
+--------------------------+
describe department;
(output:)
+----------------+----------------+----------------------+--------+-------+---------+-----------------+
| Field
| Type
| Collation
| Null
| Key
| Default |
Extra
|
+----------------+----------------+-----------------------+-------+-------+----------+----------------+
| departmentID | int(11)
| binary
|
| name
| latin1_swedish_ci
| YES |
| varchar(20)
| PRI
| NULL
| NULL
| auto_increment|
|
______
|
+-----------------+---------------+----------------------+-------+------+-----------+-----------------+
36
• table options
– MyISAM, (default)
• = very fast and supports full-text indexing.
–  previous standard ISAM type.
– InnoDB
• = ACID-compliant storage engine that supports transactions, foreign keys,
and row-level locking.
– BDB (Berkeley DB)
• a storage engine that supports transactions and page-level locking.
– HEAP tables
• are stored completely in memory and are never written to disk, so they are
very fast, but limited in size and are unrecoverable in the event of failure.
– MERGE tables
• allow you to combine a set of MyISAM tables with the same structure so that
they can be queried as if they were one table.
• 최대 파일/테이블 size의 한계를 극복하는 방법의 하나로 이용.
37
•
기타의 table options (주로 optimization목적)
; 1 이외의 값을 지정
– AUTO_INCREMENT = #
– AVG_ROW_LENGTH = #
;…
– CHECKSUM = 1
; 1 to turn it on. (default: Off) ; MyISAM table만 적용
– COMMENT = "string"
;…
– MAX_ROWS = #
;…
– MIN_ROWS = #
;…
– PACK_KEYS = {0 | 1 | DEFAULT}
• By default, MySQL packs (compresses) strings in keys (즉, CHARs, VARCHARs, and TEXT).
• 1 = all keys will be packed; 0 = none will be packed.
– PASSWORD = "string"
; does nothing in the standard version of MySQL.
– DELAY_KEY_WRITE = {0 | 1} ; key update를 table이 closed될 때까지 delay시킴.
• (단, MyISAM table에만 적용)
– ROW_FORMAT= {default | dynamic | fixed | compressed } ; specify the storage
format for rows. (단, MyISAM tables에만 적용).
– RAID_TYPE= {1 | STRIPED | RAID0 } RAID_CHUNKS=# RAID_CHUNKSIZE=#
• RAID configuration 설정 (optimization 용)
– UNION = (table_name,[table_name...]) ; only for MERGE tables
– INSERT_METHOD= {NO | FIRST | LAST }
; only for MERGE tables
– DATA DIRECTORY="absolute path to directory" ; …
– INDEX DIRECTORY="absolute path to directory" ; index보관장소 지정
•
(참고) CREATE TABLE 문장형식에서 마지막에 SELECT 문 사용하는 경우
38
–  to fill the new table with the data that is returned by the SELECT statement.
Column and Data Type
• (3 Basic column types)
– Numerical types / String or text types/ Date and time types
• Numerical types
– 개요
– int (integer=exact type) 및 float (floating-point = approximate numerical type). -표현형식을 지정: salary decimal(10, 2)
– UNSIGNED and/or ZEROFILL
» UNSIGNED ; column contains only zero or positive numbers.
» ZEROFILL ; number will be displayed with leading zeroes.
• NUMERIC or DECIMAL (= DEC)
– store exact floating-point values (예: monetary values.)
– same range as double-precision floating-point numbers.
• INTEGER and Variations (INT)
– = a standard integer, (in 4 bytes  a range of 232 possible values)
– several variations on INT: 1/2/3/4/8
» TINYINT (1 byte  28 possible values). = BIT = BOOL
» SMALLINT (2 bytes  216 possible values)
» MEDIUMINT is 3 bytes (224 possible values).
» BIGINT is 8 bytes (264 possible values).
• FLOAT
– = a single-precision floating-point number.
– (표현) 1.18x10-38 ~ 3.40x1038 또는 비슷한 범위의 negative numbers.
• DOUBLE
– = a double-precision floating-point number=REAL=DOUBLE PRECISION.
– (표현) 2.23x10-308 ~ 1.80x10308 또는 비슷한 범위의 negative numbers.
39
• String and Text Types
– CHAR
• store fixed-length strings.
• 길이를 ()로 지정. Max=255, Default=1
• 고정길이로 보관된 후 retreive될 때 space가 stripped 됨.
– CHAR takes up more space on disk than variable-length string.
– Trade-off: faster to retrieve rows (즉, CHAR, numeric, or date).
• Both CHAR and VARCHAR types can be preceded with the keyword NATIONAL,
meaning to restrict the contents to the standard character set. (default이므로
cross-platform compatibility에서만 의미가 있음)
• CHAR와 VARCHAR 모두 뒤에 BINARY를 표시할 수 있다. ; they should be treated as
case sensitive when evaluating string comparisons. (default는 case insensitive).
– VARCHAR
• variable-length strings. (0 to 255).
– TEXT, BLOB, and Variations
• TEXT types ; storing longer text than in a CHAR or VARCHAR.
• BLOB ; Binary Large OBject.
– These types are the same except that BLOBs are for binary data rather than text.
Comparisons on BLOBs are case sensitive, and on TEXTs, they are not.
• They are both variable in length, but both come in various sizes:
–
–
–
–
TINYTEXT or TINYBLOB ; up to 255 (that's 28-1) characters(bytes).
TEXT or BLOB ; up to 65,535 (216-1) characters or bytes (64KB).
MEDIUMTEXT or MEDUIMBLOB ; up to 16,777,215 (224-1) chars/ bytes (16MB).
LONGTEXT or LONGBLOB ; up to 4,294,967,295 (232-1) characters or bytes (4GB).
– ENUM
• a set of possible values. 예: gender enum('m', 'f') 단, NULL값을 가질 수 있으므로
실제는 m, f, NULL, or error.
– SET
40
• ENUM과 유사. 단, rows may contain a set of values from the enumerated set.
• Date and Time Types
– DATE
• in ISO year-month-day order, avoiding trans-Atlantic arguments.
• Dates are displayed as YYYY-MM-DD.
– TIME
• HH:MM:SS.
– DATETIME
• = a combination of the previous types.
• YYYY-MM-DD HH:MM:SS.
– TIMESTAMP
• If you do not set this column in a particular row, or set it to NULL, it
will store the time that row was inserted or last changed.
• When you retrieve a timestamp, it will be displayed in the DATETIME
format. (version 4.0 to 4.1과는 달라짐. …)
– YEAR
• you can declare it as YEAR(2) or YEAR(4) to specify the number of
digits. YEAR(4) is the default.
• YEAR(2) represents the range 1970 to 2069.
41
Creating Indexes
• 일반론
– PRIMARY KEY, KEY, UNIQUE, or INDEX 로 선언된 column은
will be indexed.
• CREATE INDEX
– CREATE INDEX statement is mapped to an ALTER TABLE
statement before being executed.
– (예:) create index name on employee(name);
– create index statement에는 option이 많지 않다.
• CREATE UNIQUE index ; enforce a uniqueness constraint.
• CREATE FULLTEXT ; create a full-text index on a MyISAM table.
• Limit indexes on char and varchar types to index just the first
few characters in each field.
– 예: create index part_name on employee(name(5));
– 이유: indexes on text types are not as efficient as indexes on
numeric types, and just indexing the first few characters improves
performance.
42
Databases, Tables, Index의 삭제
• (명령어) drop database employee;
– optional IF EXISTS clause before the database name
• DROP TABLE statement:
– DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name,...]
• TEMPORARY ; for dropping temporary tables.
• 기타 생략
• DROP INDEX statement
– drop index part_name on employee;
43
Altering Existing Table Structures
• ALTER TABLE
윤형기: 특이사항만 검토:
• CHANGE / MODIFY (공통); column
definition or table에서의 position 변
ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec ...]
경.
• DROP COLUMN ; deletes a column
alter_spec:
from the table (비교) DROP PRIMARY
ADD [COLUMN] create_definition [FIRST | AFTER col_name ]
KEY and DROP INDEX delete just the
or ADD [COLUMN] (create_definition, create_definition,...)
associated index for that column.
or ADD INDEX [index_name] (index_col_name,...)
• DISABLE KEYS ; stop updating
indexes for a MyISAM table only.
or ADD PRIMARY KEY (index_col_name,...)
ENABLE KEYS turns index updating
or ADD UNIQUE [index_name] (index_col_name,...)
back on.
or ADD FULLTEXT [index_name] (index_col_name,...)
• RENAME ; change name of a table.
or
ADD [CONSTRAINT symbol] FOREIGN KEY [index_name]
• ORDER BY ; put the rows in the
(index_col_name,...)
newly altered table in a particular
[reference_definition]
order (This order will not be
or ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
maintained as the data in the table
or CHANGE [COLUMN] old_col_name create_definition
changes over time.)
[FIRST | AFTER column_name]
• table_options ; lets you specify
the same table options as at the
or MODIFY [COLUMN] create_definition [FIRST | AFTER col_name]
end of the CREATE TABLE
or DROP [COLUMN] col_name
statement.
or DROP PRIMARY KEY
or DROP INDEX index_name
or DISABLE KEYS
or ENABLE KEYS
44
or RENAME [TO] new_tbl_name
or ORDER BY col_name or table_options
5. Inserting, Deleting, and
Updating Data
45
Listing 5.1 employee_data.sql
use employee;
delete from department;
insert into department values
(42, 'Finance'),
(128, 'Research and Development'),
(NULL, 'Human Resources'),
(NULL, 'Marketing');
delete from employee;
insert into employee values
(7513,'Nora Edwards','Programmer',128),
(9842, 'Ben Smith', 'DBA', 42),
(6651, 'Ajay Patel', 'Programmer', 128),
(9006, 'Candy Burnett', 'Systems Administrator', 128);
46
delete from employeeSkills;
insert into employeeSkills values
(7513, 'C'),
(7513, 'Perl'),
(7513, 'Java'),
(9842, 'DB2'),
(6651, 'VB'),
(6651, 'Java'),
(9006, 'NT'),
(9006, 'Linux');
delete from client;
insert into client values
(NULL, 'Telco Inc', '1 Collins St Melbourne', 'Fred Smith',
'95551234'),
(NULL, 'The Bank', '100 Bourke St Melbourne', 'Jan Tristan',
'95559876');
delete from assignment;
insert into assignment values
(1, 7513, '2003-01-20', 8.5);
47
select * from department;
+------------------+---------------------------+
| departmentID |
name
|
+------------------+---------------------------+
|
42
| Finance
|
|
128
|Research and Develop
|
|
129
| Human Resources
|
|
130
| Marketing
|
+-----------------+-----------------------------+
4 rows in set (0.01 sec)
48
윤형기:
• LOW PRIORITY or DELAYED.
(general form of the INSERT statement)
INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ((expression | DEFAULT),...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expression, ... ]
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name
SET col_name=(expression | DEFAULT), ...
[ ON DUPLICATE KEY UPDATE
col_name=expression, ... ]
; (공통) insertion will be delayed until no client is
trying to read from the table.
; (차이점) LOW PRIORITY will block the inserting
client and DELAYED will not. 즉,
•LOW PRIORITY insert; 잠시 기다린 후 query수
행
•DELAYED; you will be told OK and can
continue running queries, but you need to
remember that the insert will not be
performed until the table is not in use.)
• IGNORE ; chiefly useful when inserting multiple
rows. 보통 신규 입력 row가 기존의 row의 PRIMARY
KEY or UNIQUE value와 clash되면 error표시 후 insert
자체가 abort되지만 IGNORE로 지정하면, error will be
ignored and the insert will continue and will attempt
to insert the next row.
• DEFAULT ; column의 default value 지정.
• ON DUPLICATE KEY UPDATE ; (뒷장)
49
•ON DUPLICATE KEY UPDATE
– Primary key 또는 unique value가 clash될 때 적절한 처리방
법 제시. 즉, change the primary key or unique value in
the row already in the table so that it no longer clashes
with the new row.
– (예:)
create table warning
(
employeeID int primary key not null
references employee(employeeID),
count int default 1
) type =InnoDB;
insert into warning (employeeID)
values (6651)
on duplicate key update
count=count+1;
윤형기:
(다음 상황에 유용) unique event 발생 시 이를 기록
(record)할 뿐 아니라 어떤 action을 취하고자 할 때
(예1) increment a counter when non-unique
events occur.
(예2) 각종 logging (위의 employee DB에서 we will
record employees who have been given a
warning in the table warning.)
어떤 사람의 warning을 기록할 때 count의 default
값이 1일 때, 추가의 (subsequent) insert 발생 시
(동일 employeeID에 대해) ON DUPLICATE KEY UPDATE
clause 에 의해 counter 값을 증가.
50
Using REPLACE
(general form of REPLACE)
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES (expression,...),(...),...
or REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...
or REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name=expression, col_name=expression,...
51
Using DELETE
• delete from department;
– delete all the rows from the table.
– 실수에 대비:–-safe-updates 또는 –-i-am-a-dummy option
• delete from department where name='Asset Management';
윤형기:
delete employee, employeeSkills
DELETE [LOW_PRIORITY] [QUICK] FROM table_name
[WHERE where_definition]
[ORDER BY ...]
[LIMIT rows]
or
DELETE [LOW_PRIORITY] [QUICK] table_name[.*]
[, table_name[.*] ...]
FROM table-references
[WHERE where_definition]
or DELETE [LOW_PRIORITY] [QUICK]
FROM table_name[.*] [, table_name[.*] ...]
USING table-references
[WHERE where_definition]
from employee, employeeSkills, department
where employee.employeeID = employeeSkills.employeeID
and employee.departmentID = department.departmentID
and department.name='Finance';
delete from employee, employeeSkills
using employee, employeeSkills, department
where employee.employeeID = employeeSkills.employeeID
and employee.departmentID = department.departmentID
and department.name='Finance';
(다른 optional clauses)
· * LOW_PRIORITY clause; (생략)
· * QUICK ; DELETE 시 index에 대한 housekeeping 일부작업 생략
 speed up
· ; ORDER BY ; row 삭제시의 순서 지정 - LIMIT 문과 함께 사용하
면 유용 —(예) delete the oldest n rows from a table.
52
; LIMIT ; DELETE 로 삭제될 수 있는 최대 row 숫자를 설정
Using TRUNCATE
• delete all the rows from a table.
• 예: TRUNCATE TABLE employee;
• DELETE 문보다 빠르다. ( works by dropping the
table and re-creating it empty. )
• 단, TRUNCATE is not transaction safe.
53
Using UPDATE
(general form of the UPDATE statement)
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
[ORDER BY ...]
[LIMIT rows]
or
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name [, tbl_name ...]
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
• 특이사항 없음.
54
Uploading Data with LOAD DATA INFILE
(general form of the LOAD DATA INFILE statement)
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL]
INFILE 'fileName.txt‘
[REPLACE | IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY '\t']
[[OPTIONALLY] ENCLOSED BY '']
[ESCAPED BY '\\' ]
]
[LINES TERMINATED BY '\n']
[IGNORE number LINES]
[(col_name,...)]
•
•
bulk insert data from a text file into a single table
(INSERT 대신)
Listing 5.2 department_infile.txt
42
Finance
128 Research and Development
NULL Human Resources
NULL Marketing
load data local infile 'department_infile.txt‘
into table department;
•
•
윤형기:
optional clauses:
· * LOW PRIORITY clause ; (생략)
* CONCURRENT allows other clients to read
from the table while the bulk insert is going on.
· * LOCAL; means the data file is on client
machine. (LOCAL이 없으면 MySQL은 서버에서
infile 을 찾음)
· *
데이터 insert과정에서 clash 발생 시 2가지 대처:
- REPLACE replace old row with new row,
- IGNORE tells MySQL to keep old row.
· * FIELDS and LINES ; infile에 수록된 데이터가
layout되는 방법을 지정. 일반적으로는 default값 이
용.—each row on a new line, column values
separated by tabs. We can also enclose column
values in quotes and use the backslash
character to escape any special characters (like
single quotes) that might confuse MySQL.
·
* IGNORE number LINES ; ignore the first
number lines in the infile.
·
* 마지막 줄의 col_name; allows you to specify
that you only want to read data into some of
the table's columns.
다른 DB format, spreadsheet, CSV
file 로부터 data convert하는 데에 유용.
(주의) FILE privilege가 필요함.  security reasons (예) to stop
from loading in /etc/passwd
55
• Listing 5.3 new_programmers.csv
Name,Job,DepartmentID
Julia Lenin,Programmer,128
Douglas Smith,Programmer,128
Tim O'Leary,Programmer,128
• Load this data into employee table with:
load data infile 'e:\\new_programmers.csv‘
into table employee
fields terminated by ',‘
lines terminated by '\n‘
ignore 2 lines
(name, job, departmentID);
• (주요사항)
– Windows/DOS-style path에서의 backslash처리
'e:\\new_programmers.csv'.
– CSV file are terminated by commas
– first two lines as a header should be ignored.
56
6. Querying MySQL
57
Overview of SELECT
•
General form:
SELECT columns
FROM tables
[WHERE conditions]
[GROUP BY group
[HAVING group_conditions]]
[ORDER BY sort_columns]
[LIMIT limits];
•
•특정 column의 선택
select name, employeeID from
select * from department; 
employee; 
+--------------------+---------------------------------+
+-----------+----------+
| departmentID
|
name
|
| name
| employeeID |
+--------------------+---------------------------------+
+-----------+----------+
|
42
| Finance
|
| Ajay Patel
|
6651 |
|
128
| Research and Development
|
| Nora Edwards |
7513 |
|
129
| Human Resources
|
| Candy Burnett |
9006 |
|
130
| Marketing
|
| Ben Smith
|
9842 |
+--------------------+---------------------------------+
+------------+---------+
4 rows in set (0.00 sec)
4 rows in set (0.00 sec)
Simple Queries
58
• Specifying Absolute Databases and Tables
select employee.name from employee; 
+-----------------+
| name
|
+-----------------+
| Ajay Patel
|
| Nora Edwards |
| Candy Burnett |
| Ben Smith
|
+------------------+
4 rows in set (0.41 sec)
– 마찬가지로,
• select name from employee.employee; 
• (위와 동일한 결과)
– 가급적 database.table.column syntax:
• select employee.employee.name from employee;
59
• Aliases
– rename columns or expressions in a SELECT statement
select name as employeeName
from employee; 
+----------------------+
| employeeName
|
/* 여기서 employeeName이 alias이다 */
+----------------------+
| Ajay Patel
|
| Nora Edwards
|
| Candy Burnett
|
| Ben Smith
|
+----------------------+
4 rows in set (0.01 sec)
– 다른 예:
• select e.namefrom employee as e;  Alias를 쓰던 말던 결과는 같다.
– 앞의 예에서 keyword AS는 optional. 즉,
select name employeeName from employee;
select e.name from employee e;
AND
60
특정 Row선택을 위한 WHERE Clause 사용
• 간단한 예:
select employeeID, name  +----------------+-----------------+
from employee
| employeeID
| name
|
where job='Programmer';
+-----------------+----------------+
|
6651
| Ajay Patel
|
|
7513
| Nora Edwards |
+-----------------+----------------+
2 rows in set (0.42 sec)
– 사용할 수 있는 주요 operators:
• =, <>, >, <, >=, <= (비교 시 수식 적용 가능)
(예: somevalue > someothervalue*10)
• IS NULL , IS NOT NULL (이것을 somevalue=NULL 여부 test에 사용 불가.)
• 표준 Boolean operators (AND, OR, NOT) 사용가능. 단, 비교 operator보다는
후 순위 (예: salary > 30000 AND salary < 50000)
– 많이 쓰이는 함수: count() ; query에 따른 row 숫자 count
(예) select count(*) from employee;  “how many rows in …”
– 또한 precedence 조절 by grouping expressions with parentheses. 예:
select * from assignment
where employeeID=6651 and hours > 8;
– (주의) WHERE 에는 column aliases를 사용불가(=ANSI SQL 한계)
– 이유: alias 처리된 column 값은 WHERE 조건이 검사되는 시점에는 알 수 없는 상태
61
Removing Duplicates with DISTINCT
• 예:
select count(job) from employee;
+--------------+
| count(job) |
+--------------+
|
4
|
+--------------+
1 row in set (0.01 sec)
• Misleading! 정확한 방법은:
select count(distinct job) from employee;
+------------------------+
| count(distinct job)
|
+------------------------+
|
3
|
+-----------------------+
1 row in set (0.05 sec)
• 즉, how many different values are in the job column
62
Using the GROUP BY Clause
select count(*), job
from employee
group by job; 
+------------+-----------------------------+
| count(*) | job
|
+------------+-----------------------------+
|
1
| DBA
|
|
2
| Programmer
|
|
1
| Systems Administrator |
+------------+-------------------------------+
3 rows in set (0.04 sec)
• MySQL and ANSI SQL 에서의 2가지 차이점:
– ANSI SQL; you must group by all the columns you have listed in the
initial SELECT clause.
– MySQL; you can have additional fields in the SELECT clause that are
not in the GROUP BY clause. 또한 group order의 sort도 가능. 예:
select count(*), job
from employee
group by job desc; 
+----------+-----------------------------+
| count(*) | job
|
+----------+-----------------------------+
|
1 | Systems Administrator
|
|
2 | Programmer
|
|
1 | DBA
|
+----------+-----------------------------+
3 rows in set (0.04 sec)
63
•
Selecting Particular Groups with HAVING
– GROUP BY with a HAVING clause ; SELECT with a WHERE clause와 유사. 예:
select count(*), job
from employee
group by job
having count(*)=1; 
+----------+--------------------------------+
| count(*) | job
|
+----------+--------------------------------+
|
1 | DBA
|
|
1 | Systems Administrator |
+----------+--------------------------------+
2 rows in set (0.05 sec)
– WHERE와 HAVING
• WHERE ; 개별 row에 대한 조건 test로서 일상적으로 많이 사용됨.
• HAVING; whole group에 대한 조건 테스트.
•
Sorting Search Results with ORDER BY
select *
from employee
order by job asc, name desc; 
+-----------------+-------------------+-------------------------------+-------------------+
| employeeID
| name
| job
| departmentID |
+-----------------+-------------------+-------------------------------+-------------------+
|
9842
| Ben Smith
| DBA
|
42
|
…
|
9006
| Candy Burnett
| Systems Administrator
|
128
|
+-----------------+--------------------+------------------------------+-------------------+
4 rows in set (0.02 sec)
64
– Default는 ASC. 단, ORDER BY 를 지정하지 않으면 산출물의 순서는 장담할 수 없다.
Limiting Search Results with LIMIT
– 예:
select *
from employeeSkills
limit 5; /* only the first five rows that match the selection criteria */
+-----------------+-------+
| employeeID
| skill |
+-----------------+-------+
|
6651
| Java |
|
6651
| VB
|
|
7513
|C
|
|
7513
| Java |
|
7513
| Perl |
+-----------------+-------+
5 rows in set (0.44 sec)
– 다른 예 (to retrieve rows 6 through 8 from the preceding query):
select *
from employeeSkills
limit 5, 3; /* 앞 파라미터=offset (start point), 뒤 파라미터 = 원하는 max no of rows*/
– Row numbering starts from zero when specifying offsets (앞의 예에서 6th row
는 offset 5로 표현). (앞의 예에서는 rows 0 to 4, 뒤의 예에서는 rows 5 to 7.)
– second parameter가 -1 이면 offset 부터 table의 끝까지 생성됨.
– LIMIT 와 ORDER BY를 함께 사용하면 유용. (단, ORDER BY 없이 사용하면 아무런
논리적 순서 없이 record를 retrieve함.)
65
– 특히 Web or GUI applications 개발에 유용.  easy mechanism for paging
7. Advanced Queries
• 여러 table에 대한 join
– Natural, inner, and cross joins
– Straight joins
– Left and right joins
• Subquery의 작성과 이용
• SELECT문에서의 options
66
Using Joins to Run Queries over Multiple Tables
•
•
(개요) RDBMS의 특징=relationships= table들 사이의 link로부터 정보선택=join
Joining Two Tables
– 예 (employee name과 그들이 근무하는 department name):
select employee.name, department.name
from employee, department
where employee.departmentID = department.departmentID; 
+---------------------+-----------------------------------+
| name
| name
|
+---------------------+-----------------------------------+
| Ben Smith
| Finance
|
…
+--------------------+-------------------------------------+
4 rows in set (0.42 sec)
– WHERE 없이 사용하면 “all possible combinations of … = Cartesian product of the two tables.
– 이 예에서는 employee.departmentID = department.departmentID가 join condition (= the
link between the tables based on the foreign keys in our original schema).
– 또한 readability를 위해서는 aliases사용.
select employee.name as employeeName, department.name as departmentName
from employee, department
where employee.departmentID = department.departmentID; 
+----------------------+--------------------------+
| employeeName
| departmentName
|
+----------------------+--------------------------+
| Ben Smith
| Finance
|
…
67
+---------------------+---------------------------+
4 rows in set (0.55 sec)
•
Joining Multiple Tables
– 예: Telco Inc.라는 고객에 할당된 department's employees를 찾고자 함.
– 해법: client name > clientID > assignments in the assignment table >
employeeIDs from the assignment table > ids of the departments they work for
from employee table > department name from department table
select department.name
from client, assignment, employee, department
where client.name='Telco Inc‘
and client.clientID = assignment.clientID
and assignment.employeeID = employee.employeeID
and employee.departmentID = department.departmentID; 
+------------------------------------+
| name
|
+------------------------------------+
| Research and Development
|
+------------------------------------+
1 row in set (0.00 sec)
– 이 예에서 … (guideline) If you are joining n tables, in most cases, you will have a
link between each pair of tables, and therefore have n-1 join conditions. (Fig 7.1)
68
• Self Joins - Joining a Table to Itself
– 이것도 가능하다.
• Find Nora Edwards와 같은 부서(department)에서 근무하는 사람.”
• (해법) find the departmentID Nora works for from the employee table >
employee table에서 그 deparment에 근무하는 employee를 찾아 냄.
select e2.name
from employee e1, employee e2
where e1.name = 'Nora Edwards‘
and e1.departmentID = e2.departmentID;
• 이 예에서 employee table에 대해 2개의 aliases 선언  pretend we have two
separate tables, e1 and e2, which just happen to contain the same data.
그런 후 simply join them as we would any two other tables.
+-------------------+
| name
|
+-------------------+
| Ajay Patel
|
| Nora Edwards |
| Candy Burnett |
+-------------------+
3 rows in set (0.00 sec)
– 여기서 Nora를 빼려면 다음과 같이 수정
select e2.name
from employee e1, employee e2
where e1.name = 'Nora Edwards‘
and e1.departmentID = e2.departmentID
and e2.name != 'Nora Edwards';
69
Join Type의 종류
• Basic Join
– 앞에서의 Cartesian product = full join = cross join ; 
• complete set of combinations.( FROM 과 comma로 table 나열)
– Join에 조건문을 붙여서 결과를 한정시키는 것 = equijoin
• (예: employee.departmentID = department.departmentID)
– Consider our original query:
select employee.name, department.name
from employee, department
where employee.departmentID = department.departmentID;
– Optionally, comma대신 keyword JOIN을 사용 가능
select employee.name, department.name
from employee join department
where employee.departmentID = department.departmentID;
– JOIN대신 CROSS JOIN or INNER JOIN 사용가능.
• 이들 join 수행 시 MySQL looks at the tables we are joining and tries
to work out the most efficient way to join them together, rather
than necessarily joining the tables in the order we have listed. 간혹
이러한 query optimization이 의도대로 되지 않는 경우도 있다.
• If you want to override the optimizer and force to join tables70in the
order in which you list them, replace JOIN with STRAIGHT JOIN.
• Understanding LEFT and RIGHT Joins
– 앞서 equijoin의 경우 JOIN, CROSS JOIN, INNER JOIN, or STRAIGHT JOIN을 사용.
– 예: <<한 table에는 있는데 다른 table에는 해당 record가 없는 row를 찾음>>
– find employees who have not yet worked on any outside assignments!
select employee.name
from employee left join assignment
on employee.employeeID = assignment.employeeID
where clientID is null; 
+-------------------+
| name
|
+-------------------+
| Ajay Patel
|
| Candy Burnett |
| Ben Smith
|
+-------------------+
3 rows in set (0.49 sec)
– left join
• take the left-hand table in the join (여기서는 employee) and try to match it to rows in
the right-hand table. (Join에서의 왼쪽 table을 오른 쪽 table에 match시킴. )
•  Right table에 matching되는 row가 없는 left table의 각각의 row에 대해서는 LEFT JOIN
substitutes a row of NULL values. (따라서 NULL key value 를 검색하면 right table에
matching row가 없는 left table의 row를 찾을 수 있다.) 즉, “dummy row”를 생성 시킴.
(The clientID is a key field, so this should never occur in the assignment table.)
• 앞의 예에서: When an employee has no matching row in the assignment table, the left
join will make up a "dummy row" consisting of all NULLs.  find these dummy rows by
looking for employees who have worked on an assignment for which clientID is NULL.
71
– 이 예에서 RIGHT JOIN을 이용할 수도 있다. (uses the right table as a base and fills
any missing rows from the left table with NULLs.)
Writing Subqueries
• (개요)
– Subquery = a query within a query = nested queries = subselect
– 새 기능은 없지만 (복잡한 set of joins대신 사용되어서) readability 개선.
• Using Derived Table Subqueries
– list a query in the FROM clause of another query.  create a
temporary table and add it to the query. 예:
select employeeID, name from employee where job='Programmer';
– 이것을 다음 경우에 활용 가능하다.
select programmer.name
from (select employeeID, name from employee where job='Programmer')
as programmer,
assignment
where programmer.employeeID = assignment.employeeID; 
+-------------------+
| name
|
+-------------------+
| Nora Edwards
|
+-------------------+
1 row in set (0.01 sec)
72
• Using Single-Value Subqueries
select max(hours) from assignment;
– 이것을 다음 경우에 활용
select e.employeeID, e.name
from employee e, assignment a
where e.employeeID = a.employeeID
and a.hours = (select max(hours) from assignment); 
+-----------------+------------------+
| employeeID
| name
|
+-----------------+------------------+
|
7513
| Nora Edwards |
+-----------------+------------------+
1 row in set (0.42 sec)
• Subqueries가 single value가 아닌 row를 산출할 수도 있다.
73
• Boolean Expression Subqueries
– (용도) check our query against some special functions that
return a Boolean expression. (예:IN, EXISTS, (grouped
together) ALL, ANY, and SOME.) 예:
select name
from employee
where employeeID not in
(select employeeID
from assignment);
– 앞서의 LEFT JOIN활용과 같은 효과 (같은 결과 생략)
– 다른 예:
select name
from employee
where employeeID not in (6651, 1234);
74
– EXISTS keyword (IN 과는 달리 사용)
• use data from the outer query in the subquery. = a correlated subquery.
• 예 (outside assignment에 근무한 적이 없는 employee를 찾고자 함)
select e.name, e.employeeID
from employee e
where not exists
(select *
from assignment
where employeeID = e.employeeID);
– Subquery에서, …
• e.employeeID comes from the outer query. For each row in the employee table,
we check the results of the subquery, and if there is no matching row (WHERE
NOT EXISTS), we add the employee's details to the result set.
– 같은 결과를 앞서의 LEFT JOIN을 이용할 수도 있다. (오히려 더 효율적)
+---------------+------------------+
| name
| employeeID
|
+---------------+------------------+
| Ajay Patel
|
6651
|
…
+----------------+-----------------+
3 rows in set (0.00 sec)
– ALL, ANY, and SOME keywords
• Subquery의 결과를 비교 (예: Nora Edwards, hardest working programmer, wants
to establish that nobody works longer hours than the programmers.)
select e.name from employee e, assignment a
where e.employeeID = a.employeeID
and a.hours > all
(select a.hours
from assignment a, employee e
where e.employeeID = a.employeeID
and e.job='Programmer');
75
Using SELECT Statement Options
SELECT [STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] [HIGH_PRIORITY]
[DISTINCT | DISTINCTROW | ALL]
select_expression,...
[INTO {OUTFILE | DUMPFILE} 'file_name' export_options]
[FROM table_references
[WHERE where_definition]
[GROUP BY {unsigned_integer | col_name | formula} [ASC | DESC], ...]
[HAVING where_definition]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...]
[LIMIT [offset,] rows | rows OFFSET offset]
[PROCEDURE procedure_name(argument_list)]
[FOR UPDATE | LOCK IN SHARE MODE]]
•
•
STRAIGHT JOIN ; force query optimizer to join the tables in the order specified. (=extension to ANSI SQL)
SQL_SMALL_RESULT, SQL_BIG_RESULT, SQL_BUFFER_RESULT options ; optimization 용
–
–
•
•
SQL_CACHE and SQL_NOCACHE ; 결과치를 cache 처리할 것인지의 지정. (extension to ANSI SQL.) ??
SQL_CALC_FOUND_ROWS ; (LIMIT clause와 함께 사용. )
–
•
•
•
•
•
•
SQL_SMALL_RESULT and SQL_BIG_RESULT; tells MySQL that you expect the result set to have either few rows or a large
number of them.
SQL_BUFFER_RESULT; put the result set into a temporary table. (결과를 client에 보내는데 너무 많은 시간이 소요되는 경우) (=
MySQL extensions to ANSI SQL)
it tells MySQL to work out how many rows would have been returned if there had been no LIMIT clause. We can then
retrieve this number with select found_rows(); (another extension to ANSI SQL). In versions without it, a common task is to
run a COUNT(*) query and then a SELECT with a LIMIT.
HIGH PRIORITY; priority over any UPDATE statements that are waiting to use the involved tables.
DISTINCTROW = DISTINCT (단, ALL is the opposite (return all duplicates) and is the default option.)
SELECT INTO OUTFILE ; SELECT 문의 결과를 지정된 file로 보냄.(LOAD DATA INFILE 의 반대)
export_options = options in LOAD DATA INFILE (see Chapter 5 for details).
PROCEDURE ; 결과가 client에 보내지기 전에 수행되어질 procedure 지정 (C++)
FOR UPDATE and LOCK IN SHARE MODE ; storage engine이 page- or row-level locking을 사용할 때만 영향.
–
–
76
실제로는 InnoDB 와 BDB.
(If you specify FOR UPDATE, you will set an exclusive lock, and if you use LOCK IN SHARE MODE, you will set a shared lock. )
8. 내장함수와 SELECT의 이용
77
• (일반론) …
– …
– MySQL에서는 any expression containing NULL will evaluate
to NULL, with a few exceptions.
– SELECT 를 basic calculator로 사용도 가능.
select 2+2; 
+-------+
| 2+2 |
+-------+
| 4
|
+-------+
1 row in set (0.42 sec)
78
Operators
•
Arithmetic Operators
•
Comparison Operators
– +, -, *, / (단,Division by zero produces a safe NULL result.)
– 특이점(1): 일부 예외를 빼고 comparing anything to NULL gives a NULL result.
(NULL과 NULL의 비교 포함):
select NULL=NULL;
+----------------+
| NULL=NULL
|
+----------------+
|
NULL
|
+----------------+
1 row in set (0.00 sec)
– 비교: NULL이 포함된 비교operator사용 시에는 주의할 것
select NULL IS NULL;
+-------------------+
| NULL IS NULL
|
+-------------------+
|
1
|
+-------------------+
1 row in set (0.00 sec)
– 특이점(2)
• MySQL에서 대부분의 string comparisons 는 case insensitive 이므로 case-sensitive 필요 시
는 prefix one of them with the keyword BINARY. 예:
select * from department where name='marketing';
≠ select * from department where name = binary 'marketing'; 
• <계속>
79
Table 8.1. Comparison Operators
Operator
Meaning
=
Equality
!= or <>
Inequality
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
n BETWEEN
min AND max
Range testing
n IN (set)
Set membership. Can be used with a list of literal values or expressions or with a subquery as the
set. 예: (apple, orange, pear)
<=>
NULL safe equal. This will return 1 (true) if we compare two NULL values
n IS NULL
test for a NULL value in n
ISNULL(n)
test for a NULL value in n
80
•
Logical Operators
– MySQL interprets any nonzero, non-null value as true.
– Logical expressions in MySQL can evaluate to 1 (true), 0 (false), or NULL.
– NULL 이 관련된 경우 일반적인 진리표 (truth table)와 다른 경우가 있다.
Table 8.2. Logical Operators
Operator 예
의미
진리표
AND or &&
n && m
Logical AND
true&&true = true
false&&anything = false
All other expressions evaluate to NULL.
OR or ||
n || m
Logical OR
true || anything = true
NULL||false = NULL
NULL||NULL = NULL
false||false = false
NOT or !
NOT n
Logical NOT
!true = false
!false = true
!NULL = NULL
XOR
n XOR m
Logical exclusive OR
true XOR true = false
true XOR false = true
false XOR true = true
NULL XOR n = NULL
n XOR NULL = NULL
81
Control Flow Functions
• IF (e1, e2, e3)
– If e1 이 true이면 e2를 return하고 otherwise, it returns e3.
select name, if(job='Programmer', "nerd", "not a nerd")
from employee; 
+-------------------+-----------------------------------------------------+
| name
| if(job='Programmer', "nerd", "not a nerd")
|
+------------------+------------------------------------------------------+
| Ajay Patel
| nerd
|
…
+------------------+------------------------------------------------------+
4 rows in set (0.00 sec)
• CASE
– 예:
CASE value
WHEN [compare-value] THEN result
[WHEN [compare-value] THEN result ...]
[ELSE result]
END
CASE
WHEN [condition] THEN result
[WHEN [condition] THEN result ...]
[ELSE result]
END
select workdate, case
when workdate < 2000-01-01 then "archived“
when workdate < 2003-01-01 then "old“
else "current“
end
from assignment;
82
• /* Assignments from the last century are categorized as "archived", ones prior
to this year are categorized as "old", and everything else is "current". */
String Functions
Table 8.3. String Processing Functions
Function
Purpose
concat(s1, s2, ...)
Concatenate the strings in s1, s2, ....
conv (n, original_base,
new_base)
Convert the number n from original_base to new_base. (It may surprise you to see this as a string
function, but some bases use letters in their notations, such as hexadecimal.)
length(s)
Returns the length in characters of the string s.
load_file(filename)
Returns the contents of the file stored at filename as a string.
locate(needle, haystack,
position)
Returns the starting position of the needle string in the haystack string. 검색 시작은 position부터.
lower(s) and upper(s)
Convert the string s to lowercase or uppercase.
quote(s)
Escapes a string s so that it is suitable for insertion into the database. This involves putting the string
between single quotes and inserting a backslash.
replace(target, find, replace)
Returns a string based on target with all incidences of find replaced with replace.
soundex(s)
Returns a soundex string corresponding to s. A soundex string represents how the string sounds
when pronounced. It can be easier to match soundex strings of names than names themselves.
substring (s, position,
length)
Returns length characters from s starting at position.
trim(s)
Removes leading and trailing whitespace from s. ((참고) use ltrim() to just remove whitespace
83 from
the left or rtrim() for the right.)
String Comparison Functions
• Using LIKE for Wildcard Matching
select *
from department
where name like '%research%';
/* names containing 'research'. */
+--------------------+------------------------------------+
| departmentID
| name
|
+-------------------+-------------------------------------+
|
128
| Research and Development
|
+-------------------+-------------------------------------+
1 row in set (0.04 sec)
– two kinds of wildcard matching.
• %; matches any number of characters (including zero).
– '%research%' matches all strings that have the word research in them
somewhere. (case insensitive)
• underscore (_); matches any single character.
– '_at' matches the strings 'cat', 'mat', 'bat', and so on.
84
• Using RLIKE for Regular Expression Matching
– to match on the basis of regular expressions.
• regular expression = a pattern that describes the general shape of a string.
• List of strings
– The pattern 'cat' matches 'cat‘, but also 'catacomb' and 'the cat sat on the mat'.
– If we want to match only the word 'cat', then the pattern would need to be '^cat$'. The
caret (^) means "anchor to the start of the string;" (=the first thing at the start of a
matching string is the word 'cat‘). The dollar sign ($) means "anchor to the end of the
string;" (=the last thing in the string must be the word 'cat‘).
 '^cat$' can match only the string 'cat' and nothing else.
– 단, 오직 한 개의 wildcard 지원: the dot (.), that will match any single character.
» '.at' matches 'cat', 'bat', 'mat' and so on  specify how often characters (wildcard 포함)
can appear in a string. (예:'n*' matches '', 'n', 'nn', 'nnn', …)
» '(cat)*‘;matches '', 'cat', 'catcat', 'catcatcat', and so on.
– '.*' ; matches any number of any characters—basically anything.
– (+) ; means that the character or string before it should be repeated one or more times,
– (?) ; means to match zero times or one time.
– You can also list a specific range, so, for example, '(cat)(2,4)' matches 'catcat', 'catcatcat',
and 'catcatcatcat'.
• 또한 list sets of characters.
– '[a-z]' matches any single letter, and '[a-z]*' matches any number of letters.
• character class=predefined set. 예:[[:alnum:]] matches any alphanumeric character.
• (단, MySQL uses POSIX-style regular expressions ≠ Perl regular expressions.
– 예:
select * from department where name rlike 'an'; /* containing 'an' inside them:*/
+-------------------+--------------------------+
| departmentID | name
|
+-------------------+--------------------------+
….
85
+-------------------+--------------------------+
3 rows in set (0.00 sec)
•
Using STRCMP() for String Comparison
– STRCMP(s1, s2) returns the following values:
• 0 if the strings are equal
• -1 if s1 is less than s2? that is, if it comes before s2 in the sort order
• 1 if s1 is greater than s2? that is, if it comes after s2 in the sort order
• 예:
select strcmp('cat', 'cat');
+-----------------------+
| strcmp('cat', 'cat') |
+----------------------+
|
0
|
+----------------------+
1 row in set (0.42 sec)
select strcmp('cat', 'dog');
+-----------------------+
| strcmp('cat', 'dog') |
+-----------------------+
|
-1
|
+-----------------------+
1 row in set (0.00 sec)
select strcmp('cat', 'ant');
+-----------------------+
| strcmp('cat', 'ant') |
+-----------------------+
|
1
|
+-----------------------+
1 row in set (0.00 sec)
– (참고) sort order changes with the character set.  internationalization
86
Numeric Functions
Table 8.4. Numeric Functions
Function
Purpose
abs(n)
absolute value of n
ceiling(n)
the value of n rounded up to the nearest integer.
floor(n)
the value of n rounded down to the nearest integer.
mod(n,m)
and div
divide n by m.
단, div returns the integral quotient, and mod() returns the integral remainder.
power(n,m)
Returns n to the power of m.
rand(n)
Returns a random number between 0 and 1. parameter n 은 optional로서 pseudorandom number 생성의
seed로 사용 (Giving the same n to rand will produce the same pseudorandom number.)
round(n[,d])
Returns n rounded to the nearest integer. If you supply d, n will be rounded to d decimal places.
sqrt(n)
Returns the square root of n.
87
– 주의:
• mod(9,2) or 9 mod 2 or even 9 % 2 모두가 가능하지만
• 9 div 2 만 가능하다. (예: div(9, 2) a syntax error.)
– mod and div in MySQL gives the following results:
mysql> select 9 mod 2;
+-----------+
| 9 mod 2 |
+-----------+
|
1
|
+-----------+
1 row in set (0.00 sec)
select 9 div 2;
+----------+
| 9 div 2 |
+----------+
|
4 |
+----------+
1 row in set (0.00 sec)
88
Date and Time Functions
Table 8.5 Date and Time Functions
Function
Purpose
adddate(date, INTERVAL n type) 및
subdate(date, INTERVAL n type)
 add and subtract dates.
Type ; SECOND, MINUTE, HOUR, DAY, MONTH, YEAR, MINUTE:SECOND
(format of n should be 'm:s'), HOUR:MINUTE ('h:m'), DAY_HOUR ('d h'),
YEAR_MONTH ('y-m'), HOUR_SECOND ('h:m:s'), DAY_MINUTE ('d h:m'),
DAY_SECOND ('d h:m:s').
curdate(), curtime(), now()
 current date, time, and date and time, respectively
date_format(date, format) 및
time_format(time, format)
 reformat dates & times to any format you like. (예)
date_format(workdate, '%W %D of %M, %Y'). ('Monday 16th of June,
2003').
dayname(date)
 name of the day in date (for example, 'Monday').
extract(type FROM date)
value of type in date. (예: YEAR  return the year from date.)
Type는 adddate(), subdate()에서와 동일
unix_timestamp([date])
current Unix timestamp. (= 1970/1/1이래로 몇 초)
If called with a date, this returns timestamp corresponding to that date.
89
– 예: start from the 1st of January 1999 and add 1 year and 6 months to it:
select adddate("1999-01-01", INTERVAL "1-6" YEAR_MONTH); 
+----------------------------------------------------------------------------+
| adddate("1999-01-01", INTERVAL "1-6" YEAR_MONTH)
|
+----------------------------------------------------------------------------+
| 2000-07-01
|
+----------------------------------------------------------------------------+
1 row in set (0.41 sec)
– Unix timestamps are not humanly readable, but they are very compatible
with the APIs that come with other programming languages. (예: date()
function built into PHP)
– unix_timestamp() function과 동일한 query
select unix_timestamp(adddate("1999-01-01", INTERVAL "1-6" YEAR_MONTH));
+------------------------------------------------------------------------------------------+
| unix_timestamp(adddate("1999-01-01", INTERVAL "1-6" YEAR_MONTH))
|
+------------------------------------------------------------------------------------------+
|
962373600
|
+------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
90
Cast Functions
• 오직 2개의 cast 함수: cast() 와 convert().
– = typecasting (=하나의 type (예: signed integer)에서 다른
type(예: char)으로 변환 (convert))
/* Syntax는 약간 다르지만 동일한 작업.*/
– Prototypes:
• cast(expression AS type)
• convert(expression, type)
/* ANSI compliant, */
/* ODBC compliant.*/
– Valid types: BINARY, CHAR, DATE, DATETIME, SIGNED
(INTEGER), UNSIGNED (INTEGER).
– MySQL에서는 대부분의 casting이 자동적으로 실행됨.
• 예; string 함수에 숫자를 입력하면  자동으로 string으로 cast 됨.
91
Other Functions
Table 8.6. Miscellaneous Functions
Function
Purpose
benchmark(count,
expression)
Evaluates expression count times. Always returns zero—the point of this function is to time
execution and look at the execution time at the bottom of the result set.
encrypt(s[,salt])
Encrypts s using a Unix crypt system call. The salt string is an optional two-character string.
If crypt is not available on your system (for example, Windows), this function will return NULL.
found_rows()
Returns the number of rows that would have been returned by the last query if no limit
clause was used. (SELECT문에서 SQL_CALC_FOUND_ROWS 가 명시되었을 때만 유효) as in Ch 7.
last_insert_id()
Returns the last automatically generated AUTO_INCREMENT value.
한 table에 row insert한 후 그 row의 id를 다른 table의 foreign key 로서 insert하려는 경우에 유용.
md5(s)
Returns the 128-bit MD5 hash of string s.
username 과 password를 저장하는 응용 프로그램 작성시 유용.
password(s)
Calculates a password string for the string s. (Chapter 11)
프로그램 내에서 password를 보관하는 경우에 이용.
92
Functions for Use with GROUP BY Clauses
– = grouping functions = aggregate functions.
– 또한 query의 결과 전체에 대해 수행할 수도 있다. (전체 rows를 single group취급) 예:
select job, count(job)
from employee
group by job;
select count(*)
from employee; /*  count the number of rows */
Table 8.7. Grouping Functions
Function
Purpose
avg(column)
Returns the average value in column.
count(column)
Returns the number of values in column.
min(column)
Returns the smallest value in column.
max(column)
Returns the largest value in column.
std(column)
Returns the standard deviation of the values in column.
sum(column)
Returns the sum of values in column.
93
9. MySQL Table Type이해
94
기초
• DB설계
– trade-offs ; (예: a transaction-safe  속도저하, 디스크, 메모리 부담)
– 최종 결정은 DB설계자
• (용어)
– table types = storage engines
– This reflects
• DB의 본질적 목적은 데이터 저장.
• Caching, indexing, locking, and disk access등을 처리하는 source code는
특정되어 있다.
– transaction or transaction safe
• 예: 은행 자금이체  at least two SQL queries—one to deduct, and one to
add to the other.  데이터베이스가 일관성 있는 상태 (a consistent state)
를 유지하기 위해서는 either both happen or both fail.
– Transaction-safe tables
•  a set of queries is one indivisible unit of work—a transaction. The
whole transaction should complete, or the database must roll back or
return to the same state it was in before the transaction started.
• MySQL에서 이용 가능한 table types는
– ISAM/ MyISAM/ InnoDB/ BerkeleyDB (BDB)/ MERGE/ HEAP
• 이 중에서:
– InnoDB and BerkeleyDB are transaction safe. (나머지는 X)
– 가장 많이 이용되는 것: MyISAM and InnoDB.
95
ISAM Tables
• MySQL에 내장 시킨 것은 단지 legacy support를 위한 것
– > MyISAM table이 완전 대체 -- 여기서 언급 생략.(ver. 5.0에서 완전히 없이질 것)
• ISAM
– ISAM table 생성 (just for reference) :
create table asset
(
assetID int not null,
description varchar(255)
) type=ISAM;
– ISAM table이 존재하지만 신규 개발에는 이용X 가급적 MyISAM으로 전환할 것
• MyISAM
– 장점:
• ISAM table은 빠르지만 transaction-safe storage를 제공하지 못함.
• Table portability. -- 디스크 상의 Table은 (어떤 플랫폼이든지) 다른 시스템으로 load될 수
있지만 ISAM table은 X
• Support for very large tables. – OS 한도 내에서 무제한 – 실제로는 대부분 file system이
최대 2GB size (MERGE tabe을 이용해서 이 한도를 회피.) (Cf. ISAM tables; max 4GB.)
• 디스크 공간을 보다 효율적으로 이용  space 및 fragmentation이 줄어 듬
• Less restricted keys. - MyISAM tables ; 64 keys/table, a default max key 길이=
1024 bytes. (cf. ISAM tables ; 16 keys/table, a default max key length=256 bytes.)
96
MyISAM Tables
• 빠르지만 transaction-safe storage는 아님.
– (MyISAM table을 생성하는 코드)
create table article (
articleID int not null auto_increment primary key,
title varchar(255),
body text
); /* 또는) type=MyISAM; */
• MyISAM table types:
•  Column지정에 따라 자동적으로 결정됨.
– The char and numeric types  고정된 크기.
– The size of varchar, text, blob columns  내용에 따라 크기가 달라짐.
– Dynamic
– Static
– compressed
• To repair or defragment a MyISAM table,
– myisamchk (command-line) 또는 REPAIR TABLE (MySQL 명령어). (Ch 13)
• To defragment but not repair,
– OPTIMIZE TABLE(MySQL command) 이용 (Ch 18)
97
– Dynamic
• tables with variable-length rows will be created as dynamic tables.
• (cache, find, 또는 record의 repair 등에 있어) 보다 복잡한 관리가 필요.
–  크기가 변화하기 때문이기도 하지만 + fragmented되기 때문
» Corruption을 고치기 어렵다.
» a segment of a file that has been cached by the operating system
cannot be guaranteed to contain all parts of a row.
– Static
• Tables with fixed-length rows will be created as static tables
• 장점.
– 다른 table형식보다 search가 빠르다.
»  Index를 통해 record를 retrieve할 때 그 위치가 파일 시작점으로부터 특정
offset으로 정해짐.
»  It is very easy to cache.
»  It is less likely to suffer serious corruption in the event of a crash—
the repair facility can usually recover all rows except the damaged one.
• Disadvantage
– 대부분 disk space의 낭비를 초래. (대상 column에 따라 그 정도가 다르다.)
– compressed
• 인위적으로 myisampack tool을 이용하여 생성.
98
• Compressing MyISAM Tables
– Table type은 자동으로 결정되지만 table compression은 자동이 아님.
 myisampack. (단, pure ISAM table의 경우 pack_isam 이용)
– Compression 은 좋은 것처럼 들릴 수 있지만 실제로는 일부에만 해당
•  compressed tables become read-only.
– Table을 alter, update, or insert data 할 경우 전체 table을 uncompress하고
변경한 후 다시 recompress the table.
– myisampack에 의한 compression은 다음의 혼합 작용:
• true compression (Huffman coding)
• + a set of optimizations aimed at shrinking columns, such as
– converting types to smaller types and
– converting columns to enums.
– record 별로 각각 compress되므로 실제로 overhead는 그리 크지 않다.
This may even be counterbalanced on slow devices by the
reduction in data that needs to be read from disk.
99
•
Full-Text Searching on MyISAM Tables
– 예: create a MyISAM table with a full-text index:
create table article (
articleID int not null auto_increment primary key,
title varchar(255),
body text,
fulltext (title,body));
– 예: retrieve any records containing the word 'merger':
select title from article
where match (title,body) against ('merger');
– 예: 다음 문자 포함하는 record ;'merge‘ or 'acquisition‘ or 'acquire‘ or 'takeover'.
select title from article
where match (title,body) against ('merge acquisition acquire takeover');
• /* 단, we are matching any record that contains at least one of the words. (string or record
containing every word listed 가 아님. 뒤에 나옴)
– 'acquire‘와 'acquisitions' 는 별개로 검색할 것. 왜냐하면 MySQL이 stemming을 지원x.
• Stemming; 여러 개의 단어에 대한 어근 (stem word)을 통한 검색. (예: 'acquire' is the stem of many
words such as 'acquires', 'acquired', and 'acquisition'.)
– match 되는 것으로 발견된 항목에는 relevance value가 매겨지고 그에 따라 자동 sorting된.
• You may want to see the relevance scores for records.
– 예: retrieve an unsorted list of scores for all records. Any records with a score of zero
have no similarity and will not be retrieved.
select title, match (title,body)
against ('merge acquisition acquire takeover')
as relevance
from article;
• /* 단, relevance 는 keyword가 아니라 just an alias for match(title,body) against ('merge acquisition
100
acquire takeover')*/
– 예: retrieve article titles and scores for matched documents.
select title, match (title,body)
against ('merge acquisition acquire takeover')
as relevance
from article
where match (title,body) against ('merge acquisition acquire takeover');
– 검색할 수 없는 단어: 성능문제 등
• Short words are not indexed.
– 4 characters (default) 미만은 무시. For some installations, most famously Slashdot.org,
this is a problem because three-letter acronyms are often an important part of the
content in technical material. You can change this limit by altering the variable
ft_min_word_len, but you will need to regenerate your indexes.
• Stop word = a word with no semantic value. 문장구성에는 중요하나 컨텐츠에서는
중요치 않은 것. (예: 'the', 'and', 'then', and 'soon' ) 표준의 stop word외에도 별도
지정 가능.
– 참고:full-text indexing은 복잡한 작업. 규모가 커지면 (예: > 1 M row) 성능저하.
– Words that are common in your data are not used when searching.
• 예: Table에 company newsletter articles for Acme PTY Ltd.이 포함되면 'Acme'라는
말이 반복되기 쉽다. If 50% or more of your records contain a word, that word is
taken to have no value when calculating relevance.
101
• Boolean Full-Text Search
– 예: match only records that contain the word 'linux' and the string "Open
Source", but not the word 'desktop'. 또한 'Java' 와 'Oracle‘은 optional. 그
리고 이때 record에 'Java‘가 포함되면 relevance가 올라가고'Oracle' 이 포함되
면 degrade the ranking. search string 에서의 단어 또는 record의 순서는 중요
하지 않음.
select title
from article
where match (title,body)
against ('+linux +"Open Source" -desktop Java ~Oracle' IN BOOLEAN MODE);
– Boolean mode검색에 반드시 full-text index가 필요치는 않지만 unindexed
table의 검색은 매우 늦어짐.
– 또한 Boolean mode로 검색 시에는 흔히 나타나는 단어도 무시하지 않고 검색.
즉, 50% rule does not apply.
• 신문의 article에서 Acme PTY Ltd를 검색하는 경우 다음 첫째 query문에서는 대부분의
row가 출력되지만 그 다음의 query문에서는 empty result set.
select title from article
where match (title,body) against ('Acme' IN BOOLEAN MODE);
select title from article
where match (title,body) against ('Acme');
102
Table 9.1. Boolean Mode Search Operators
Oper
ator
Meaning
+
This word is compulsory.
-
This word must not appear.
<
This word is less important.
>
This word is more important.
()
Group words together as a subexpression.
~
This word may appear, but it has a negative effect on ranking.
*
Wildcard suffix. 예: merge will not match merger, but merge* will match both
merge and merger.
May be used only at the end of a word.
""
This is a phrase. Matches only exactly the same content in the same order.
103
InnoDB Tables
• 일반론
– InnoDB is a fast, transaction-safe storage engine (Transactions–Ch 10)
– InnoBase Oy 역시 dual-licensing. (www.innodb.com)
– 특히 대량 데이터를 고속으로 transaction-safe 환경에서 처리하는 경우 유용.
• 사례: Slashdot (www.slashdot.org), Google (www.google.com), and Yahoo!
Finance (http://finance.yahoo.com)
– 대부분의 경우 MyISAM이 다소 빠르다.
– Row-level locking.
•  only the row we are using in a particular query is unavailable to other users.
(BDB제외한) 대부분 다른 storage엔진은 table-level locking, 즉, while one process
is updating a table, it is not available to other processes.
– Support for foreign keys.
– Consistent nonlocking reads in SELECTs.
• (The idea for this is borrowed from Oracle.)
• InnoDB has its own configuration options, its own directory, and its
own way of storing data.
– Whereas MyISAM stores one table per file, InnoDB stores all its tables
and indexes in a tablespace, which means that they may be stored over
multiple files. This allows InnoDB to have very large tables that will be
unaffected by any operating-system file size restrictions.
– 단, MyISAM와 비교하여 동일 record저장에 훨씬 많은 disk 공간 사용.
104
BerkeleyDB (BDB) Tables
• BDB is provided by Sleepycat software (www.sleepycat.com).
• All BDB tables must have a primary key (not that this is much
of a problem because we would recommend this anyway). In
fact, one will be created silently for you if you do not create
one for yourself.
• 그러나 그리 많이 쓰이지 않음.
–
–
–
–
BDB engine은 오래 전에 나왔지만 MySQL integration은 최근.
page-level locking이므로 InnoDB보다 성능이 다소 떨어짐
또한 많은 table을 동시에 열어놓으면 성능저하.
BDB tables are stored as b-trees.
• 비교: 대부분의 다른 table types store their indexes as b-trees.  table
scanning이 늦어짐 (such as when you are retrieving all rows from the
table or performing unindexed queries).
– BDB tables also take up more space on disk.
– you cannot move the stored data around on disk. 왜냐하면 the
data stored for a table includes the path to the data.
– 또한 be careful when backing up BDB tables to remember to back
up the log files because you will be unable to restart without them.
105
MERGE Tables
• maximum file size에 대한 운영체제의 한계를 극복하는 방법으로 유용.
– Because each MyISAM table is stored in a single file, tables are limited in
size by the maximum file size of the operating system.
– MERGE table = a construct that allows you to treat multiple MyISAM
tables as a single table for the purpose of queries.
• 예: create a MERGE table consisting of three log tables (Listing 9.1)
• MERGE table에서도 각각의 table에 대해 query 가능.
• 단, We cannot DROP, ALTER, DELETE FROM TABLE, REPAIR,
TRUNCATE, OPTIMIZE, or ANALYZE any of the component tables.
You will be able to do some of these things (DELETE FROM TABLE)
if the MERGE table is not currently open. You can close it with
FLUSH TABLES.
– manual 에서는 FLUSH 후에 위의 작업을 할 수 있다고 하지만 실제로는 잘 안됨
(*) 예; dropping one of the component tables leads to the MERGE table
also being silently dropped. If you need to make these sorts of changes,
you may be better off dropping the MERGE table and re-creating it.
Dropping the MERGE table does not affect the component tables or their
data.
• You can compress individual tables in the MERGE with myisampack.
여기서의 logfile 예에서 유용—compress the earlier months' log files
because we are writing to only the most recent log.
106
Listing 9.1 A MERGE Table Example
create database logs;
use logs;
create table log2003Jan
(logid int auto_increment primary key,
logts datetime,
entry char(255));
insert into log2003Jan values
(NULL, '2003-01-01', 'first jan entry');
create table log2003Feb
(logid int auto_increment primary key,
logts datetime,
entry char(255));
insert into log2003Feb values
(NULL, '2003-02-01', 'first feb entry');
create table log2003Mar
(logid int auto_increment primary key,
logts datetime,
entry char(255));
insert into log2003Mar values
(NULL, '2003-03-01', 'first mar entry');
create table logs
(logid int auto_increment primary key,
logts datetime,
entry char(255))
type = merge
union = (log2003Jan, log2003Feb, log2003Mar)
insert_method = last;
 동일한 structure의 table 3개를 생성 (log2003Jan, …)
 Logging is a common application of MERGE tables.
 데이터 입력 후 MERGE table 생성, by creating logs table.
(specifying that it is of type MERGE and that it is the
UNION of the three tables.)
맨 끝의 INSERT_METHOD ; if we insert data into the
MERGE table, it will be added to the last table in the merge,
in this case log2003Mar. The other options are FIRST
(insert into the first table in the list) or NO (don't allow
inserts into the MERGE table).
  a table that contains all the data in the merged tables.
 (중요) MERGE table에서의 primary key인 logid 는 일반적인
primary key의 운용방식과는 다르다. Usually, they must be
unique, but because the MERGE table manages three sets
of primary keys, there may well be more than one row with
the same primary key, as in the preceding output.
(run the query)
select * from logs; 
+-------+--------------------------+-------------------+
| logid | logts
| entry
|
+-------+--------------------------+-------------------+
| 1 | 2003-01-01 00:00:00 | first jan entry |
| 1 | 2003-02-01 00:00:00 | first feb entry |
| 1 | 2003-03-01 00:00:00 | first mar entry |
+-------+---------------------------+-------------------+
3 rows in set (0.01 sec)
107
HEAP Tables
• = extremely fast tables that are stored wholly in memory.
 hashed indexing scheme 이용
• 장단점
– 단점; 전원문제 발생 시 your HEAP data is gone forever.
– 장점: temporary table 저장에 적합.
• You can create a HEAP table like this:
•
•
•
•
create table testHeap
(id int not null primary key, data char(100))
type=heap
max_rows = 100;
– 이 예에서처럼 max number of rows를 limit하는 것이 좋다.  HEAP
table이 커지면 곧바로 메모리 부족문제. The number of rows can also
be limited by the configuration directive max_heap_table_size.
• HEAP tables have a few limitations:
–
–
–
–
AUTO_INCREMENT 지원x
BLOB or TEXT type 지원 x
HEAP tables cannot use the leftmost prefix of an index to find rows.
Indexes will be used only to find rows with queries that use the =
or <=> operators in the search clause.
108
10. InnoDB Table을 통한
Transaction 이용
109
Transaction이란?
• A transaction = a sequence of related instructions that must
be treated as one indivisible unit.
– (atomicity) A transaction cannot be broken down into parts—It all
gets processed (done) or it all gets ignored (undone).
– 예: 은행 계좌관리 (bank account)
create table account
(
number int not null auto_increment primary key,
balance float
) type = InnoDB;
• (그런 후)
insert into account (balance) values (0.0);
insert into account (balance) values (1000.0);
insert into account (balance) values (2000.0);
• (결과)
+------------+-----------+
| number
| balance |
+------------+-----------+
|
1
|
0 |
|
2
|
1000 |
|
3
|
2000 |
110
– (단순한 사례: deposit $500 into account 2)
# first check balance
select balance from account where number = 2;
# query gives us a result of $1000
# now store updated balance
update account set balance = 1500 where number = 2;
– These queries are related. They need to be run together.
• 만약 잔액조회와 update 사이에 balance를 다른 사람이 update한다면 문제!
• IF
query를 통해 $500을 예금하고 다른 고객이 $100을 예금하려 한다면
THEN 잔액 결과치는 $1100 도 될 수 있고 $1500도 될 수 있다.
# first check balance
select balance from account where number = 2;
# query gives us a result of $1000
# now store updated balance
update account set balance = 1100 where number = 2;
– 단, 이 경우에는 문제가 쉽게 해결될 수 있다. Make our updates
relative rather than absolute will make them into single, indivisible
units and will solve the problem.
– 다음 query는 (다른 query가 동시 실행되던지 관계없이) 이용할 수 있다.
update account set balance = balance + 500 where number = 2;
– A single update statement in MySQL is always atomic. It cannot
be interrupted by another query or half succeed. It will complete
or will completely fail on an error.
111
– (복잡한 scenario) transfer $1000 from account 2 to account 1:
update account set balance = balance - 1000 where number = 2;
update account set balance = balance + 1000 where number = 1;
– 2개의 query가 함께 수행되어야 한다.  거래 전후의 총 금액(total amount)는 동일. 도중에
power failure가 발생 시 data가 inconsistent!  collapse queries into one SQL statement!
update account as source, account as dest
set source.balance = source.balance ?1000,
dest.balance = dest.balance + 1000
where source.number = 2 and dest.number = 1;
– account table에 대한 2개의 aliases (source 및 dest)를 통해 하나의 atomic update가 either
succeed or fail하도록 하였다. Readability가 문제일 뿐. 그러나 많은 경우 모든 관련 query들을
이처럼 collapse시키는 것이 불가능하다.
– (solution) use MySQL's transaction syntax를 통해 여러 문장을 하나의 transaction (=a
related, indivisible set). 으로 표시
start transaction;
update account set balance = balance - 1000 where number = 2;
update account set balance = balance + 1000 where number = 1;
commit;
– (transaction의 특징)
• they are not visible to other sessions until they are complete and committed. No other thread
can read inconsistent data from the table(s) while you are in the process of updating it.
• partially performed transactions can be undone.  roll back the transaction before we have
committed it  changes made by queries that are part of the transaction will be undone.
• (위의 예에서 SELECT 문을 통해 잔고이상의 인출을 못하게 하면  ROLLBACK를 통해 cancel 가능)
start transaction;
update account set balance = balance - 1000 where number = 2;
update account set balance = balance + 1000 where number = 1;
select balance from account where number = 2;
# select tells us that account #2 has a negative balance!
# we'd better abort rollback;
– Calling ROLLBACK aborts the transaction and undoes any changes made. A transaction
that was rolled back instead of committed leaves no trace in the data. Because partial
results were never visible to other sessions, it is exactly as though it never happened.
112
Using Transactions in MySQL
– START TRANSACTION is synonymous with BEGIN or BEGIN WORK.
• 다른 DB 사용 등에 따라 달라지지만 START TRANSACTION 가 SQL-99 syntax이므로 권장.
• Setting the Autocommit Mode
– 보통 때 이미 autocommit mode로 수행–각 query는 실제 하나의 isolated transaction.
(모든 query에 대해 자동으로 START TRANSACTION 및 COMMIT가 적용된 효과)
update account set balance = balance - 1000 where number = 2;
update account set balance = balance + 1000 where number = 1;
– ==
start transaction;
update account set balance = balance - 1000 where number = 2;
commit;
start transaction;
update account set balance = balance + 1000 where number = 1;commit;
– Note that if you manually type
start transaction; /*  nothing will be committed until */
commit;
– To disable the autocommit behavior using the SET command:
set autocommit=0;
•  이 경우에는 START TRANSACTION 을 이용할 필요가 없다.
• (중요) 단, 주기적으로 COMMIT 를 수행하여 데이터베이스 변경사항을 적용해 줄 것.
– To put MySQL back into autocommit mode:
set autocommit=1;
– The autocommit variable is local to a single session
• changing the mode will affect only queries run from your session and only for
as long
113
as your session is connected.
• autocommit 가 on이든 off이든 상관없이 간혹 변경사항이 자동으로
적용되는 경우가 있다. 즉, MyISAM과 같은 non-transaction-safe
table type을 이용하는 경우 모든 변경사항은 (autocommit setting
에 상관없이) 즉각 (immediately) commit된다.
– You can happily group your statements with START TRANSACTION
and COMMIT; it is just that this will have no effect on the nontransaction-safe tables.
– 심지어 ROLLBACK을 호출(call)할 수도 있다.  실행오류가 되지는 않고
단지 아무런 효과가 없을 뿐.
– 각각 다른 table type을 가진 서버에 dump file을 reload하거나 code
test 하는 경우 유용.
• For transaction-safe tables, there are actions (other than
typing COMMIT) that will automatically trigger a COMMIT.
Requesting a lock will implicitly commit any outstanding
queries.
114
Locks의 이용
• Table을 manually lock and unlock해도 transaction의 효과를
거둘 수 있다.
lock tables account write;
select balance from account where number = 2;
update account set balance = 1500 where number = 2;
unlock tables;
– 단, 일단 table을 lock하면 가능한 빨리 unlock해야 다른 thread에
대한 impact를 줄일 수 있다. 따라서 Request all the locks you
need at once!
• 앞서와 달리 여러 테이블을 이용하면서 lock이 필요하면 함께 호출:
lock tables account write, account as a read, othertable
low_priority write;
– LOCK TABLES를 호출하면 기존의 모든 lock을 해제 (release).
•  If you attempt to collect the locks you need over multiple
statements, you will release all the early ones and will only
actually hold the locks requested in the final statement.
115
• (Lock의 주된 유형:= read and write.)
– 기록을 위해 table access할 때 write lock하면 다른 어떤 thread도 read/write가
불가능 (until you release it).
– A read lock is less extreme. 단지 table read만 하려는 경우 다른 thread가 동시
에 read하는 것은 상관 없다. (단지 lock을 건 동안 다른 thread의 write만 금지)
• write lock에서 low_priority로 표시; 누구에게 lock의 우선순위를 줄 것인가에 대한 policy
가 필요.
• MySQL은 보통 write lock request에 대해 read lock보다 우선권 (priority)을 주어서 기존
저장된 데이터의 update가 가급적 빨리 이루어지도록 함. 만약 다른 방식을 원한다면 you
can request a low-priority write lock (앞의 예에서의 other table).
단, 주의: Lock을 request할 때마다 잠시 wait해야 한다. 또한 low-priority lock will be
granted only if there are no other threads requesting read or write locks on that
table. It is possible on a busy server that this might never happen.
– 간혹 lock을 manually control해야 하는 경우
• If you have an application that requires very high performance but needs
transaction-like behavior only occasionally, it might be worth using a fast nontransaction-safe table type and using locks to solve your transaction issue.
– 또한 LOCK TABLES을 call하는 경우
• MySQL's data file에 직접 작업하는 경우. 예: Backup 시 확인!: “disk files stayed
consistent and unmodified while backing up.” 즉, you would need to lock them.
– (중요) 다른 사용자를 위해 lock을 사용 후에는 가능한 빨리 release lock. Some
tasks you may lock while performing, such as reindexing or backing up
large files, can take significant amounts of time.
116
InnoDB의 Transaction Model
• ACID Compliance
– Atomicity; transactions are atomic and indivisible.
– Consistency; operations transform the database from one valid state
to another. 즉, no intermediate stages where the data is inconsistent.
– Isolation; transactions do not affect each other while they are running.
Each transaction should be able to view the world as though it is the
only one reading and altering things.
• 실제에서는 어려운 상황이므로 lock을 통해 이러한 illusion을 만들어 냄.
– Database와 option에 따라 different levels of isolation.
– Durability; 일단 transaction이 commit되면 그 효과(effect)는 permanent
하다.
• 대개 단순하지만 복잡한 DBMS (locking 및 multiversioning을 통해 concurrent
multiuser access를 보장하고 caching을 통해 성능향상을 추구)경우 잠재적 폭탄!
• 또한 durability implies recoverability in the event of a failure  we should
be able to combine a backup and a log to bring the database back to its
precrash state and perhaps process transactions that had been logged but
not yet executed or committed.
– InnoDB tables (or BerkeleyDB tables)을 이용하면
• MySQL is ACID compliant. (cf. MyISAM table이용하는 경우 x)
• You can choose the level of isolation that transactions have from one
117
another. The binary log and repair tools provide durability.
• Transaction Isolation
– InnoDB table은 4가지 transaction isolation level을 가짐.
(‘strongweak’순서)
•
•
•
•
•
Serializable
Repeatable read
Read committed
Read uncommitted
/* trade-off between robustness and performance. */
– Serializable isolation
• is the ideal from a purity and robustness angle.
•  Reads and writes on the database appear to be happening in a
sequence, with changes from a write being completely recorded before
the next read starts.
• Transaction이 항상 noninterleaved sequence로 수행될 필요는 없다. (많은 경
우 서로 interfere하지 않으므로)
• 그러나 clash가 발생하면 noninterleaved sequence로 수행해야 한다. 이 경우
locking and waiting + (어떤 transaction들이 interfere하는지의 조합에 대한)
overhead of predicting  serializable isolation 은 가장 성능이 느려짐. 이러
한 mode 필요 시 다음 명령어 수행:
• /*set transaction isolation level serializable; */
– Repeatable read (이하 다음 페이지)
– Read committed
– Read uncommitted
118
– repeatable read.
• InnoDB에서의 default level. 여기서는 each transaction gets to work in an isolated
version of the table where each row remains as it was when the transaction
started. Reading a row is guaranteed to be repeatable.
• If you call select * from account where number=1; at the start of the
transaction and perform the same query later in the transaction, you will get the
same results both times. You can, however, get what are called phantom reads.
It is possible that another transaction which commits before yours is adding new
rows to the table. If you perform the same query with a condition twice, such as
select * from account where balance>1000;
• it is possible that you will get new rows—phantom rows—the second time. 실제로
는 phantom read는 매우 드물다. InnoDB에서는 이러한 문제 해결에 (이러한 조건을 적용
하는 column이 index되었다면) next key locking 라는 algorithm 사용.
• 또한 InnoDB의 row-level locking.  … As well as locking the rows used, next key
locking also locks the gaps between rows found in the index. Because phantom
reads are addressed in this way, few systems really need to be put in serialized
isolation mode.
– read committed
• your transactions are no longer very isolated. If you perform a query and repeat
it later in the same transaction, you will get different results the second time if
another transaction has modified the data in the meantime and committed.
• 이를 위해서는:
set transaction isolation level read committed;
– read uncommitted,
• 여기서는 it is distinctly arguable not only that your transactions are no longer
isolated, consistent, and therefore ACID compliant, but that you no longer really
have transactions. In this mode, it is possible for transactions to read changes
that other transactions have made before the changes have been committed.
(=a dirty read). You would tolerate this only in fairly unusual circumstances, such
as at a time when you know all active threads will be reading or writing, but not
119
both. read uncommitted mode를 위해서는:
set transaction isolation level read uncommitted;
Table 10.1. Transaction Isolation Level Characteristics
Dirty Read
Nonrepeatable Read
Phantom Read
Read Uncommitted
Possible
Possible
Possible
Read Committed
Not possible
Possible
Possible
Repeatable Read
Not possible
Not possible
Possible (but unlikely)
Serializable
Not possible
Not possible
Not possible
120
Part V: MySQL 관리
11
12
13
14
15
16
Managing User Privileges
Configuring MySQL
Administering Your Database
Backup and Disaster Recovery
Securing Your MySQL Installation
Replicating Your Database
121
11. User Privileges의 관리
• Creating user accounts with GRANT and REVOKE
• Privilege levels
• Understanding the privilege tables
122
Creating User Accounts with GRANT and REVOKE
•
Granting Privileges
– to create user accounts and give users access to databases, tables, and functions.
grant usage
/* usage= log in만 가능 */
on *
to luke@localhost identified by 'password';
– 일반형:
GRANT priv_type [(column_list)] [, priv_type [(column_list)] ...]
ON {tbl_name | * | *.* | db_name.*}
TO user_name [IDENTIFIED BY [PASSWORD] 'password']
[, user_name [IDENTIFIED BY 'password'] ...]
[REQUIRE
NONE |
[{SSL| X509}]
[CIPHER cipher [AND]]
[ISSUER issuer [AND]]
[SUBJECT subject]]
[WITH [GRANT OPTION | MAX_QUERIES_PER_HOUR # |
MAX_UPDATES_PER_HOUR # |
MAX_CONNECTIONS_PER_HOUR #]]
• * ; 현재 선택된 database에 적용, 선택된 database가 없을 때에는 =*.*
• TO ; 대상자 지정. Log in 된 host도 가능 ; 예: fred@localhost. … (Username; 최대 16 문자)
• IDENTIFIED BY; password 설정 (신규 사용자 또는 기존 user).
• 사용자가 Password 변경 시에 ; set password = password('newpassword');
• 관리자가 사용자 password변경; set password for fred@localhost = password(‘newpassword’)
/* mysql이라는 데이터베이스에 access권한을 가져야 함 */
• WITH GRANT OPTION ; a special privilege that allows the user to grant privileges.
• WITH ; 1시간 내에 시도할 수 있는 query, update, connection 회수 (default= 0 =no limitation.)
• REQUIRE; secure connection이 있어야 사용자가 연결할 수 있도록 함. (해당 configuration123
필요)
Privilege Levels
Table 11.1 User-Level Privileges
Privilege
Meaning (사용자가 …을 할 수 있음)
CREATE
Create tables
CREATE TEMPORARY TABLES
Create temporary tables
DELETE
Delete rows
EXECUTE
Execute procedures
INDEX
Create indexes
INSERT
Insert rows
LOCK TABLES
Lock tables
SELECT
Select rows
SHOW DATABASES
‘SHOW DATABASES’ 통해 available DB list 출력
UPDATE
Update rows
USAGE
Users can log in, but cannot do anything else
124
Table 11.2 Administrator-Level Privileges
Privilege
Meaning
ALL
User has all the privileges except WITH GRANT OPTION
ALTER
User can alter tables. 일부 power user에게 부여할 때도 주의할 것
privilege table을 변경하는데 사용될 수도 있음
DROP
User can drop tables. Only to the trusted users!
FILE
User can load data from a file. Only to the trusted users!
(주의) 사용자가 arbitrary file (예:/etc/passwd)을 load하지 않도록 유의
PROCESS
MySQL 에서 수행되는 모든 process의 list를 출력
RELOAD
User can use the FLUSH statement.
REPLICATION CLIENT
User can check where the masters and slaves are.
REPLICATION SLAVE
slave에서의 special replication user 를 위한 특수한 privilege.
SHUTDOWN
User can run mysqladmin shutdown.
SUPER
User can connect even if MySQL has its maximum number of
connections and can execute the commands CHANGE MASTER, KILL
(thread), mysqladmin debug, PURGE MASTER LOGS, and SET
GLOBAL.
WITH GRANT OPTION
User can pass on any privileges he has.
REFERENCES
Future use를 위해 예약됨
125

Evaluating Privileges
• 4 sets of privileges are granted with GRANT
statement:
– Global privileges ; 모든 database에 적용.
grant all on *.* to fred;
– Database privileges ; 특정 database에 적용.
grant all on employee.* to fred;
– Table privileges ; 특정 table에 적용.
grant select on department to fred;
– Column privileges apply to a single column.
grant select (employeeID) on employee to fred;
• 특정 사용자의 privilege 결정에는
– 그 사용자의 <<global privileges + database privileges +
table privileges + column privileges>> 를 ORed together.
126
Using the REVOKE Statement
• = GRANT의 반대. (= privilege를 빼앗음)
– 예: revoke all on employee.* from fred;
• 일반형:
REVOKE
priv_type
[(column_list)]
[(column_list)] ...]
ON {tbl_name | * | *.* | db_name.*}
FROM user_name [, user_name ...]
[,
priv_type
127
Privilege Tables
• GRANT 및 REVOKE의 결과는 mysql이라는 database
에 저장. 이들 명령어를 사용 대신 직접 이 database를
수정도 가능. 단, 다음 문장을 수행시킬 것.
flush privileges;
• six tables in the mysl database:
•
•
•
•
•
•
user
db
host
tables_priv
columns_priv
func
– 이 중 앞의 5개가 user privilege와 관계. (func 테이블은 userdefined function information을 저장)
• 앞의 3개 table– user, db, and host— 은 database connection
을 허용할지를 결정하는데 이용.
128
• Understanding the user Table
– 사용자의 global privilege set에 대한 정보로서 user table의 column은:
• Scope columns ; determine when a row is relevant.
– Host: Where the user is connecting from
– User: The username
– Password: The user's password, as encoded by the PASSWORD() function
• Privilege columns ; 각각 다음의 global privileges에 해당. Y (user has the
global privilege) or N ( the user does not have global privilege)의 값을 가짐.
–
–
–
–
–
–
–
–
–
–
–
Select_priv
Update_priv
Index_priv
Create_priv
Grant_priv
Reload_priv
Process_priv
Show_db_priv
Create_tmp_table_priv
Execute_priv
Repl_client_priv
Insert_priv
Delete_priv
Alter_priv
Drop_priv
References_priv
Shutdown_priv
File_priv
Super_priv
Lock_tables_priv
Repl_slave_priv
• Secure connection columns ; grant문에서의 REQUIRE 에 표시.
– ssl_type
– x509_issuer
ssl_cypher
x509_subject
• Resource limitation columns ; 사용자 resource limitation.
– max_questions
– max_connections
max_updates
129
• Understanding the db Table
– stores a user's privileges for particular databases.
• Scope columns ; determine when a row of privileges is relevant. If you have
different rules for different hosts, leave the host field blank and then create
a corresponding set of rows in the host table to give more information.
– Host
– User
Db
• Privilege columns ; specify whether the combination of Host, Db, and User
have each of the listed privileges. 역시 Y or N 값을 가짐.
–
–
–
–
–
–
Select_priv
Insert_priv
Update_priv
Delete_priv
Index_priv
Alter_priv
Create_priv
Drop_priv
Grant_priv
Create_tmp_table_priv
Lock_tables_priv
• Understanding the host Table
– MySQL consults the host table when it finds a blank host entry in the
db table. (단, GRANT 문으로는 효과가 없고 대신 manually set up할 것)
•
Scope columns ; determine when a row of privileges is relevant. Each row
here gives information for a single database accessed from a single host.
– Host
Db
• Privilege columns ; specify whether the combination of Host and Db have
each of the listed privileges. ; contain values Y or N. 단, 해당 columns:
–
–
–
–
–
–
Select_priv
Update_priv
Index_priv
Create_priv
Grant_priv
Lock_tables_priv
Insert_priv
Delete_priv
Alter_priv
Drop_priv
Create_tmp_table_priv
130
• Understanding the tables_priv Table
– 개별 table에 대한 user privilege를 표현.
• Scope columns ; (앞의 설명 참조) + Table_name privilege (; lists the
specific table that a grant applies to)
– Host
– User
Db
Table_name
• Grant columns ; 이 privilege 를 누가, 언제 grant했는지에 대한 정보.
– Grantor
Timestamp
• Table_priv column ; determines what privileges the Host/Db/User has
on the table listed in Table_name.
– 값: Select, Insert, Update, Delete, Create, Drop, Grant, References, Index,
Alter.
• Column_priv column ; table 내 모든 column에 대한 사용자의 privilege를
알려 줌.
– 수록 값: Select, Insert, Update, and References.
131
• Understanding the columns_priv Table
– 개별 column에 대한 user privilege를 표현. 다음 column 수록:
• Scope columns ; determine when a row in this table is relevant.
–
–
–
–
–
Host
Db
User
Table_name
Column_name
–
–
–
–
Select
Insert
Update
References.
• Column_priv column ; determines which privileges have been
granted to the combination outlined by the scope column.
• Timestamp column ; tells when this privilege was granted.
132
12. Configuring MySQL
• Setting MySQL configuration options
• Multi-install configuration options
• Configuring for internationalization
133
Setting MySQL Configuration Options
• MySQL programs have configuration options.
– ( command line or) options file (하나의 file을 통해 여러 MySQL 프로
그램에 default option 지정) Specifically;
• mysql, mysqladmin, mysqld, mysqld_safe, mysql.server, mysqldump,
mysqlimport, mysqlshow, mysqlcheck, myisamchk, and myisampack.
– 장점:
• 표준 options를 운영 여러 서버 운영 시에 편리. (특히 replication 이용 시)
• Under Unix, MySQL also supports the use of:
– Per-server
» = An options files for the each whole server (단, 하나의 하드웨어에서 여러
개의 MySQL server 운영가능)
» a global options file (=하나의 기계 상의 각각의 MySQL server에 대한 options
file) ; /etc/my.cnf에 위치
» Per-server files are in in the data directory for each server.
– Per each individual users.
» 위치: 각 사용자의 home directory.
» per-user file은 prefixed with a dot (예: .my.cnf instead of my.cnf)
• Windows에서는 you have a choice of:
– Windows 디렉토리에 my.ini or
– 서버가 설치된 드라이브의 root 디렉토리에 두고 (예: C:\) my.cnf
– = a global options file 즉 서버상의 모든 사용자에 적용.
134
Listing 12.1 Sample my.cnf File
[mysqld]
# turn on binary logging and slow query logging
log-bin
log-slow-queries
# InnoDB config
# This is the basic config as suggested in the manual
# Datafile(s) must be able to hold your data and indexes.
# Make sure you have enough free disk space.
innodb_data_file_path = ibdata1:10M:autoextend
#
# Set buffer pool size to 50 - 80 % of your computer's
memory
set-variable = innodb_buffer_pool_size=70M
set-variable = innodb_additional_mem_pool_size=10M
#
# Set the log file size to about 25 % of the buffer pool
size
set-variable = innodb_log_file_size=20M
set-variable = innodb_log_buffer_size=8M
#
# Set ..flush_log_at_trx_commit to 0 if you can afford
losing
# some last transactions
innodb_flush_log_at_trx_commit=1
윤형기:
[mysqld]
적용시키고자 하는 프로그램 지정. 또한 [client]를 통해 모든
client program에 적용시키도록 할 수도 있다.
<3 forms of syntax for setting individual options: >
(I) Specifying the option you want switched on; 예:
log-bin == mysqld --log-bin.
(ii) Specifying the option you want with a value; 예:
innodb_flush_log_at_trx_commit=1
(iii) Specifying the option you want with a value
using the set-variable syntax; 예:
set-variable = innodb_log_buffer_size=8M
/* (iii)syntax는 deprecated, but 알아둘 것. */예제파일
(my.cnf)은 매뉴얼상의 InnoDB configuration예제 */
Certain options work for all these programs:
·
--no-defaults ; no options files are to be read.
·
--print-defaults ; tell you what the values of all
the options are being set to for this program.
·
--defaults-file=/path/to/file ; tell the program
to use the specified file instead of any other options
files it has. configuration 변경을 test할 때 편리.
·
--defaults-extra-file=/path/to/file will read
the specified file after reading the global options file
but before reading any individual user options files.
다른 곳에서도 대부분 언급됨. 단, mysqld에 대해서는 이하 설명.
135
• Setting Options for mysqld
• mysqld --help
– 주요 options:
• ansi:
– ANSI 호환 mode로 수행.  MySQL에서 ANSI-99 SQL 사용 가능.
• basedir:
– 나름대로의 설치 base directory 설정.
• datadir:
– basedir,와 같은 효과를 데이터 디렉토리에 대해 적용
• log-bin:
– Turn on binary logging. (log의 위치/file명 지정 가능)
• log-error:
– Turn on error logging. Again, you can specify the location of the log.
• log-slow-queries:
– Turn on slow query logging.
• port:
– Specify the port that the server should listen on. Default는 3306.
• user:
– Specify the user that the MySQL server should run as.
136
Setting InnoDB Configuration Options
•
Option지정 없이 InnoDB를 사용할 수 있지만 option지정 시 성능개선
– innodb_data_file_path = ibdata1:10M:autoextend
• InnoDB data를 보관할 장소 지정. MyISAM table (; table별로 별도 file)과 달리, InnoDB tables
are stored in a shared tablespace, which may consist of one or more files.
• (이 예에서는) 모든 InnoDB data를 ibdata1라는 하나의 파일에 보관, initial file size=10MB, and
to automatically make it bigger (8MB at a time) if the tablespace becomes full.
• (일반형) filename:filesize[;filename:filesize;...][:autoextend[:max:size]]
• The autoextend option allows the tablespace to grow. max option ; max size 지정.
– innodb_buffer_pool_size=70M
• buffer size지정 (table data와 index를 cache). Cache와 마찬가지로 클 수록 disk I/O는 줄어든다.
• 지정되는 buffer pool의 크기는 서버 상의 다른 application 유무, 메모리 크기 등에 따라 달라짐.
– innodb_additional_mem_pool_size=10M
• sets aside memory to store internal MySQL data structures. If MySQL is running out of
room here, it will begin writing warnings to the error log.
– innodb_log_file_size=20M
• sets the size of each log file. InnoDB rotates between n log files—where n is the value set
in the innodb_log_files_in_group option, which defaults to 2, the recommended value.
– innodb_log_buffer_size=8M
• ; sets the size of the buffer in which logs are stored before they are written to disk.
– innodb_flush_log_at_trx_commit=1
•
•
•
•
1 로 지정 = transaction이 commit될 때마다 log will be flushed to disk. (가장 일반적 형태)
0으로 지정 시; the log will be written to and flushed to disk only roughly once per second.
2로 지정 시; the log will be written to with each commit, but flushed only once per second.
Values of 0 or 2 will improve performance, but are obviously a fairly risky proposition.
137
Multi-Install Configuration Options
•
•
•
•
하나의 하드웨어에서 여러 개의 MySQL 서버 수행 (예: ISP users, 교육)
– port: Each server must listen on a different port.
– socket: Under Unix, each server must use a different socket file. Under Windows, the
socket option sets the name of the named pipe used by the server. In both cases,
this value for socket must be different for each server.
– shared-memory-base-name (Windows only): 각 서버는 서로 다른 shared memory 사용.
– pid-file (Unix only): 각 서버는 process id (pid)를 기록할 서로 다른 파일을 필요로 함.
– Logging options: If you set any of the log file options, such as log-bin, you will need
to set up different log file locations for each server.
(간편법) 서버마다 basedir option을 다르게 설정  force the data directories and
log files to be different. (권고) 서버마다 데이터 디렉토리를 별도로 설정.
--defaults-file option 의 유용성 = starting each server instance with a different
set of defaults (or for installing each server as a Windows service with a
separate set of defaults).
(참조) 여러 개의 (multiple) 서버를 수행하면 클라이언트 프로그램, 기타 (예:
mysqladmin 등)에게 어느 서버에 연결할지를 지정해야 한다.
– --port command switch
– 이 경우, 사용자 별 my.conf 를 통해 지정하면 편리 ( 사용자별로 자신의 서버에 자동 연결)
138
Configuring for Internationalization
• 다음 2개의 options to mysqld 을 통해 default character set과
collation 설정.
– character set = the set of symbols used by default on the server.
– collation = the set of rules for performing comparisons, (sort
order—in the character set)
• --default-character-set option. Each character set has an
associated default collation,
• --default-collation option. If the combination of default
character set and default collation is not valid, mysqld will
give you an error message.
• Default:
– default character set = latin1,
– default collation = latin1_swedish_ci.
• This character set can also be described as ISO-8859-1 West European,
which is the one used in this book. The collation represents the sort
order for latin1 used by the Swedes and Finns.
• There are also collations for latin1 that represent the ways that
Germans, Danes, and Norwegians sort strings.
139
13. Administering Your Database
• Mysqladmin script 를 중심으로 살펴봄.
또한
• mysqlshow scripts
• mysqlcheck scripts
• 명령어:
– KILL, RESET, CHECK, REPAIR, and
– ANALYZE TABLE
140
Starting Up and Shutting Down
the MySQL Server
• Under Linux
– start the server with
• /etc/init.d/mysqld start
• 단, path지정 유의.
– You can also start the server by running
• safe_mysqld
– To shut down your MySQL server
• /etc/init.d/mysqld stop or
• mysqladmin -u root -p shutdown
• root 및 privilege부여시 조심.
– Figure 13.1. Services window in Windows Admin Tools (생략)
– mysqladmin script for Linux를 이용해서 shutdown도 가능
141
Server 및 Database에 대한 정보의 획득
• Retrieving Database Information
•
•
•
•
•
•
mysqlshow /* DB 정보제공. 단, Parameter가 없으면 access가능한 모든 DB*/
= show databases; /*mysql monitor 또는 다른 UI에서 수행*/
mysqlshow -u username -p password 도 가능
mysqlshow ?help /*  a full list of these options.*/
mysqlshow -u username -p database /*특정 DB에 대한 정보 (list of table)*/
mysqlshow -u username --status employee
– /*  information about the storage engine used in each table, how much data is
in each table, the current value of any auto-increment column in a table, and
the character set used in each table. */
– MySQL client내에서 SHOW 명령어  database 및 서버 상태 정보.
•
•
•
•
show
show
show
show
databases; and
tables; /*  databases and table에 대한 정보. 단, options 이 많음 */
columns from tablename; /*  DESC statement.에서와 같은 정보 */
table status /* mysqlshow –status와 같은 정보 */
142
• Viewing Server Status and Variables
• SHOW STATUS /* inside MySQL, or */
• mysqladmin -u username -p -extended-status /* command line에서*/
•  서버 실행 이후의 통계/정보. 예:
– com_select ; tells you how many select statements have been executed
– threads_connected: = current number of connections to the server.
– slow_queries: = number of queries this server has run that have taken more
time than the value of the server variable long_query_time. These queries
are logged in the Slow Query Log.
– uptime: This is how long this server instance has been running in seconds.
– To see the values of server variables, you can use
• show variables; /*from inside MySQL or */
• mysqladmin -u username -p variables /*from the command line */
• Viewing Process Information
• show processlist;
• mysqladmin -u username -p showprocesslist
143
• Viewing Grant and Privilege Information
show grants for username@host; 예:
mysql> show grants for root@localhost; 
+-------------------------------------------------------------------------------------------+
| Grants for root@localhost
|
+-------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
|
+-------------------------------------------------------------------------------------------+
1 row in set (0.40 sec)
– To remind yourself what the various privileges are:
• show privileges;
• Viewing Reference Information About Tables
• show table types;
• show create table tablename; /* 예: */
show create table department; 
CREATE TABLE 'department' (
'departmentID' int(11) NOT NULL auto_increment,
'name' varchar(30) default NULL,
PRIMARY KEY ('departmentID')
) TYPE=InnoDB CHARSET=latin1
• (참고: column names are quoted to be safe, and the default character
set—which we didn't specify—as specified here.)
144
• Setting Variables
• set variable=value; 예:
• set sql_safe_updates=1; /*turn on safe updates (=-i-am-a-dummy).
• Killing Threads
– …
• kill process_id;
• Clearing Caches
– 예: update user privilege by manually altering grant tables
 이들 변경사항이 roll through the system 여부를 확인하는 경우:
• flush privileges;
• flush query cache;
• /*  defragment the query cache performance개선.*/
– 또는
• reset query cache;
• /*query cache를 defragment하는 대신, 실제로 clear it altogether.*/
145
Understanding the Log Files
• Logging 필요 시에는 이를 switch on시킬 것. (set 명령어)
– Error log:
• 발생된 모든 error를 추적 (track). -- logged by default and appear in your data
directory.
• (파일명: hostname.err (Linux) and mysql.err (Windows).) 위치를 별도로 지정 가능.
option log-error=filename (in my.ini or my.cnf file.)
– Query log:
• 수행되는 모든 query를 log. You can turn on this log and specify the location with the
option log=filename.
– Binary log:
• 데이터를 변경시키는 모든 query를 log. (기존 update log를 대체.) version 5.0 이후 없어질
것. –turn on/ 위치 지정 by the option log-bin=filename.
– Slow query log:
• long_query_time변수에 저장된 값보다 오래 걸리는 모든 query를 log. -- turn on/위치 지
정 by the option log-slow-queries=filename
• 이들은 (binary log을 제외하고) 모두 text files.  mysqlbinlog logfile 명령
어로 볼수 있다. 또한 크기가 계속 늘어나므로 regularly rotate your log files!
– Linux의 경우
• mysql-log-rotate script file이용
– LX 이외의 경우
• move the old log files to a safe location manually and then tell MySQL to start using
a new log file with the command
146
• mysqladmin flush-logs
mysqladmin Option Summary
–
–
–
–
–
–
–
–
–
mysqladmin
mysqladmin
mysqladmin
mysqladmin
mysqladmin
mysqladmin
mysqladmin
mysqladmin
mysqladmin
create databasename
drop databasename
ping
version
status
extended-status
processlist
kill id1,id2,id3...
variables
147
14. Backup 및 Disaster
Recovery
148
Backing Up and Restoring Your Database
• 일반론
– 4 ways you can make a backup in MySQL:
• mysqldump script ; creates a dump file, that is, a file containing
the SQL statements necessary to re-create the database.
• mysqlhotcopy script ; creates a data file. This copies the data files
for a particular database directly.
• 데이터 파일을 직접 백업
– mysqlhotcopy 가 하는 일을 수작업으로 하는 것. 단, 이때 copy전에 해당
database를 shut down 또는 모든 table을 flush and lock (internal
consistency를 위해서).
– Both mysqldump and mysqlhotcopy will flush and lock for you, so they
are easier, safer options.
• BACKUP TABLE and RESTORE TABLE 명령어;
– 참고:
• 모든 backup에는 작업 동안 사용자 access 제한  To take a
consistent snapshot of a database, tables need to be flushed and
held constant while the backup is performed. 즉, 서버를 정지시키거
나 또는 table을 lock.
• 하나의 해결책 = replication. You can take down one slave and back
it up while users continue blissfully about their business.
149
• Backing Up and Restoring with mysqldump
– MySQL server를 접속하고 SQL dump file 생성 (  dump file에는 DB
re-create위한 SQL 문장이 수록되어 있음) 예:
• mysqldump --opt -u username -p password employee > backup.sql
– 여기서: --opt option; encapsulates a few other options
– Using this script on the simple employee database  Listing 14.1.
Listing 14.1 Sample Output from mysqldump
-- MySQL dump 10.2
--- Host: localhost Database: employee
----------------------------------------------------------Server version 4.1.0-alpha-max-debug
--- Table structure for table 'assignment‘
-DROP TABLE IF EXISTS assignment;
CREATE TABLE assignment (
clientID int(11) NOT NULL default '0',
employeeID int(11) NOT NULL default '0',
workdate date NOT NULL default '0000-00-00',
hours float default NULL,
PRIMARY KEY (clientID,employeeID,workdate)
) TYPE=InnoDB CHARSET=latin1;
150
--- Dumping data for table 'assignment‘
-/*!40000 ALTER TABLE assignment DISABLE KEYS */;
LOCK TABLES assignment WRITE;
INSERT INTO assignment VALUES (1,7513,'0000-00-00',5),(1,7513,'200301-20',8.5);
UNLOCK TABLES;
/*!40000 ALTER TABLE assignment ENABLE KEYS */;
--- Table structure for table 'client‘
--
DROP TABLE IF EXISTS client;
CREATE TABLE client (
clientID int(11) NOT NULL auto_increment,
name varchar(40) default NULL,
address varchar(100) default NULL,
contactPerson varchar(80) default NULL,
contactNumber varchar(12) default NULL,
PRIMARY KEY (clientID)
) TYPE=InnoDB CHARSET=latin1;
--- Dumping data for table 'client‘
--
151
/*!40000 ALTER TABLE client DISABLE KEYS */;
LOCK TABLES client WRITE;
INSERT INTO clientVALUES (1,'Telco Inc','1 Collins St Melbourne','Fred
Smith','95551234'), (2,'The Bank','100 Bourke St Melbourne','Jan Tristan','95559876');
UNLOCK TABLES;
/*!40000 ALTER TABLE client ENABLE KEYS */;
--- Table structure for table 'department‘
-DROP TABLE IF EXISTS department;
CREATE TABLE department (
departmentID int(11) NOT NULL auto_increment,
name varchar(30) default NULL,
PRIMARY KEY (departmentID)
) TYPE=InnoDB CHARSET=latin1;
--- Dumping data for table 'department‘
-/*!40000 ALTER TABLE department DISABLE KEYS */;
LOCK TABLES department WRITE;
INSERT INTO department
VALUES
(42,'Finance'),
(128,'Research and Development'),
(129,'Human Resources'),
(130,'Marketing'),
(131,'Property Services');
UNLOCK TABLES;
152
/*!40000 ALTER TABLE department ENABLE KEYS */;
--- Table structure for table 'employee‘
-DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
employeeID int(11) NOT NULL auto_increment,
name varchar(80) default NULL,
job varchar(30) default NULL,
departmentID int(11) NOT NULL default '0',
PRIMARY KEY (employeeID)
) TYPE=InnoDB CHARSET=latin1;
--- Dumping data for table 'employee‘
-/*!40000 ALTER TABLE employee DISABLE KEYS */;
LOCK TABLES employee WRITE;
INSERT INTO employee
VALUES
(6651,'Ajay Patel','Programmer',128),
(7513,'Nora Edwards','Programmer',128),
(9006,'Candy Burnett','Systems Administrator',128),
(9842,'Ben Smith','DBA',42),
(9843,'Fred Smith','DBA',131);
UNLOCK TABLES;
153
/*!40000 ALTER TABLE employee ENABLE KEYS */;
--- Table structure for table 'employeeSkills‘
-DROP TABLE IF EXISTS employeeSkills;
CREATE TABLE employeeSkills (
employeeID int(11) NOT NULL default '0',
skill varchar(15) NOT NULL default '',
PRIMARY KEY (employeeID,skill)
) TYPE=InnoDB CHARSET=latin1;
--- Dumping data for table 'employeeSkills‘
-/*!40000 ALTER TABLE employeeSkills DISABLE KEYS */;
LOCK TABLES employeeSkills WRITE;
INSERT INTO employeeSkills
VALUES
(6651,'Java'),
(6651,'VB'),
(7513,'C'),
(7513,'Java'),
(7513,'Perl'),
(9006,'Linux'),
(9006,'NT'),
(9842,'DB2');
UNLOCK TABLES;
/*!40000 ALTER TABLE employeeSkills ENABLE KEYS */;
154
• We could reload or re-create the employee database elsewhere by:
1. 적절한 이름의 DB를 생성한 후
2. Loading this file using: mysql -u username -p < backup.sql
• mysqldump script의 options;
• --quick: tells MySQL to dump the data directly to the file, rather than
buffering it in memory first (default). This will speed things up.
• --add-drop-table: Tells MySQL to add a DROP TABLE statement before each
CREATE TABLE in the dump.
• --add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements
• --extended-insert: use the multiline insert syntax to insert multiple rows
with a single INSERT. 앞의 예에서:
INSERT INTO employeeSkills
VALUES
(6651,'Java'),
(6651,'VB'),
(7513,'C'),
• /* If we have to use our backup to re-create the database, this will be
faster to execute than a series of single INSERT statements. */
• --lock-tables: lock all the tables before starting to dump.
– (주의) --opt (meaning optimized) optimizes the length of time it takes
to reload the dump file, rather than the length of time it takes to
create the dump file  Creating the dump file can be slow.
155
• Here are a couple of other useful options:
–
–
–
–
--databases: list more than one database for dumping.
--all-databases: dump all the databases it has in storage.
--allow-keywords: MySQL keyword를 column이름으로 사용 시 지정
-d or --no-data: Dumps only the database structure, not the
contents. 여러 서버에 DB테스트 또는 deploy시 유용.
• 장단점
– 장점: simple to use and it takes care of table locking issues for you.
– 단점:
• this script locks tables: Table 크기에 따라 수 초, 수 분.. 따라서 nonpeaktime에 수행.
• Because mysqldump works through the MySQL server, it will be slower
to run than mysqlhotcopy.
• mysqlhotcopy 는 MySQL server 자체는 별로 이용치 않고 직접 파일 시스템
이용.
156
• Backing Up and Restoring with mysqlhotcopy
– mysqlhotcopy
• (특징) copies the actual database data files, rather than retrieving
data through a connection to the server.
• 즉, DB table을 flush 및 lock하기 위해 DB서버에 connection을 설정하지만
주로 파일시스템 중심이므로 mysqldump보다 다소 빠르다.
– 사용:
• mysqlhotcopy -u username -p database_name backup_location
• Perl script이므로 (Ux/Lx에서는 상관없으나) Windows에서는 download
Perl for Windows www.activestate.com/Products/ActivePerl
– The files produced by mysqlhotcopy are replicas of the database
data files. To use these backups, you should stop the MySQL
server and replace the data files in the MySQL data directory
with the backed-up files.
157
• Backing Up and Restoring Manually
– = flush and lock the tables and copy the data files to a backup
location while the tables are still locked.
1. LOCK TABLES 명령어
lock tables
employee read,
department read,
client read,
assignment read,
employeeSkills read;
• /* LOCK TABLES 문장의 parameters = a list of table names and the type of
lock we would like to acquire, READ or WRITE. */
• 일반적으로는 backup을 위해서는 read lock이면 충분. 즉, other threads
(connections) can continue to read from the tables but will be unable to
write to them while performing a backup.
• Backup이 오래 걸리므로 Locking은 중요.
2. FLUSH TABLES 명령어:
• flush tables;
• 전체 DB 백업 시에는 다음 명령으로 한번에 처리 가능.
flush tables with read lock;
3. (중요) leave your session (where you locked and flushed the tables)
open. This makes sure that the locks are maintained. When you close
that session, the tables will be unlocked.
4. File copy 후 table을 unlock할 것.
158
• unlock tables;
• /* mysqlhotcopy script와 마찬가지, and you can restore in the same way. */
• BACKUP TABLE and RESTORE TABLE
– MyISAM table type에만 적용가능. 예:
• backup table t1 to 'path/to/backup';
– Windows에서는 드라이브 지정할 것.예:
• backup table t1 to 'c:/path/to/backup';
• /* The table will be read locked before it is backed up. */
– 여러 table은 comma로 연결  각 table은 차례로 locked & backed up.
• consistent set of table이 필요하면 LOCK TABLES 문장을 먼저 수행.
– To restore from the backup,
• restore table t1 from 'c:/tmp';
• /* 만약 같은 이름의 table이 있는 경우 DROP TABLE 먼저 수행 후 RESTORE. */
• Restoring from the Binary Log
– 대부분; backup으로부터 restore했을 때는 처음 backup한 이래로 발생한
insert/update를 반영하는 작업이 필요.
– 이들 변경사항은 binary log or update log에 저장되어 있다.
• mysqlbinlog logfile > updates.sql
– (주의) 작업 내역을 미리 점검! 예:
• update user set password='password';
• 즉, 이런 작업이 재 실행되지 않도록 주의.
159
Testing Your Backup
– …
– Test할 것
– 특히 install 및 recovery에서 binary log가 중요.
• It is not enabled by default, but it is needed to bring a
restored database back up to date.
160
Checking and Repairing Tables
• Checking tables for corruption is part of routine job
• MySQL에서 table check하는 3가지 방법:
– CHECK TABLE
– myisamchk (or isamchk)
– Mysqlcheck
• 문제 table의 repair;
– REPAIR TABLE or
– again myisamchk (or isamchk) or mysqlcheck.
• 어떤 option을 사용할지를 정하는데 고려해야 할 사항:
CHECK /REPAIR 명령어는 MySQL내에서, 다른 방법들은 명령어 줄에서 수행.
CHECK /REPAIR 명령어는 MyISAM과 InnoDB table 모두에 적용
isamchk script는 ISAM에, myisamchk와 mysqlcheck는 MyISAM에 적용.
myisamchk /isamchk 이용 시는 table을 사용하고 있지 않아야 하므로
locking 등의 조치.  자칫 data corruption.
– CHECK, REPAIR, and mysqlcheck are all safe to use when the server
is up and tables are in use.
– (계속)
–
–
–
–
161
• Checking and Repairing Tables with CHECK and REPAIR
– 예:
check table department; /* MyISAM and InnoDB tables 모두 가능. */ 
+-----------------------------+---------+-------------+-------------+
| Table
| Op
| Msg_type | Msg_text |
+-----------------------------+---------+-------------+-------------+
| employee.department
| check | status
| OK
|
+-----------------------------+---------+-------------+-------------+
1 row in set (0.00 sec)
– 또는 “Table is already up to date”, 즉, everything’s fine.
– 다른 message =문제 발생한 것이므로  REPAIR TABLE :
repair table t1;
– Repair가 정상적으로 되면 (or 애당초 repair가 불필요했으면) 
+---------+--------+--------------+-------------+
| Table | Op
| Msg_type | Msg_text |
+---------+--------+--------------+-------------+
| test.t1 | repair | status
| OK
|
+---------+---------+--------------+-------------+
1 row in set (0.03 sec)
– If you get any message other than OK, the REPAIR hasn't worked
and you will need to resort to the more powerful myisamchk.
162
• Checking and Repairing Tables with myisamchk
– (주의) myisamchk 사용 시 stop the server!
– 용례:
myisamchk table /* at the command prompt. */
• /* The table should be the path to a .MYI file that represents a MyISAM
table. This will report virtually all errors. If it doesn't seem to be finding
your problem, you can try running it with the -m switch. The default
behavior looks for corruption in the indexes; with this switch, the rows
are scanned as well. */
– myisamchk을 이용한 repair
• -q -r options for quick recovery, as shown here:
myisamchk -q -r table
• /* 안되면 back up the data file and try a full recovery: */
myisamchk -r table
• /* 안되면 try --safe-recover option ; -r 로 처리 안된 error를 fix: */
myisamchk --safe-recover table
• /* myisamchk 을 parameter없이 입력하면 많은 option을 볼 수 있다. */
163
• Checking and Repairing Tables with mysqlcheck
– mysqlcheck program ; checks MyISAM and InnoDB tables and
repairs MyISAM tables safely while the server is up and running.
• mysqlcheck -u username -p employee
– 아무 문제 없을 때에는 
•
•
•
•
•
employee.assignment
employee.client
employee.department
employee.employee
employee.employeeSkills
OK
OK
OK
OK
OK
– --databases switch ; specifies a list of databases to check
– --all-databases option ; checks all the databases on the server.
– -r option ; repairs any corrupted MyISAM tables that it encounters.
164
15. Securing Your MySQL
Installation
165
How the Privilege System Works in Practice
• two stages to the privilege system.
– 1 단계
• 사용자가 서버에 연결해도 되는지 확인 –입력된 username/ password과 host
를 비교 -- user table 이용. 즉, matching row가 있어야 됨
• user table이 host column에서 wildcard를 지원하므로 user/hostname
combination이 여러 개의 row에 match될 수 있다. MySQL determines which
row is relevant by matching the most specific hostname first.
– 예: if there are rows in the table for test from host localhost and user test
from host % (meaning any host), then the localhost row will be selected.
Note that these two rows can have different passwords.  confusion.
– 2 단계
• 특정 query or command 수행 시 grant table로 확인 후 수행.
– 수행하려는 query가 global privilege를 필요로 하는 경우—예: LOAD DATA INFILE
or trying to use SHOW PROCESSLIST—user table을 check.
– For database-specific queries, the user table will be checked first.
– If the user has the privilege on all databases, this will be sufficient.
– If not, then the db and host tables are checked.
– If (여기서도 확인 안되면) then 마지막으로 table- or column-level privileges 확인.
166
Securing Accounts
• Setting the Password for the Root Account
– MySQL서버 설치 시 반드시 즉각 root password를 먼저 설정할 것. (default설정 x)
• Deleting Anonymous Accounts
– MySQL을 Windows에 설치할 때, 자동적으로 특정 account를 생성
– Linux의 경우에는 mysql_install_db script수행 시 발생.
– Two of these accounts are anonymous; (No passwords set by default)
• host value of localhost and
• % (any other host, so effectively any remote connection).
– Delete these accounts!!
delete from user where User=‘ ‘;
delete from db where User=‘ ‘;
• /* follow this with FLUSH PRIVILEGES to flush the grant tables. */
– (다른 이유) 이들 account가 혼란야기 when regular users try to log in.
• 예컨대 laura 라는 username at any host (%)를 생성 
• laura tries to connect from localhost  MySQL server looks for matching entries in
the user table. It has laura@% and (anonymous)@localhost.
• 이때 MySQL은 the most specific hostname을 우선적으로 match시키므로 matching
row는 (anonymous)@localhost가 된다. Note that although laura has supplied a
username, this doesn't matter! The anonymous accounts don't require a username.
This anonymous account is likely to have a different password from laura's account
(by default, the password is blank, meaning the user should not supply one). 즉,
when laura tries to log in with her username and password from localhost, she will
get an Access Denied error for no obvious reason.
167
• Dangerous Privileges
– MySQL has a very fine-grained privilege system을 가진다. (Ch 11) 따
라서 privilege grant시 주의. 특히 FILE, PROCESS, and WITH GRANT
OPTION을 조심할 것!
– FILE privilege ; allows users to LOAD DATA INFILE. This can be
manipulated to load in files from the server (such as the password
file /etc/passwd) or even database data files, effectively
circumventing the privilege system.
– PROCESS privilege ; allows users to SHOW PROCESSLIST. (이때 수
행 중인 query 등 사용자 관련 정보가 유출될 가능성)
– WITH GRANT OPTION privilege ; allows a user to share his
privileges with others. 따라서 위험성을 명확히 인식한 상태에서만 사용.
• Passwords and Encryption
– MySQL user passwords are encrypted. Before version 4.1, you
could use the encrypted password as stored to log in. This has now
been fixed.
– (non-MySQL) username 및 password 저장하는 프로그램 개발 시
PASSWORD() 함수보다는 MD5() or ENCRYPT()를 이용할 것.See Ch 8.
168
Securing Your Installation Files
– 앞서의 항목 이외에도 MySQL binaries, scripts, and data files에 대한
access를 통제할 것!
• Don't Run mysqld as Root
– …
– (Web server 에서처럼) 별도 계정을 생성
• Access and Privileges Under Your Operating System
– 무엇보다 control file access in your operating system !!
• MySQL binaries, scripts, 특히 data directory에 대한 사용자 access를 통제!
• 흔한 오류 = 서버가 있는 시스템에 계정을 가진 자가 자칫 데이터 디렉토리에
들어와서 다른 MySQL 서버로 데이터 복제하는 문제.
– (following safeguards)
• Only appropriate users can run mysqld.
• 적절한 사용자만이
– MySQL에 연관된 program과 script에 access할 수 있도록 할 것. (예:
mysqladmin, mysqldump, mysqlhotcopy. – program 별로 검사할 것).
– MySQL data directory에 access할 수 있도록 할 것. If the server is running as
user mysql, this user will need access to the directory. Any other users are
optional and are therefore generally best denied.
169
Filtering User Data
• Before passing on any user-entered
data to MySQL, you should do some
application-level error checking.
– 예: user name에서의 ‘ (apostrophe)
170
Other Tips
• Using SSL Connections
– …
– OpenSSL library (available from www.openssl.org)를 설치하고 start
the server with the --with-vio and --with-ssl options, and do some
setup at the command line. (MySQL manual 참조)
– 설치 후 restrict GRANT statements by requiring users to connect
using SSL or to have an appropriate certificate. 예:
• grant all on employee.*to testuser identified by 'password'require ssl;
• /* This creates (or modifies) an account for testuser, giving the user the
password password. This user will be able to connect only via SSL. You can
demand that all your users connect this way or perhaps all users logging in
from anywhere other than localhost. */
• Securing Your Installation Physically
– … 중요 …
171
16. Replicating Your Database
172
Replication Principles
•
다양한 Replication 형태 중 MySQL은 directional master-slave relationship.
– Master controls what data is stored, while the slaves try to mirror that content.
– Master의 binary log stores details of every query executed on the server since
logging was enabled. Slaves are sent queries from the master's binary log to
apply to their own stored data.
– 일반적으로 write operation은 직접 master에 수행하고 read operation은 모든 slave 또
는 master-slave간에 공유.  통상 application logic.
– (유의) 데이터를 가진 기존 database에 replication 추가 시  the binary log may be
incomplete. Binary logging는 자동 (by default) 구동되는 것이 아니므로 정상적 복제를
위해서는 binary log 시작할 시점에 모든 slave가 master와 같은 데이터를 가져야 한다. 
– A thread on the slave connects to a thread on the master and requests new
events. These are stored in a relay log on the slave. A separate thread on the
slave reads events from the local relay log and executes the queries on the local
mirror of the data.
– Master와 slave가 서로 다른 시점에 시작될 수 있으므로 (예: query도중 시스템 추가 또는
네트워크 fail 또는 bottleneck발생), slaves need to be able to keep track of where
they are in the log of updates to be performed. It is important that atomic
transactions are honored and updates are performed in order.
– 대부분 consistent state로 복제되기만 하면 몇 분 (초)의 시간차이는 크게 중요치 않다.
– (내부작용) Updates are asynchronous and do not happen in real time. Queries sent
to different servers can give different results for some time after an update is
made. This can be seen as a negative, but the positive side is that if you have a
slave running on a portable device or an unreliable network, it will happily operate
for long periods between updating data from the master.
•
A Note on Versions
– MySQL (3.23.15)부터 적용. 가급적 최신 version으로… Older version이 불가피하면
173
• www.mysql.com/doc/en/Replication_Implementation.html
Setting Up and Configuring for Replication
• (일반론)
– Internet사용 시 보안 문제
– 3306 default port가 firewall을 통과할 수 있는지 확인.
• Create a Replication User
– Master에서의 replication을 위한 계정  root
– If you are going to populate your slaves initially by using LOAD
TABLE FROM MASTER or LOAD DATA FROM MASTER, your
replication user needs a special set of permissions.
– 예: 연결설정을 위해서는 필요 permission을 가진 사용자 계정을 생성
grant replication slave, reload, super, select
on logs.*
to replication@" %" identified by 'password';
• (Note: 여기서는 logs database 이용하므로 이를 변경시켜 줄 것.)
– 일단 initial copy from master to slave가 완전히 수행되면 replication
사용자는 그리 많은 permission을 필요로 하지 않는다. If you are
populating your slaves from a backup or reducing the user's
permissions after the initial copying is complete, the user needs
only the replication permission, so the following query will create
a user named replication that can connect from any of the slave
servers (and any other machines):
• grant replication slave on logs.* to replication@"%"
'password';
identified by
174
• Check Master Configuration
– Master server에서는 binary logging이 enabled 되어야 한다. …
– 확인:
• show variables; /*  complete list */
• show variables like "log_bin" ; /*, but for concise output */
– If binary logging is off, add log-bin to options file as in Listing 1.1.
•
•
•
•
Options file의 이름= my.ini or my.cnf (운영체제에 따라 달라짐)
Edit your my.ini/my.cnf file to give your master server a unique id. 예:
[mysqld] log-binserver-id=1
여기서 server-id는 MySQL 서버마다 할당되는 unique identifier 로서 양의 정수
(a positive integer)여야 한다.
– options file 편집 후에는 서버를 restart해야 효력 발생.
175
•
–
–
Create a Master Snapshot
Replication을 위해서 다음의 3가지가 필요
1.
2.
3.
A complete, consistent snapshot of the current database
Master 서버의 binary log file 이름
The offset into the binary log where the server is currently
이들은 다음 요인에 의해 영향 (i) MyISAM or InnoDB tables (ii) 특정 기
간 내에 서버 정지를 얼마나 허용할 것인가의 문제.
•
•
MyISAM table; you can grab a snapshot after you start each slave. ; 비
효율적, 특히 if you have a large amount of data and many slaves. For
each slave, the LOAD DATA FROM MASTER query will obtain a lock on
the master's data and hold it until a complete copy has been transmitted.
You can lock the databases for a much shorter time by making the
snapshot manually via the file system. This will also allow you to use
one snapshot to start as many slaves as required, reducing time when
the server is locked.
InnoDB table; LOAD DATA FROM MASTER query option이 없다.
–
–
–
You can make a file-system snapshot or buy the hot backup tool.
이 경우 우선 다음 명령을 통해 보관된 데이터가 consistent & up-to-date 한지 확인.
get the current binary log file and offset
flush tables with read lock; /* This will also lock the table. */
show master status;
+-------------------------+------------+------------------+------------------------+
| File
| Position
| Binlog_do_db | Binlog_ignore_db
|
+-------------------------+------------+-------------------+------------------------+
| server-bin.000007
|
211
|
|
|
+-------------------------+-------------+------------------+------------------------+
–
첫째 (binary log 파일이름) 및 둘째 column (offset into the binary log)내용을
176 기록.
이들 내용이 empty이면 파일이름으로는 empty string 그리고 offset으로는 4를 이용.
– MyISAM table의 snapshot을 만들려면 단지 데이터를 보관한 디렉토리를
archiving program을 이용하여 복사하면 된다.
• Unix에서는:
tar -cvf /tmp/snapshot.tar /path/mysql/data/logs
• Windows에서는:WinZip or 기타 archiving tool을 통해 grab a copy of the
directory C:\mysql\data\logs.
– copy가 완료된 후 re-enable write access to the database by typing:
unlock tables;
– 이상의 논의는 InnoDB table에 대한 것.
• If you have purchased the (commercial) InnoDB hot backup tool, it is perfect
for this task. (www.innodb.com/hotbackup.html)
• Without this tool, the safest approach is to flush and lock the database with
the following queries. Use
• flush tables with read lock;
• /* 그런 후display and record the binary log file and offset (as for MyISAM): */
show master status;
– Without unlocking the database, shut it down and make a copy of the
directory that relates to that database inside your MySQL data directory.
For InnoDB tables, you will also need to copy across the data files and
logs. After snapshot is complete, you can restart and unlock database.
177
• Configure Slaves
– 각 slave는 고유한 server id를 필요로 하므로 options파일 (my.cnf/my.ini)에 다
음 내용을 삽입.
• server-id=2
– The id = positive integer (단, 서로 다른 숫자이기만 하면 어떤 값도 상관없음)
If you are going to have more than a handful of servers running, an
escalating sequence is probably your best hope of keeping them unique.
– If you are working from a file-system snapshot, you need to copy the
files into the appropriate places on the slave server.
– If you are working with more than one operating system, remember to
consider filename capitalization.
– Editing your options file or copying across InnoDB files will require you to
restart your slave server.
178
• Start Slaves
change master to master_host='server',
master_user='replication',
master_password='password',
master_log_file='server-bin.000007',
master_log_pos=211;start slave;
• /* 이 예에서 server = hostname of the master server.
replication = replication을 위해 생성한 사용자의 username
password = 그 사용자의 암호.
그 외에 binary log file name 및 offset
• START SLAVE query ; launches the slave's replication threads, causing
it to try to connect to the master and collect updates. */
– If you have copied a snapshot via the file system, you should be
able to run some matching queries against the master and slave
to check that the replication is working correctly. Make a small
update to the master and check that it is mirrored on the slave.
– If the tables you are replicating are relatively small MyISAM tables,
you can create and populate them via a query like
load table logs.logJan2003 from master; /* copy a single table or */
load data from master; /* to copy all tables onto this slave. */
– replication 동작 후에는 query를 manually 입력해서 configure
replication하는 것은 practical하지 않다. (, even if only occasionally.)
같은 정보가 The options file에서는 slightly different syntax! 예:
[mysqld]
server-id = 2
master-host = server
master-user = replication
master-password = password
replicate-do-db = logs `
179
Advanced Topologies
• 가장 일반적인 사용처; load balancing (주로 많은 read와 상대적으
로 적은 writes). 대부분 single master + a small number of
slaves
• Slave 수가 많거나 지역적으로 spread되면 cascade.
– Figure 16.1. Replication with Cascading Masters
• 순환 관계(circular relationship) 시 복잡해 질 수 있다. 예: 시스템 2
대가 서로 master-slave/slave-master로 엮인 경우 프로그래밍 주의.
– Because changes are applied asynchronously, you can end up
with conflicting auto increment fields, clashing unique ids, and
inconsistent data. In some applications, this arrangement may
work well. For instance, a data logging application with few
relationships between tables that requires high throughput and
availability may be willing to sacrifice consistency.
180
Replication Future
• standard distribution 에 포함되어 있고 상당히 안정되었으나 일부 미진한 점:
– the process for setting up a new slave with a snapshot is not very userfriendly.
– The treatment of MyISAM tables and InnoDB tables is not consistent.
– Options for setting up SSL connections between slaves and masters.
– MyISAM에도 InnoDB에서와 같은 hot backup tool이 있을 것.
• 향후 계획:
– multimastering—하나의 slave가 여러 master를 mirror하면서도 resolve conflict.
– built-in failover and load-balancing features.
• 현재는 application에서 해결하던가 또는 3rd-party clustering tool 필요. (EAC)
• Some replication-related settings in the options file that are currently
unimplemented.
– Currently, if you want to make secure connections, you need to use another
product, such as Stunnel.
181
17. MySQL 서버 Configuration의
최적화
• Do it empirically.
– 하나씩 바꿔 가면서 이전과 비교
– Benchmark 시에는 log a period worth of queries (an hour, a
day, or some average period) and then replay those
queries with the new server configuration.
182
Compiling and Linking for Speed
• 직접 컴파일  성능 향상: extra 10% to 30%
– If you have a Pentium-based machine and run Linux,
you will be able to get a significant performance
increase by compiling MySQL with the pgcc compiler,
which optimizes for Pentium only.
– With AMD chips compiled with plain gcc.
• 필요한 character set(s)만 적용하고 Compile
183
Tuning Server Parameters
– 현재의 설정상태 파악: show variables;
– See the effects of your server configuration:
show status;
– 현재 서버의 진행 상황을 Monitor: Jeremy Zawodny's mytop Perl script.
http://jeremy.zawodny.com/mysql/mytop
• = Unix top command와 유사
• shows what processes are active, the process state, the time spent, etc.+ drill
into a MySQL process and see the actual query being executed.
• Server parameters in my.cnf options files
– MySQL’s sample my.cnf files in the support-files directory.
• 4 suggested my.cnf files in this directory: my-huge.cnf, my-large.cnf, mymedium.cnf, and my-small.cnf.
• (이하 다음 페이지에 …)
• 2개의 가장 중요한 parameters: key buffer와 table cache
• (이외에도) 다양한 chunks of memory allocated on a per-thread basis
– read buffer size, controlled by the read_buffer_size parameter,
– The sort buffer, controlled by the sort_buffer parameter,
184
• 2개의 가장 중요한 parameters: key buffer와 table cache
• 이들은 서버에서 수행되는 모든 thread에 공유되어 있다. MySQL의 메모리 이
용과 관련됨.  MySQL’s internal buffers 및 caches를 통해 각각의 task에
적절히 할당되었는지 확인.
– key buffer = MyISAM indexes가 보관되는 메모리 장소.
– Index block이 이용될 때 이들은 buffer로 load됨.  query 수행 시 해당 index
block이 buffer에 있으면 거기서 읽고, 없으면 disk에서 keybuffer로 load시킨다
(따라서 늦다). 일반적으로 key buffer는 클 수록 좋다.
• key_buffer_size의 결정  메모리 크기, 서버가 dedicated MySQL server
인지 여부, index data의 크기 (즉, how big your .MYI files are in total).
– Jeremy Zawodny는 dedicated서버 총 메모리의 20% ~ 50% 권장. shared
machine의 경우 더 적게 설정! 또한 index data가 작을 때도 적게 설정. If you
have only 20MB of index data, there is little point in allocating 128MB to
the key buffer.
• (주의) key buffer는 단지 MyISAM tables을 위한 것이므로 다른 table type
에는 다른 parameter 적용할 것. (예: InnoDB table만 사용 시에는
innodb_buffer_pool_size를 조절.  InnoDB buffer pool stores both
index and table data.)
– table cache의 문제 - controlled via the table_cache option.
• 동시에 open 될 수 있는 테이블의 최대 개수를 한정. (즉, MyISAM table의 경
우, 각각의 테이블과 인덱스는 OS상에서의 개별적인 파일) Opening and
closing files is slow, so these files are left open until they are
explicitly closed, the server shuts down, or the total number of open
tables exceeds the table_cache parameter. table의 숫자가 많을 때는
table_cache 값을 증가시키면 좋다. (또한 OS에 따라 open file의 수 또는
single process/user 당 가능한 open file의 수가 한정되므로 table_cache
185
값을 reset하기 전에 살펴 볼 것)
• (이외에도) 다양한 chunks of memory allocated on a perthread basis
• The value is the same for each thread, but each thread can
have this amount of memory allocated to the specified
purpose.
– read buffer size, controlled by the read_buffer_size
parameter,
• 테이블 데이터를 저장하기 위해 full table scan 할 때 사용. 보관되는
table data가 많을 수록 disk read는 작아진다. 단, 자칫 너무 크면 각
각의 thread에 대한 read buffer가 너무 많은 메모리를 소비할 수도
있다. (You may want to note that this parameter was
previously called the record buffer and was controlled by the
record_buffer parameter.)
– The sort buffer, controlled by the sort_buffer parameter,
• is used when you run queries containing ORDER BY clauses.
186
Tuning Other Factors
• tips
• MySQL recommends Solaris (특히 multiprocessor사용 시)
• Multiple disk 사용 시 각각의 database에 적합한 disk 사용.
– RAID—RAID 0 will improve reading and writing performance,
and RAID 1 or 5 will improve reading performance.
• Consider use of a journaling file system, such as Reiserfs
or XFS.
• Fast networks when using replication.
187
Chapter 18. Optimizing Your Database
• 일반적인 database design guidelines and normalization
+
• What's slow in MySQL databases?
• Making the right design choices
• Using indexes for optimization
• Using OPTIMIZE TABLE
188
What's Slow in MySQL Databases?
• Server optimize 후 다음 사항 검토:
– Not using enough indexes.
• 가장 흔한 문제 = tables that have no indexes or that are
without indexes on columns you are searching.
• 그렇다고 index가 많을 수록 좋은 것은 아니다. 
– Using too many indexes.
• If you are retrieving data, indexes are good.
• When you are inserting new rows, updating rows, or deleting
rows, indexes are no longer your friends. When you update
data, the indexes need updating too, increasing the amount of
overhead you have to deal with.
– Using table- and column-level privileges.
• 많은 resource에 table- or column-level privileges 설정하면
MySQL이 이를 query시마다 확인해야 한다.
– Making the wrong database design choices.
• …
189
Making the Right Design Choices
•
Use the smallest type that data will fit in.
– 예: storing numbers of 1 ~10: INT 대신 TINYINT 사용.
– Row와 table이 작을 수록 검색이 빠르다. + 데이터가 적을 수록 더 많은 row를 cache.
•
Use fixed-length records where possible.
– …
– no VARCHAR, no TEXT, and no BLOB.
•
•
If you need to store TEXT and BLOB, you might consider denormalizing
your schema to break the TEXT or BLOB fields out into a separate table.
VARCHAR만 사용하는 경우 CHAR로 변환을 검토.
– = a trade-off with more space on disk, against suggestion above
•
Declare as many columns NOT NULL as possible.
– If your data logically requires NULL values, then obviously you should use them.
단, small speed and storage space price for NULL.
•
Choose the table type on a table-by-table basis.
– Non-transaction-safe tables (MyISAM) involve a lot less overhead  faster than
the transaction-safe types (InnoDB and BDB).
•
•
Choose appropriate indexes.
극단적인 경우 even consider denormalization of tables to reduce the number
of joins made for common queries.
– 단, 관리가 어려워질 수 있기 때문에 매우 조심해서 결정할 사항.
190
Indexing for Optimization
• 개요
– check if database has any indexes. ( DESCRIBE 명령어)
• index = a lookup table to find specific rows in a table quickly.
– Indexes in MySQL are stored as b-trees
– can be on a single column or can span multiple columns (just like keys).
An index will be used when running a query, if the search is being
performed on the following:
• A single column that has a single-column index. 예: if we index departments
on departmentID and perform a query like SELECT...WHERE departmentID=n.
• A set of columns that forms a multicolumn index. 예: if we have created an
index on the employee.assignment table on (clientID, employeeID, workdate)
and we perform a query like SELECT...WHERE clientID=x AND employeeID=y
AND workdate=z.
• A column or set of columns that forms a subset of a multicolumn index, as long
as there is a leftmost prefix of the index columns. 예: with the assignment
table as before, with an index on (clientID, employeeID, workdate), indexes
would be used for these types of queries:
• SELECT...WHERE clientID=x
• SELECT...WHERE clientID=x AND employeeID=y
– But, they would not be used for this type:
• SELECT...WHERE employeeID=y AND workdate=z
– (하나 또는 여러 column에 대해) queries that does not fit the preceding
criteria가 많은 경우, run CREATE INDEX statement to create an
191
appropriate index. Note that MySQL can use only one index per table in
a single query. It cannot combine existing indexes automatically.
ANALYZE TABLE
• use the ANALYZE TABLE statement to
review and store the key distribution in
a table.
– MySQL stores this information and uses it to
decide how to execute joins.
• analyze table tablename ;
192
Using OPTIMIZE TABLE
• = MySQL equivalent of defragmenting your hard disk.
• OPTIMIZE TABLE tablename;
– use OPTIMIZE TABLE periodically …
–  스토리지를 잘 정돈하고, re-sort the index, and update
statistics for the table.
– MyISAM and BDB tables 에만 적용
193
19. Query 최적화
• 개별 query 분석을 통해 성능향상 가능.
• In this chapter:
–
–
–
–
–
–
Finding slow queries
Benchmarking your queries
Using the slow query log
Using EXPLAIN to see how queries are executed
Understanding MySQL's built-in query optimization
Optimization tips
194
Finding Slow Queries
• 시간을 많이 소요하는 부분이 어느 곳인지 찾는다.
– Observation: 흔히 특정 query가 늦다는 것을 발견
– Benchmarking: Test your application to see which
parts of it are slow.
– Slow query log: This log tracks slow queries, as you
might expect from its name.
• 일단 시간지체 부분을 확인하면 그 원인 등을
EXPLAIN 을 통해 분석하고 optimize the query.
• 모든 query를 speed up 할 필요는 없다. 가장 영향
도가 큰 부분에 주력. 잘못하면 작은 성능개선에 많
은 시간이 소요되기도 한다.
195
Benchmarking Your Queries
• Benchmarking = timing how long your queries take.
– by running a query many times …
•  A single execution of the query will be subject to load issues
• 또한 2번째로 실시하면 query수행이 빨라진다.  the query is cached.
– 또한 external scripts or programs 를 이용하기도 함.
• 예: download the source distribution of MySQL and look at the benchmarking
code that is included in the sql-bench directory.
– 또한 built-in BENCHMARK() function 실시
• 예: select benchmark(1000000, 6*9); 
+-----------------------------------+
| benchmark(1000000, 6*9)
|
+-----------------------------------+
|
0
|
+-----------------------------------+
1 row in set (0.25 sec)
• /* 2 parameters: the number of times to evaluate the expression (여기서는 1백
만) and the expression we want to evaluate (여기서는 six times nine). */
• BENCHMARK() function 결과값은 항상 0이므로 의미 없고 대신 query 수행시간이 중요.
(여기서는 evaluating 6x9 one million times took a quarter of a second.)
– 또한 pass BENCHMARK() a query, 예:
select benchmark(10000000, 'select employee.name, department.name from
employee, department where
196
employee.departmentID=department.departmentID');
Using the Slow Query Log
• (목적) 어떤 query가 늦은지 추적(track) (단위: 시간)
• (방법) turn on slow query logging with the --log-slowqueries=filename option when starting the MySQL server or in
your configuration file.
– If you also turn on the --log-long-format option, all queries that
run without using an index are also logged. This can help you to
see where you should be targeting your optimization efforts.
– You can define what is meant by a slow query by using the
long_query_time variable. You can set this in your configuration
file or by using the SET command. This variable is in seconds.
– slow query log는 text file이므로 직접 볼 수 있다. Summary만 볼 수도
있다.  by running the mysqldumpslow script (in the scripts
directory of your MySQL installation. 또한 Perl script이므로
Windows에서는 Perl 설치 필요)
• (한계) you cannot configure it to log slow queries that take
less than one second. The administrator might want to be
informed when queries take more than one-tenth of a second
or some other fraction.  future version.
197
Using EXPLAIN to See How Queries Are Executed
•
예:
explain
select e.name, d.name
from employee e, department d
where e.departmentID = d.departmentID; 
+----+-------------+-------+--------+---------------+--------------+-----------+-----------------------+------+--------+
| id | select_type | table | type | possible_keys| key
| key_len | ref
| rows | Extra |
+----+-------------+-------+--------+---------------+--------------+-----------+-----------------------+------+--------+
| 1 | SIMPLE
|e
| ALL
| NULL
| NULL
| NULL
| NULL
| 5 |
|
| 1 | SIMPLE
|d
| eq_ref | PRIMARY
| PRIMARY | 4
| e.departmentID | 1 |
|
+----+-------------+-------+--------+----------------+-------------+------------+----------------------+------+--------+
2
•
•
•
•
•
•
•
•
•
•
•
rows in set (0.00 sec)
/* table당 하나의 row. Row의 순서 = table이 join된 순서.
id = a sequence number. (예컨대 subquery사 용시, each SELECT is numbered.)
select_type =(대부분은 SIMPLE (a plain vanilla SELECT). subqueries 사용 시에는, the outer query
가 PRIMARY, inner queries가 SUBSELECT or DEPENDENT SUBSELECT)
table This is the table this row is about.
type ; tells you how the table is being joined to the other tables in the query.
possible_keys ; 어떤 index가 이용될 수 있었는지 표시 (relevant indexes 가 없으면 NULL)
key ; query에 사용된 index 표시. (NULL if no index was selected)
key_len = the length of the index MySQL decided to use.
ref = the value being compared to the key to decide whether to select rows.
rows = an estimate of the number of rows from this table MySQL will read to generate the
results of the query. You can work out how many rows will be read overall by multiplying the
rows' values together. This gives a basic benchmark for how fast the query will run.
Extra = 추가정보 (예: Using index means that MySQL can retrieve the result of the query
completely from an index without reading data from the table.)
(해석 뒷장)
198
–
–
Employee table에서의 join type ALL; 모든 row들이 scan될 것을 의미. table에 데이터가 많으면 늦어질것. 실제
로 join type ALL은 최악의 경우. ( typically if a table has no useful index.) (해결책) add an index.
Department에 해당되는 row의 join type eq_ref; (의미) a single row will be read from the department table
for each row in the employee table. (= one of the best types.) The only better values for type are system
and const, (의미: table이 오직 하나의 matching row를 가지며 can effectively be treated as a constant. )
– (other possible values for type: )
• ref ; All rows with matching index values will be read from the table. (eq_ref 다음의
second best)로서 non-unique key 상태일 때임.
• range ; (의미) all rows in a particular range will be read from the table. (eq_ref 또는 ref
만도 못 함)
• index ; (의미) the complete index will be scanned. (ALL보다는 좋지만 앞서의 다른 것보다
못함.) This is preferable to scanning the complete table, but is far from ideal.
• possible_keys and key ; The department table has one option: PRIMARY. Employee
table has the value NULL in both of these columns, (의미: there's no key to use and
therefore no key will be used. = a strong hint that we should be adding another index!
– 이 정보에 따라 다음과 같이 추가 index 생성 
create index ename_did on employee(name, departmentID);
• If we then rerun EXPLAIN, we get the following output:
+----+-------------+-------+---------+-------------------+---------------+-----------+---------------------+------+---------------+
| id | select_type | table | type
| possible_keys
| key
| key_len | ref
| rows | Extra
|
+----+-------------+-------+---------+-------------------+--------------+------------+---------------------+------+---------------+
| 1 | SIMPLE
|e
| index
| NULL
| ename_did |
85
| NULL
|
5 | Using index |
| 1 | SIMPLE
|d
| eq_ref | PRIMARY
| PRIMARY
|
4
| e.departmentID
|
1|
|
+----+-------------+------+----------+-------------------+---------------+----------+----------------------+------+---------------+
2 rows in set (0.00 sec)
• /* employee의 type은 index ( 이제 적합한 index를 가지므로) The new index is listed as
a possible key, but it is not actually being used. Under Extra you will see that only the
index for this table is used, rather than the table itself. This should be slightly faster.
• (EXPLAIN ; query속도증진을 위해 index를 어떻게 활용할지 알려 줌.)
199
Understanding MySQL's Built-In
Query Optimization
•
MySQL applies many optimization rules to queries.
– MySQL uses its estimated number of rows (as shown in EXPLAIN) to work
out the best order in which to join tables. If you notice that its estimate is
off, you may want to experiment with using a STRAIGHT JOIN to force the
table order. Benchmarking before and after cases will tell you whether you
are helping or hindering.
– To choose an index, MySQL looks for the most relevant index that spans
less than 30% of the rows. If it can't find an index fitting these criteria, the
table will be scanned instead. (This was what happened in the EXPLAIN
query we looked at earlier, after we added the new index.)
– Expressions in WHERE clauses are optimized in a similar way to the way
many programming compilers optimize expressions. For example,
unnecessary parentheses in expressions are removed. This is one reason
you should feel free to make your queries more readable with parentheses.
– If a query can be resolved wholly from indexes, it will be done without any
reference to the actual rows in the table. Evaluation of COUNT(*) is also
evaluated without reading or counting the rows in a table because this data
is stored separately.
•
Manual, source code 분석할 것 !!
200
Optimization Tips
• Add indexes.
– Chapter 18, "Optimizing Your Database."
– Remember though that although an appropriate index might
speed up operations that need to find data in the table, keeping
indexes up-to-date increases the time required to write data. Do
not add indexes that will not be used.
• Use ANALYZE TABLE.
– (See Chapter 18 for syntax.)
– This updates the information MySQL stores about key distribution.
This information is used to decide the order in which tables are
joined. If MySQL seems to be joining your tables in a strange
order, try ANALYZE TABLE.
• Use OPTIMIZE TABLE.
– (See Chapter 18 for syntax.)
– This defragments the table storage, sorts the indexes, and
updates the table statistics as used by the query optimizer.
201