03주차 ddl- table을 만들자

20
DDL – 구조를 만들자 웹 데이터 베이스 , [email protected] 한림대학교 금융정보통계학과

Upload: yoonwhan-lee

Post on 14-Jul-2015

653 views

Category:

Documents


3 download

TRANSCRIPT

DDL – 구조를 만들자

웹 데이터 베이스

이 윤 환, [email protected]한림대학교 금융정보통계학과

DataBase

• 자료를 저장하고 보여주고 관리하는 작업 공간

• 문자 저장 방식에 대한 이해 필요o http://dev.mysql.com/doc/refman/5.1/en/charset-general.html

o Character Set

• 심볼과 특정 코드로 인코딩 될 규칙들의 집합

• Ex) “A”, “B”, “a”, “b”

o “A” = 0, “B” = 1”, “a” = 2, “b” = 3

• “A”, “B”, “a”, “b” : 심볼

• 0, 1, 2, 3 : 코드

o Collation

• Character Set에서 문자들을 비교하기 위한 규칙들의 집합

• Ex) “A” = 0, “B” = 1

o 0 < 1 이므로 “A” 가 “ㅠ” 보다 작다

o 대소문자 비교는?

• binary collation : Case-sensitive

• 대소문자를 비교하지 않을 경우 : Case-insensitive

이 윤 환, [email protected]한림대학교 금융정보통계학과

DataBase

• MySQL에서 지원하는 Character Set

이 윤 환, [email protected]한림대학교 금융정보통계학과

DataBase

• MySQL에서 지원하는 Collation

이 윤 환, [email protected]한림대학교 금융정보통계학과

DataBase

• DB 생성문 : CREATE DATABASEo http://dev.mysql.com/doc/refman/5.1/en/create-database.html

o CREATE DATABASE [IF NOT EXISTS] db_name [create_specification]

• Create_specification

o [DEFAULT] CHARACTER SET [=] charset_name

o [DEFAULT] COLLATE [=] collation_name

이 윤 환, [email protected]한림대학교 금융정보통계학과

Ex)

- DB명을 test1DB로 하되 동일한 이름이 있을 경우에만

CHARACTER SET이 utf8이 되는 DB를 생성하시오

DataBase

• DB 제거문 : DROP DATABASEo http://dev.mysql.com/doc/refman/5.1/en/drop-database.html

o DROP DATABASE [IF EXISTS] db_name

이 윤 환, [email protected]한림대학교 금융정보통계학과

Ex)

- 방금 전에 작성한 test1DB가 있는지 점검하여 해당 db가 존재하면

제거하시오.

사용자

• 현대의 DBMS들은 별도의 사용

자를 갖는다.

• MySQL 설치시 시스템 사용자

인 „root‟를 갖는다.

• DBMS 사용자는 시스템 자원

(DB, table, index, view 등)별

로 주어진 권한을 부여 받아 해

당 권한에 맞게 DB에 접근할

수 있다.

o „root‟의 경우 일반 자원 접근 권

한과 서버 관리 권한을 모두 부여

받는다.

• http://dev.mysql.com/doc/

refman/5.1/en/privilege-

system.html

이 윤 환, [email protected]한림대학교 금융정보통계학과

Privilege Column Context

CREATE USER Create_user_priv server administration

PROCESS Process_priv server administration

RELOAD Reload_priv server administration

REPLICATION CLIENT Repl_client_priv server administration

REPLICATION SLAVE Repl_slave_priv server administration

SHOW DATABASES Show_db_priv server administration

SHUTDOWN Shutdown_priv server administration

SUPER Super_priv server administration

ALL [PRIVILEGES] server administration

USAGE server administration

관리자 권한

사용자

이 윤 환, [email protected]한림대학교 금융정보통계학과

Privilege Column Context

CREATE Create_priv databases, tables, or indexes

DROP Drop_priv databases, tables, or views

GRANT OPTION Grant_priv databases, tables, or stored ro

utines

REFERENCES References_priv databases or tables

EVENT Event_priv databases

ALTER Alter_priv tables

DELETE Delete_priv tables

INDEX Index_priv tables

INSERT Insert_priv tables or columns

SELECT Select_priv tables or columns

UPDATE Update_priv tables or columns

CREATE TEMPORA

RY TABLES

Create_tmp_table_priv tables

LOCK TABLES Lock_tables_priv tables

TRIGGER Trigger_priv tables

CREATE VIEW Create_view_priv views

SHOW VIEW Show_view_priv views

ALTER ROUTINE Alter_routine_priv stored routines

CREATE ROUTINE Create_routine_priv stored routines

EXECUTE Execute_priv stored routines

FILE File_priv file access on server host

일반 자원 접근 권한

사용자 : 추가

• 사용자 추가 : CREATE USERo http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

o http://dev.mysql.com/doc/refman/5.1/en/create-user.html

o CREATE USER user_specification

• User_specification

o „User_name‟@‟Host_name‟

o [IDENTIFIED BY [PASSWORD] „password‟]

이 윤 환, [email protected]한림대학교 금융정보통계학과

Ex)

- 클라이언트가 서버와 같은 호스트에서 접근할 수 있는 사용자

„test_user‟를 암호가 „test_pwd‟가 되도록 생성하시오.

사용자 : 추가

이 윤 환, [email protected]한림대학교 금융정보통계학과

사용자 : 삭제

• 사용자 삭제 : DROP USERo http://dev.mysql.com/doc/refman/5.1/en/drop-user.html

