Mysql 명령어

Download Report

Transcript Mysql 명령어

4장 Mysql 명령어
6
한빛미디어(주)
Section
01
학습 목표
데이터베이스 생성, 목록보기, 삭제하는 명령어를 익힌다
테이블 생성, 삭제, 변경 등의 명령어를 익힌다.
Mysql 명령어를 파일로 저장하여 일괄로 실행하는 방법을 익힌다
데이터 삽입, 삭제, 검색, 수정, 정렬하는 명령어를 익힌다
데이터베이스를 백업 및 복원하는 방법을 익힌다
2
Section
주요
학습01내용
01. 데이터베이스 관련 명령어
02. 테이블 관련 명령어
03. Mysql 명령 일괄 실행
04. 데이터 조작 명령어
05. 데이터베이스 백업 및 복원
3
Section 01
데이터베이스 관련 명령어
데이터베이스 접속
C:\mysql\bin> mysql -u계정 -p비밀번호 데이터베이스명
C:\mysql\bin> mysql -uphp5 -p1234 php5_db
데이터베이스 생성
mysql> create database 데이터베이스명;
mysql> create database sample1;
데이터베이스 목록 확인
mysql> show databases;
4
Section 01
데이터베이스 관련 명령어
데이터베이스 삭제
mysql> drop database 데이터베이스명;
mysql> drop database sample1;
mysql> show databases;
5
Section 02
01
DB 테이블 설계 예
필드명
타입
필드 설명
num
int
일렬번호
name
char(10)
이름
address
varchar(80) 또는 char(80)
주소
tel
char(20)
전화번호
[표 6-1] 주소록 DB 테이블 설계 (테이블 명 : friend)
6
Section 03
01
테이블 관련 명령어
DB 테이블 생성
mysql> create table 테이블명(
필드명1 타입,
필드명2 타입,
필드명3 타입,
...........
PRIMARY KEY(필드명)
);
mysql> create table friend(
num int NOT NULL,
name char(10),
address char(80),
tel char(20),
PRIMARY KEY(num)
);
7
Section 03
01
테이블 관련 명령어
DB 테이블 목록 보기
mysql> show tables;
DB 테이블 구조 보기
mysql> desc 테이블명;
mysql> desc friend;
새로운 필드 추가
mysql> alter table 테이블명 add 새로운필드명 타입
[first 또는 after 필드명] ;
mysql> alter table friend add age int;
8
Section 03
01
테이블 관련 명령어
“address” 필드 다음에 “email char(30)” 필드 추가
mysql> alter table friend add email char(30) after address;
필드 삭제
mysql> alter table 테이블명 drop 삭제할필드명1, 삭제할필드명2;
mysql> alter table friend drop email;
mysql> alter table friend drop age;
필드 수정
mysql> alter table 테이블명 change 이전필드명 새로운필드명 타입;
mysql> alter table friend change tel phone int;
9
Section 03
01
테이블 관련 명령어
필드 타입 수정
mysql> alter table 테이블명 modify 필드명 새로운타입;
mysql> alter table friend modify name int;
테이블 이름 수정
mysql> alter table 이전테이블명 rename 새테이블명;
mysql> alter table friend rename student;
테이블 삭제
mysql> drop table friend;
10
Section 04
01
Mysql 명령 일괄 실행
메모장으로 다음의 내용을 입력하여 ”c:\mysql\bin“폴더 안에
”friend.sql“ 이름으로 저장
create table friend(
num int NOT NULL,
name char(10),
address char(80),
tel char(20),
PRIMARY KEY(num)
);
명령 프롬프트에서 다음의 명령 실행
C:\mysql\bin> mysql -uphp5 -p1234 php5_db < friend.sql
11
Section 04
01
Mysql 명령 일괄 실행
DB에 접속하여 테이블 존재 확인
C:\mysql\bin> mysql -uphp5 -p1234 php5_db
mysql> show tables;
mysql> desc friend;
12
Section 05
01
데이터 조작 명령어
데이터 삽입하기 (insert)
mysql> insert into 테이블명 (필드명1, 필드명2, ....)
values (필드값1, 필드값2, ...);
mysql> insert into friend (num, name, address, tel)
-> values (1, ‘배성진‘, ’서울 동작구 노량진동‘, ’234-7693‘);
mysql> insert into friend values
-> (2, ‘성지연‘, ’대전시 유성구 송강동‘, ’978-3875‘);
삽입된 데이터 확인
mysql> select * from friend;
13
Section 06
01
테이블 생성과 데이터 입력
교재 뒤에 있는 CD에서 ”mem.sql" 이란 파일을 찾아
“C:\mysql\bin” 폴더로 복사
명령 프롬프트에서 다음을 실행
C:\mysql\bin> mysql -uphp5 -p1234 php5_db < mem.sql
생성된 테이블과 데이터 확인
mysql> desc mem;
mysql> select * from mem;
14
Section 07
01
mem 테이블 구조
필드명
타입
필드 설명
num
int
일렬번호
id
varchar(10)
아이디
name
varchar(10)
이름
sex
char(1)
성별(남성:'M', 여성:'W')
post_num
varchar(8)
우편번호
address
varchar(80)
주소
tel
char(20)
전화번호
age
int
나이
15
Section 08
01
데이터 조작 명령어
데이터 검색하기 (select)
mysql> select 필드명1, 필드명2, … from 테이블명;
mem 테이블의 “id”, “name”, “address” 필드 검색
mysql> select id, name, address from mem;
mem 테이블의 전체 레코드, 전체 필드 검색
mysql> select * from mem;
조건에 맞는 데이터 검색하기 (select ~ where)
mysql> select 필드명1, 필드명2, … from 테이블명 where 조건식;
16
Section 08
01
데이터 조작 명령어
여성의 아이디, 이름, 주소, 전화번호, 성별 보기
mysql> select id, name, address, tel, sex from mem where sex='W';
50세 이상인 레코드의 전체 필드 보기
mysql> select * from mem where age>=50;
20대의 이름, 아이디, 주소, 우편번호 보기
mysql> select name, id, address, post_num from mem where
-> age>=20 and age<30;
"김진모“의 이름, 아이디, 주소, 우편번호, 나이 보기
mysql> select name, id, address, post_num, age from mem where
-> name='김진모‘;
17
Section 08
01
데이터 조작 명령어
40대 남성의 이름, 주소, 나이 보기
mysql> select name, address, age from mem where
-> (age>=40 and age<50) and sex='M‘;
20대 또는 40대 여성의 이름, 아이디, 주소, 전화번호, 나이 보기
mysql> select name, id, address, tel, age from mem where
-> ( (age>=20 and age<30) or (age>=40 and age<50) )
-> and sex='W‘;
김씨 성을 가진 사람의 이름, 주소, 전화번호 보기
mysql> select name, address, tel from mem where name like '김%’;
18
Section 08
01
데이터 조작 명령어
서울 사는 사람의 모든 필드 보기
mysql> select * from mem where address like '서울%’;
부산 사는 여성의 이름, 주소, 성별 보기
mysql> select name, address, sex from mem where
-> address like '부산%' and sex='W' ;
가운데 이름에 ‘용’인 사람의 이름, 아이디 보기
mysql> select name, id from mem where name like '__용%' ;
광주에 사는 김씨성 가진 사람의 이름, 주소, 전화번호 보기
mysql> select name, address, tel from mem where
-> address like '광주%' and name like '김%';
19
Section 08
01
데이터 조작 명령어
검색된 데이터 정렬 (order by)
mysql> select 필드명1, 필드명2 from 테이블명 order by 필드명;
mem 테이블의 레코드를 나이 순으로 정렬하여 age, id, name, sex,
tel 필드
mysql> select age, id, name, sex, tel from mem order by age;
내림차순 정렬
mysql> select age, id, name, sex, tel from mem order by age desc;
서울 사는 사람에 대해 나이가 많은 순서
mysql> select age, name, address from mem where address like '서울%‘
-> order by age desc;
20
Section 08
01
데이터 조작 명령어
데이터 수정하기(update)
mysql> update 테이블명 set 필드명=필드값 [where 조건식]
“yjhwang”의 전화번호를 “123-4567” 로 변경
mysql> update mem set tel='123-4567' where id='yjhwang‘;
mysql> select id, name, tel from mem where id='yjhwang‘;
‘신수진’의 나이가 47세인데 27세로 변경
mysql> update mem set age=27 where name='신수진‘;
mysql> select name, age from mem where name='신수진‘;
21
Section 08
01
데이터 조작 명령어
데이터 삭제하기 (delete from)
mysql> delete from 테이블명 [where 조건식]
mysql> delete from mem where name='김길수‘;
나이가 30 ~ 50 세 사이의 레코드를 삭제
mysql> delete from mem where age>=30 and age<=50;
mysql> select name, address, age from mem;
모든 레코드를 삭제
mysql> delete from mem;
22
Section 09
01
데이터베이스 백업 및 복원
데이터베이스 백업
C:\mysql\bin> mysqldump -u계정 -p비밀번호 데이터베이스 이름 >
백업파일명
C:\mysql\bin> mysqldump -uphp5 -p1234 php5_db >
php5_db.sql
백업 파일 복원
C:\mysql\bin> mysql -u계정 -p비밀번호 데이터베이스 이름 <
백업파일명
C:\mysql\bin> mysql -utest -p1234 test_db < php5_db.sql
23