mysql

advertisement
10-2. MySQL
 설치
 http://www.mysql.com/
 다운로드 후
 한글 코드 설정
 암호 설정
 나머지는 디폴트로
금오공과대학 컴퓨터공학부 컴퓨터공학전공
1
 수행 방법
GUI 방식
금오공과대학 컴퓨터공학부 컴퓨터공학전공
콘솔 방식
2
로그인과 종료
mysql –h 서버명 –u 유저 –p 비밀번호 데이타베이스명
quit or bye
금오공과대학 컴퓨터공학부 컴퓨터공학전공
3
기본 명령어
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql
|
| sampledb
|
| test
|
+--------------------+
4 rows in set (0.03 sec)
mysql> use sampledb;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_sampledb |
+--------------------+
| course
|
| professor
|
| student
|
+--------------------+
3 rows in set (0.06 sec)
mysql> desc course;
+-------+------------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| Cno | int(10) unsigned | NO | PRI | NULL
|
|
| Sno | int(10) unsigned | NO | PRI | NULL
|
|
| mid | int(10) unsigned | NO |
| NULL
|
|
| final | varchar(45)
| NO |
| NULL
|
|
+-------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
금오공과대학 컴퓨터공학부 컴퓨터공학전공
4
함수 정리
mysql> select now();
+---------------------+
| now()
|
+---------------------+
| 2009-05-11 13:12:35 |
+---------------------+
1 row in set (0.00 sec)
mysql> select now(), user(), version(), database();
+---------------------+----------------+------------------+------------+
| now()
| user()
| version()
| database() |
+---------------------+----------------+------------------+------------+
| 2009-05-11 13:13:01 | root@localhost | 5.1.34-community | sampledb |
+---------------------+----------------+------------------+------------+
1 row in set (0.00 sec)
mysql>
금오공과대학 컴퓨터공학부 컴퓨터공학전공
5
사용자 관리 1
mysql> use mysql;
Database changed
mysql> insert into user(host, user, password)
-> values('localhost','user4','');
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
mysql> insert into mysql.user
-> (user, host, password, ssl_cipher, x509_issuer, x509_subject)
-> values('user4','%',password('1234'),'','','');
Query OK, 1 row affected (0.02 sec)
mysql> select user from user;
+-------+
| user |
+-------+
| user1 |
| user2 |
| user4 |
| root |
+-------+
4 rows in set (0.00 sec)
금오공과대학 컴퓨터공학부 컴퓨터공학전공
6
사용자 관리 2
mysql> flush privileges;
Query OK, 0 rows affected (0.09 sec)
mysql> update user
-> set password = password('abcd')
-> where user = 'root'
-> ;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
금오공과대학 컴퓨터공학부 컴퓨터공학전공
7
데이타베이스 생성 1
mysql> drop database sampledb;
Query OK, 3 rows affected (0.16 sec)
mysql> create database sampledb;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on sampledb.* to user5@localhost identified by '1234';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql
|
| sampledb
|
| test
|
+--------------------+
4 rows in set (0.00 sec)
금오공과대학 컴퓨터공학부 컴퓨터공학전공
8
데이타베이스 생성 2
mysql> select database();
+------------+
| database() |
+------------+
| NULL
|
+------------+
1 row in set (0.00 sec)
mysql> use sampledb;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| sampledb |
+------------+
1 row in set (0.00 sec)
mysql>
금오공과대학 컴퓨터공학부 컴퓨터공학전공
9
테이블 생성
mysql> create table enrol
-> (
-> sno char(10) not null,
-> cno char(10) not null,
-> grade char(2),
-> midterm integer,
-> final integer
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> show tables;
+--------------------+
| Tables_in_sampledb |
+--------------------+
| enrol
|
+--------------------+
1 row in set (0.00 sec)
mysql> desc enrol;
+---------+----------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| sno
| char(10) | NO |
| NULL
|
|
| cno
| char(10) | NO |
| NULL
|
|
| grade | char(2) | YES |
| NULL
|
|
| midterm | int(11) | YES |
| NULL
|
|
| final | int(11) | YES |
| NULL
|
|
+---------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> drop table enrol;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql>
금오공과대학 컴퓨터공학부 컴퓨터공학전공
10
실습 : 셸을 이용한 스키마 생성
금오공과대학 컴퓨터공학부 컴퓨터공학전공
11
실습: 셸을 이용한 데이터의 입력
금오공과대학 컴퓨터공학부 컴퓨터공학전공
12
실습: 셀을 이용한 SQL 문장의 실행
/* 교수 초기 데이터 파일 : profdata.sql */
insert into prof (pcode,pname,pdept,pphone)
values ('P001','김 구','컴퓨터공학과','0001');
insert into prof (pcode,pname,pdept,pphone)
values ('P002','안창호','컴퓨터공학과','0002');
insert into prof (pcode,pname,pdept,pphone)
values ('P003','이육사','국문학과','0003');
insert into prof (pcode,pname,pdept,pphone)
values ('P004','박종화','국문학과','0004');
insert into prof (pcode,pname,pdept,pphone)
values ('P005','심 훈','사학과','0005');
insert into prof (pcode,pname,pdept,pphone)
mysql> s o u r c e p r o f d a t a . s q l
values ('P006','한용운','사학과','0006');
ERROR 1062: 중복된 입력 값 'P001': key 1
Query OK, 1 row affected (0.06 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql>
금오공과대학 컴퓨터공학부 컴퓨터공학전공
13
스키마 생성 스크립트
use sampledb;
drop table enrol;
drop table course;
drop table student;
create table student (
sno char(10) not null,
sname char(10) not null,
year integer,
dept char(40),
primary key(sno)
);
create table course (
cno char(10) not null,
cname char(40) not null,
credit integer,
dept char(40),
prname char(10),
primary key(cno)
);
금오공과대학 컴퓨터공학부 컴퓨터공학전공
create table enrol (
sno char(10) not null,
cno char(10) not null,
grade char(2),
midterm integer,
final integer,
primary key(sno, cno),
foreign key(sno) references student(sno)
on delete cascade
on update cascade,
foreign key(cno) references course(cno)
on delete cascade
on update cascade
);
show tables;
desc course;
desc enrol;
desc student;
14
데이터 생성 스크립트
delete from student;
insert into student(sno, sname, year, dept) values
(100,'나수영', 4, '컴퓨터'),
(200,'이찬수', 3, '전기'),
(300,'정기태', 1, '컴퓨터'),
(400,'송병길', 4, '컴퓨터'),
(500,'박종화', 2, '산공');
select * from student;
delete from course;
insert into course values
('C123', '프로그래밍', 3, '컴퓨터', '김성국'),
('C312', '자료구조', 3, '컴퓨터', '황수관'),
('C324', '화일구조', 3, '컴퓨터', '이규찬'),
('C413', '데이타베이스', 3, '컴퓨터', '이일로'),
('E412', '반도체', 3, '전자', '홍봉진');
select * from course;
금오공과대학 컴퓨터공학부 컴퓨터공학전공
delete from enrol;
insert into enrol values
(100,'C413', 'A', 90, 95),
(100,'E412', 'A', 95, 95),
(200,'C123', 'B', 85, 80),
(300,'C312', 'A', 90, 95),
(300,'C324', 'C', 75, 75),
(300,'C413', 'A', 95, 90),
(400,'C312', 'A', 90, 95),
(400,'C324', 'A', 95, 90),
(400,'C413', 'B', 80, 85),
(400,'E412', 'C', 65, 75),
(500,'C312', 'B', 85, 80);
select * from enrol;
15
MySQL Engines
MySQL & Storage Engines
 MySQL supports several storage engines that act as
handlers for different table types
 In fact, the original term WAS table type
 Different storage engines offer different “properties” to
tables in the areas of





Transactional capabilities
Locking
Backup and recovery
Optimization
Pricing (licenses and support)
 A single database can support tables of any combination
of storage engine
금오공과대학 컴퓨터공학부 컴퓨터공학전공
17
SHOW ENGINES
 Displays status information about your server's storage
engines
mysql> show engines;
+------------+---------+---------------------------------------------------------------+
| Engine
| Support | Comment
|
+------------+---------+---------------------------------------------------------------+
| MyISAM
| YES
| Default engine as of MySQL 3.23 with great performance
|
| MEMORY
| YES
| Hash based, stored in memory, useful for temporary tables
|
| InnoDB
| DEFAULT | Supports transactions, row-level locking, and foreign keys
|
| BerkeleyDB | NO
| Supports transactions and page-level locking
|
| BLACKHOLE | NO
| /dev/null storage engine (anything you write to it disappears) |
| EXAMPLE
| NO
| Example storage engine
|
| ARCHIVE
| YES
| Archive storage engine
|
| CSV
| NO
| CSV storage engine
|
| ndbcluster | NO
| Clustered, fault-tolerant, memory-based tables
|
| FEDERATED | NO
| Federated MySQL storage engine
|
| MRG_MYISAM | YES
| Collection of identical MyISAM tables
|
| ISAM
| NO
| Obsolete storage engine
|
+------------+---------+---------------------------------------------------------------+
금오공과대학 컴퓨터공학부 컴퓨터공학전공
18
MyISAM (the default)
 Default engine in many MySQL installations (3.2x & 4)
 Pros
 Provides the best combination of performance and
functionality
 Cons
 It lacks transaction capabilities
 Uses table-level locking
 Does not support foreign keys
금오공과대학 컴퓨터공학부 컴퓨터공학전공
19
InnoDB
 Supports all of the database functionality of MyISAM engine
PLUS
 Full transaction capabilities (with full ACID (Atomicity,
Consistency, Isolation, and Durability) compliance)
 Row level locking of data
 Caching and indexing structure where both indexes and data
are cached in memory as well as being stored on disk
 Supports foreign key and referential integrity constraints
 LinuxPlanet’s Advice
 InnoDB를 추천
http://www.linuxplanet.com/linuxplanet/tutorials/6034/7/
금오공과대학 컴퓨터공학부 컴퓨터공학전공
20
CREATE INDEX
 Use to create a named one, two or many field (search)
index
 The index is created immediately
 The ordering of fields is significant
 Important: Any index may speed up retrieval, but always
slows down inserts, deletes and some updates
 It also increases overhead!
금오공과대학 컴퓨터공학부 컴퓨터공학전공
21
CREATE INDEX Examples
mysql> CREATE INDEX DName on
-> DepartmentCopy(Name);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> CREATE UNIQUE INDEX DMgr on
-> DepartmentCopy(Manager);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> CREATE INDEX DLoco on
-> DepartmentCopy(Location, Name);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
금오공과대학 컴퓨터공학부 컴퓨터공학전공
22
Showing Indexes (1 of 3)
mysql> show indexes from departmentcopy;
*************************** 1. row ******
Table: departmentcopy
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: DeptNum
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
……
금오공과대학 컴퓨터공학부 컴퓨터공학전공
23
Removing an Index
 To remove an index:
mysql> DROP INDEX DMgr on DepartmentCopy;
Query OK, 4 rows affected (0.17 sec)
Records: 4
Duplicates: 0
금오공과대학 컴퓨터공학부 컴퓨터공학전공
Warnings: 0
24
과제
 텍스트 북에 있는 모든 SQL 문장을 MySQL로 입력하고 결
과를 출력하여 제출할 것
 범위: 10.1, 10.2, 10.3
금오공과대학 컴퓨터공학부 컴퓨터공학전공
25
Download