o DROP USER „user_name‟@‟host_name‟;

이 윤 환, [email protected]한림대학교 금융정보통계학과

Ex)

- 앞선 예에서 생성한 test_user2를 제거 하시오.

사용자 : 테이블

• 사용자 테이블은 mysql DB

에 존재

• 사용자의 정보와 기본 권한

에 대해 저장

• use mysql 후 desc user; 를입력해 보자.

이 윤 환, [email protected]한림대학교 금융정보통계학과

사용자 : 테이블

• test_user 사용자의 정보 출력

이 윤 환, [email protected]한림대학교 금융정보통계학과

사용자 : 권한 부여

• 사용자를 생성하였으니 이제 생성된 사용자에게 DB 자원에 대해 권

한을 부여해 보자.

• 앞서 설명한 권한들을 주는 과정이다.

• http://dev.mysql.com/doc/refman/5.1/en/grant.html

이 윤 환, [email protected]한림대학교 금융정보통계학과

GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_levelTO user_specification [, user_specification] ... [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}] [WITH with_option ...]

object_typeTABLE | FUNCTION | PROCEDURE

priv_level: * | *.* | db_name.* | db_name.tbl_name | tbl_namedb_name.routine_name

user_specificationuser [IDENTIFIED BY [PASSWORD] 'password']

사용자 : 권한부여

이 윤 환, [email protected]한림대학교 금융정보통계학과

Privilege Meaning Privilege Meaning

ALL [PRIVILEGES] Grant all privileges at specified access le

vel except GRANT OPTION

INSERT Enable use of INSERT

ALTER Enable use of ALTER TABLE LOCK TABLES Enable use of LOCK TABLES on tables for

which you have the SELECT privilege

ALTER ROUTINE Enable stored routines to be altered or d

ropped

PROCESS Enable the user to see all processes with S

HOW PROCESSLIST

CREATE Enable database and table creation REFERENCES Not implemented

CREATE ROUTINE Enable stored routine creation RELOAD Enable use of FLUSH operations

CREATE TEMPORARY

TABLES

Enable use of CREATE TEMPORARY T

ABLE

REPLICATION CLIE

NT

Enable the user to ask where master or sla

ve servers are

CREATE USER Enable use of CREATE USER, DROP U

SER, RENAME USER, and REVOKE A

LL PRIVILEGES

REPLICATION SLAV

E

Enable replication slaves to read binary log

events from the master

CREATE VIEW Enable views to be created or altered SELECT Enable use of SELECT

DELETE Enable use of DELETE SHOW DATABASES Enable SHOW DATABASES to show all da

tabases

DROP Enable databases, tables, and views to

be dropped

SHOW VIEW Enable use of SHOW CREATE VIEW

EVENT Enable use of events for the Event Sche

duler

SHUTDOWN Enable use of mysqladmin shutdown

EXECUTE Enable the user to execute stored routin

es

SUPER Enable use of other administrative operatio

ns such as CHANGE MASTER TO, KILL, P

URGE BINARY LOGS, SET GLOBAL, an

d mysqladmin debugcommand

FILE Enable the user to cause the server to re

ad or write files

TRIGGER Enable trigger operations

GRANT OPTION Enable privileges to be granted to or rem

oved from other accounts

UPDATE Enable use of UPDATE

INDEX Enable indexes to be created or dropped USAGE Synonym for “no privileges”

사용자 : 권한부여

이 윤 환, [email protected]한림대학교 금융정보통계학과

Ex)

- 앞서 생성한 사용자 „test_user‟이 만들어 둔 DB „test1DB‟과

하위의 모든 자원에 대해 모든 권한을 갖도록 하시오.

사용자 : 추가의 또 다른 방법

• GRANT를 이용하여 사용자를 추가할 수 있다.

• 먼저 „test2DB‟를 생성해 보자.

o mysql> CREATE DATABASE test2DB DEFAULT CHARACTER SET = utf8;

• GRANT 문을 이용하여 다음과 같이 입력해 보자.

o mysql> GRANT ALL ON test2DB.* TO 'test_user2'@'localhost' IDENTIFIED BY

'test_pwd';

• 이제 다음의 문장등을 통해 생성되었음을 확인해 보자.

o mysql> select * from user where User='test_user2';

o mysql> select * from db where db='test2db';

이 윤 환, [email protected]한림대학교 금융정보통계학과

MySQL 접속하기

• 윈도우 키 + r 키를 눌러 실행창을 띄우고 cmd라고 입력합니다.

• 콘솔 창이 뜨면 „mysql –u 사용자명 –p mysql „을 입력하고 Enter

그 후 Password 입력

이 윤 환, [email protected]한림대학교 금융정보통계학과

개인 활동

• DB를 만들어 봅니다.

o CHARATER SET이 utf8 이 되는 DB를 스스로 만들어 봅니다.

• 사용자를 추가합니다.

• 추가한 사용자에 대해 기존에 존재하는 DB의 모든 권한을 갖도록 해

봅니다.o SELECT 권한만 갖도록도 해 보세요.

• GRANT문을 이용하여 사용자를 추가함과 동시에 기존에 존재하는

DB에 모든 권한을 줘 봅니다.

이 윤 환, [email protected]한림대학교 금융정보통계학과

다음 시간에는 …

• SQL 에 대해 학습합니다.o 실제 자료가 저장되는 TABLE을 만들어 봅니다.

o MySQL의 자료형에 대해 이야기 합니다.

이 윤 환, [email protected]한림대학교 금융정보통계학과