MYSQL
MYSQL은 데이터베이스 소프트웨어입니다.
일반적으로 데이터를 추가하거나 검색, 추출하는 기능을 모두 포함해서
데이터 베이스라고 부릅니다.
MYSQL은 세계에서 가장 많이 쓰이는 오픈 소스의 관계형 데이터베이스 관리시스템(RDBMS) 입니다.
MYSQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스로 개발된 무료 프로그램 입니다.
그래서 홈페이지나 쇼핑몰(워드프레스, 카페24, 제로보드, 그누보드)등 일반적으로 웹 개발에 널리 사용하고 있습니다.
MYSQL 설치
MAMP란 웹사이트를 개발할 때 쓰이는 기술 스택인 macOS, Apache, MYSQL, PHP의 약어이자 솔루션 스택이다.
https://www.mamp.info/en/downloads/
MYSQL 실행
윈도우 : cd MAMP/bin/mysql/bin
로그인 : mysql -uroot -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
webstoryboyhwang@Webstoryboyui-MacBookPro bin % ./mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 188
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
데이터베이스
데이터베이스 만들기
형식 : create database 데이터베이스 이름
mysql> create database sample01;
Query OK, 1 row affected (0.00 sec)
데이터베이스 보기
형식 : show databases;
>mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample01 |
| sample02 |
| sys |
+--------------------+
6 rows in set (0.00 sec)
데이터베이스 사용
형식 : use database;
use sample01
Database changed
데이터베이스 삭제
형식 : drop database 데이터베이스 이름;
mysql> drop database sample02;
Query OK, 0 rows affected (0.00 sec)
테이블
테이블 만들기
형식 : create table 테이블이름;
create table member (
myMemberID int(10) unsigned auto_increment,
youEmail varchar(40) NOT NULL,
youName varchar(20) NOT NULL,
youPass varchar(20) NOT NULL,
youBirth int(20) NOT NULL,
regTime int(20) NOT NULL,
PRIMARY KEY (myMemberID)
) charset=utf8;
테이블 목록보기
형식 : show tables;
mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| member |
+--------------------+
1 row in set (0.00 sec)
테이블 보기
형식 : desc 테이블 이름;
mysql> desc member;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youEmail | varchar(40) | NO | | NULL | |
| youName | varchar(20) | NO | | NULL | |
| youPass | varchar(20) | NO | | NULL | |
| youBirth | int(20) | NO | | NULL | |
| regTime | int(20) | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
테이블 삭제
형식 : drop table 테이블 이름;
mysql> drop table member;
Query OK, 0 rows affected (0.01 sec)
테이블 데이터
테이블 입력하기
형식 : INSERT INTO 테이블이름(필드명) VALUES(데이터);
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('thdtjdg*l415@naver.com', '송성희', '1234', '19970415', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('web*to*yboy@naver.com','황*연','1234','199*0303','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('thd*jd*ml415@naver.com', '송*희', '1234', '19970415', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('mo0*47*naver.com', '김*정', '1234', '19970*30', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('tjr*ns*kdtj@naver.com', '김*준', '1234', '1*9410*9', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('jwo*12*@naver.com', '정*비', '1234', '1999*303', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('lee*h3*32@naver.com','이*환','1234','1997*205','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('ssh*n4*82@naver.com','박*신','1234','19990303','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('to_*ef*re@naver.com', '김*준', '1234', '199*0809', '04');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('kkb*52*@naver.com','권*비','1234','199*0*03','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('pio*m1*3@gmail.com', '문*내', '1234', '1999*303', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('ghk*db*ls96@gmail.com', '황*진', '1234', '19*90*03', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('eod*js*605@naver.com','김*원','1234','1*96*530','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('wjs*hd*s971007@gmail.com','전*연','1234','19*71*07','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('noe*he*h@gmail.com','권*현','1234','19*6*331','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('hjk*ng*06@gmail.com','강*지','1234','1999*303','1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('sun*ey*810@gmail.com', '박*혜', '1234', '19*81010', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUE('kde6*03*@gmail.com', '김*언', '1234', '1*70*101', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('pra*se*109@gmail.com', '이*은', '1234', '19*9*303', '1234567');
테이블 불러오기
형식 : SELECT 필드명 FROM 테이블명 WHERE 조건
mysql> select * from member;
+------------+--------------------------+---------+---------+----------+----------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+--------------------------+---------+---------+----------+----------+
| 1 | *h*t*dgml415@naver.com | 송*희 | 1234 | 199**4*5 | 1234567 |
| 2 | *e*s*oryboy@naver.com | 황*연 | 1234 | 199**3*3 | 1234567 |
| 3 | *o*6*7@naver.com | 김*정 | 1234 | 199**5*0 | 1234567 |
| 4 | *j*w*srkdtj@naver.com | 김*준 | 1234 | 199**0*9 | 1234567 |
| 5 | *w*r*24@naver.com | 정* 비 | 1234 | 199**3*3 | 1234567 |
| 6 | *e*s*3432@naver.com | 이*환 | 1234 | 199**2*5 | 1234567 |
| 7 | *s*i*4882@naver.com | 박*신 | 1234 | 199**3*3 | 1234567 |
| 8 | *o*b*fore@naver.com | 김*준 | 1234 | 199**8*9 | 4 |
| 9 | *k*7*28@naver.com | 권*비 | 1234 | 199**3*3 | 1234567 |
| 10 | *o*n*s9605@naver.com | 김*원 | 1234 | 199**5*0 | 1234567 |
| 11 | *j*q*dus971007@gmail.com | 전*연 | 1234 | 199**0*7 | 1234567 |
| 12 | *o*y*eyh@gmail.com | 권*현 | 1234 | 199**3*1 | 1234567 |
| 13 | *j*a*g306@gmail.com | 강*지 | 1234 | 199**3*3 | 1234567 |
| 14 | *u*h*y9810@gmail.com | 박*혜 | 1234 | 199**0*0 | 1234567 |
| 15 | *r*i*e1109@gmail.com | 이*은 | 1234 | 199**3*3 | 1234567 |
| 16 | *s*i*4882@naver.com | 박*신 | 1234 | 199**3*3 | 1234567 |
| 17 | *r*n*sy@naver.com | 최*주 | 3950 | 200**4*5 | 1234567 |
| 18 | *d*k*dd4882@naver.com | 여*요 | 1234 | 229**3*3 | 12324567 |
| 19 | *k*5*93@naver.com | 김*현 | 1234 | 199**6*7 | 1234567 |
| 20 | *i*l*3743@gmail.com | 김*형 | 1234 | 200**0*9 | 1234567 |
| 21 | *a*p*ham52@gmail.com | 이*호 | 1234 | 199**7*1 | 1234567 |
+------------+--------------------------+---------+---------+----------+----------+
myMemberID가 6번인 경우
mysql> SELECT * FROM member WHERE myMemberID = 6;
+------------+---------------------+---------+---------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+---------------------+---------+---------+----------+---------+
| 6 | leesh3432@naver.com | 이*환 | 1234 | 199*0*0* | 1234567 |
+------------+---------------------+---------+---------+----------+---------+
1 row in set (0.01 sec)
myMemberID가 6번이 아닌 경우
SELECT * FROM member WHERE myMemberID != 6;
SELECT * FROM member WHERE myMemberID <> 6; //둘다 가능
myMemberID가 5~10 인 경우
SELECT * FROM member WHERE myMemberID >=5 AND myMemberID <=10;
myMemberID가 5~10 인 경우(범위 조건)
SELECT * FROM member WHERE myMemberID BETWEEN 5 AND 10;
myMemberID가 5~10 아닌 경우(범위 조건)
SELECT * FROM member WHERE myMemberID NOT BETWEEN 5 AND 10;
myMemberID(해당 조건 불러오기)
SELECT * FROM member WHERE myMemberID IN(1,4,10,14);
email중에 naver 텍스트를 포함하고 있는 경우
mysql> SELECT * FROM member WHERE youEmail LIKE '%naver%';
email중에 web 텍스트로 시작하는 경우
SELECT * FROM member WHERE youEmail LIKE 'web%'
email중에 com 텍스트로 끝나는 경우
SELECT * FROM member WHERE youEmail LIKE '%com'
email중에 naver 텍스트를 포함하고 있는 경우
형식 : SELECT 필드명 FROM 테이블명 WHERE 조건
mysql> SELECT * FROM member WHERE youEmail LIKE '%naver%';
+------------+------------------------+---------+---------+----------+----------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+------------------------+---------+---------+----------+----------+
| 1 | *h*t*dgml415@naver.com | 송*희 | 1234 | 199*0*1* | 1234567 |
| 2 | *e*s*oryboy@naver.com | 황*연 | 1234 | 199*0*0* | 1234567 |
| 3 | *o*6*7@naver.com | 김*정 | 1234 | 199*0*3* | 1234567 |
| 4 | *j*w*srkdtj@naver.com | 김*준 | 1234 | 199*1*0* | 1234567 |
| 5 | *w*r*24@naver.com | 정* 비 | 1234 | 199*0*0* | 1234567 |
| 6 | *e*s*3432@naver.com | 이*환 | 1234 | 199*0*0* | 1234567 |
| 7 | *s*i*4882@naver.com | 박*신 | 1234 | 199*0*0* | 1234567 |
| 8 | *o*b*fore@naver.com | 김*준 | 1234 | 199*0*0* | 4 |
| 9 | *k*7*28@naver.com | 권*비 | 1234 | 199*0*0* | 1234567 |
| 10 | *o*n*s9605@naver.com | 김*원 | 1234 | 199*0*3* | 1234567 |
| 16 | *s*i*4882@naver.com | 박*신 | 1234 | 199*0*0* | 1234567 |
| 17 | *r*n*sy@naver.com | 최*주 | 3950 | 200*0*1* | 1234567 |
| 18 | *d*k*dd4882@naver.com | 여*요 | 1234 | 229*0*0* | 12324567 |
| 19 | *k*5*93@naver.com | 김*현 | 1234 | 199*0*1* | 1234567 |
+------------+------------------------+---------+---------+----------+----------+
14 rows in set (0.00 sec)
테이블 수정하기
UPDATE 테이블(필드명) SET 컬럼명 조건
모든 회원의 핸드폰 번호 변경
UPDATE member SET youPhone = "01000000000"
특정 범위 테이블 내용 변경
update member set youAge = 40 where myMemberID IN(7,8,9);
필드 수정하기
UPDATE 테이블(필드명) SET 컬럼명 조건
ALTER TABLE 테이블명 MODIFY 수정할 필드명
ALTER TABLE member MODIFY youAge int(10);
ALTER TABLE member MODIFY youGender enum('m','w') int(10);
필드 삭제하기
ARTER TABLE member DROP youGender;