Skip to main content

MYSQL v1.0 </doc>

webstoryboy.co.kr

MySQL은 데이터베이스 소프트웨어입니다. 일반적으로 데이터를 추가하거나 검색, 추출하는 기능을 모두 포함해서 데이터베이스라고 부릅니다.

MySQL은 세계에서 가장 많이 쓰이는 오픈 소스의 관계형 데이터베이스 관리 시스템(RDBMS)입니다.
MySQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스로 개발된 무료 프로그램입니다.
그래서 홈페이지나 쇼핑물(워드프레스, cafe24, 제로보드, 그누보드) 등 일반적으로 웹 개발에 널리 사용하고 있습니다.

MAMP 설치

설치 이미지
MAMP 사이트
이동 : cd \MAMP\bin\mysql\bin
C:\MAMP\bin\mysql\bin
로그인 : mysql -uroot -proot
C:\MAMP\bin\mysql\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 4
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.

mysql>
구문 : create database DB이름;
CREATE DATABASE sample01;
Query OK, 1 row affected (0.01 sec)
구문 : show databases;
SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample01           |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
구문 : use DB이름;
USE sample01;
Database changed
구문 : drop database DB이름;
DROP DATABASE sample01;
Query OK, 0 rows affected (0.03 sec)
구문 : create table 테이블 이름;
CREATE TABLE myMember (
    memberID 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,
    youAge int(20) NOT NULL,
    youPhone int(20) NOT NULL,
    regTime int(11) NOT NULL,
    PRIMARY KEY (memberID)
) charset=utf8;
Query OK, 0 rows affected (0.04 sec)
구문 : show tables;
SHOW TABLES;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| mymember           |
+--------------------+
1 row in set (0.00 sec)
구문 : desc 테이블 이름;
DESC myMember;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| youEmail   | varchar(40)      | NO   |     | NULL    |                |
| youName    | varchar(12)      | NO   |     | NULL    |                |
| youPass    | varchar(20)      | NO   |     | NULL    |                |
| youBirth   | varchar(12)      | NO   |     | NULL    |                |
| regTime    | int(11)          | NO   |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
구문 : drop tables 테이블 이름;
DROP TABLE myMember2;
Query OK, 0 rows affected (0.01 sec)

테이블 구조, 데이터 복사

구문 : create table 테이블명 select * from 복사할 테이블명;
create table mymember1 select * from mymember;
Query OK, 17 rows affected (0.05 sec)
Records: 17  Duplicates: 0  Warnings: 0

테이블 구조 복사

구문 : create table 테이블명 like 복사할 테이블명;
create table mymember2 like mymember;
Query OK, 0 rows affected (0.05 sec)

테이블 데이터 복사

구문 : insert into 테이블명 select * from 복사할 테이블명;
insert into mymember3 select * from mymember;
Query OK, 0 rows affected (0.05 sec)
구문 : ALTER TABLE 테이블명 ADD 추가할 필드명 AFTER 위치;
ALTER TABLE myMember ADD youGender enum('m', 'w', 'x') default 'x' comment "남성은 m, 여성은 w" AFTER youBirth;
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0
desc myMember;
+------------+-------------------+------+-----+---------+----------------+
| Field      | Type              | Null | Key | Default | Extra          |
+------------+-------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned  | NO   | PRI | NULL    | auto_increment |
| youEmail   | varchar(40)       | NO   |     | NULL    |                |
| youName    | varchar(12)       | NO   |     | NULL    |                |
| youPass    | varchar(20)       | NO   |     | NULL    |                |
| youBirth   | varchar(12)       | NO   |     | NULL    |                |
| youGender  | enum('m','w','x') | YES  |     | x       |                |
| regTime    | int(11)           | NO   |     | NULL    |                |
+------------+-------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
ALTER TABLE 테이블명 MODIFY 필드명 변경내용;
ALTER TABLE mymember2 MODIFY youName int(20);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
구문 : ALTER TABLE 테이블명 DROP 삭제할 필드명;
ALTER TABLE mymember2 DROP youpass;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
구문 : INSERT INTO 테이블명(필드명) VALUES(데이터)
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('webstoryboy@naver.com','황상연','12dfsdecv','19920405','33','01039858583','578365839');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('qhd1994@naver.com', '유희봉', '123asdd**7', '19940728', '29', '01075790168', '20220314');


INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('webstoryboy@naver.com','황상연','12dfsdecv','19920405','33','01039858583','578365839');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('jungsik321@naver.com', '김정식', '1234', '19930209', '30', '01025738580', '20220314');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('ik6623@naver.com','박종호','1234qwer','19950909','28','01040976623','10293857');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('rmsgur2275@gmail.com','박근혁','alcls2860','19980611','25','01058122860', '12345678');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('apdlvmf8044@gmail.com', '송준섭', 'qweasdf', '19931120', '30', '01088648044', '21846351');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('sbxjs6999@naver.com','박근희','1234qwer','19970214','28',01041046999,'12345678');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('seongwoo@lycos.com','박성우','123456asd','19910506','32', '01044779156','54216451');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('dnwls7738@naver.com','천우진', '12asdf', '19960324', '27', '01055805274', '45678941');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('dnstjq6305@naver.com', '조운섭', 'qweasdf', '19911110', '32', '01024068904', '12345678');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('lhw1847@gmail.com','이현우','1234','19971221','26', '01055332287', '0303');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('dhtpfls321@naver.com', '오세린', '1234', '19980214', '25', '01032229944', '12345678');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('GarenChoi@naver.com','최근영','1234','19970117','26','01054651345','234');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('sna12345@naver.com', '배석찬','1234','19960727','27','01033125279','03270327');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('sbxjs6999@naver.com','박근희','1234qwer','19970214','26',01041046999,'12345678');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('wwuu123@naver.com','최가영','12345678','19970506','26','01066179991','12345678');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('illie@naver.com', '류성일', 'ryuPass', '19940426', '29', '01055327391', '1440');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('jsjh442@naver.com','박지선','1234','19980423','25','01057421662','12341234');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('ohwehoh@naver.com','이연우','dldus1358','19950720','28','01014141441','12341234');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('go_goa@naver.com' ,'김현우', '159753', '19920126', '31', '01087361836', '40250371');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('placidcy@gmail.com', '김지섭', '1234', '19900212', '32', '01011112222','20220314');
Query OK, 1 row affected (0.01 sec)

전체 데이터 불러오기

구문 : SELECT * FROM 테이블명;
SELECT * FROM myMember;
+----------+-----------------------+---------+------------+----------+--------+------------+-----------+
| memberID | youEmail              | youName | youPass    | youBirth | youAge | youPhone   | regTime   |
+----------+-----------------------+---------+------------+----------+--------+------------+-----------+
|        1 | qhd1994@naver.com     | 유희봉  | 123asdd**7 | 19940728 |     29 | 1075790168 |  20220314 |
|        2 | webstoryboy@naver.com | 황상연  | 12dfsdecv  | 19920405 |     33 | 1039858583 | 578365839 |
|        3 | placidcy@gmail.com    | 김지섭  | 1234       | 19900212 |     32 | 1011112222 |  20220314 |
|        4 | go_goa@naver.com      | 김현우  | 159753     | 19920126 |     31 | 1087361836 |  40250371 |
|        5 | ohwehoh@naver.com     | 이연우  | dldus1358  | 19950720 |     28 | 1014141441 |  12341234 |
|        6 | jsjh442@naver.com     | 박지선  | 1234       | 19980423 |     25 | 1057421662 |  12341234 |
|        7 | illie@naver.com       | 류성일  | ryuPass    | 19940426 |     29 | 1055327391 |      1440 |
|        8 | wwuu123@naver.com     | 최가영  | 12345678   | 19970506 |     26 | 1066179991 |  12345678 |
|        9 | dhtpfls321@naver.com  | 오세린  | 1234       | 19980214 |     25 | 1032229944 |  12345678 |
|       10 | 96tmdqh@naver.com     | 한승보  | 1234       | 19960716 |     27 | 1020153932 |      1234 |
|       11 | dnstjq6305@naver.com  | 조운섭  | qweasdf    | 19911110 |     32 | 1024068904 |  12345678 |
|       12 | dnwls7738@naver.com   | 천우진  | 12asdf     | 19960324 |     27 | 1055805274 |  45678941 |
|       13 | seongwoo@lycos.com    | 박성우  | 123456asd  | 19910506 |     32 | 1044779156 |  54216451 |
|       14 | sbxjs6999@naver.com   | 박근희  | 1234qwer   | 19970214 |     28 | 1041046999 |  12345678 |
|       15 | apdlvmf8044@gmail.com | 송준섭  | qweasdf    | 19931120 |     30 | 1088648044 |  21846351 |
|       16 | rmsgur2275@gmail.com  | 박근혁  | alcls2860  | 19980611 |     25 | 1058122860 |  12345678 |
|       17 | ik6623@naver.com      | 박종호  | 1234qwer   | 19950909 |     28 | 1040976623 |  10293857 |
|       18 | sbxjs6999@naver.com   | 박근희  | 1234qwer   | 19970214 |     26 | 1041046999 |  12345678 |
|       19 | sna12345@naver.com    | 배석찬  | 1234       | 19960727 |     27 | 1033125279 |   3270327 |
|       20 | GarenChoi@naver.com   | 최근영  | 1234       | 19970117 |     26 | 1054651345 |       234 |
+----------+-----------------------+---------+------------+----------+--------+------------+-----------+
20 rows in set (0.00 sec)

조건 데이터 블러오기

구문 : SELECT * FROM 테이블명 WHERE 조건;

회원 아이디가 10번인 경우

SELECT * FROM myMember WHERE memberID=10;

회원 아이디가 10번이 아닌 경우

SELECT * FROM myMember WHERE memberID <> 10;

회원 아이디가 5~10번인 경우

SELECT * FROM myMember WHERE memberID >= 5 AND memberID < 11;

범위 조건 데이터 불러오기

구문 : SELECT * FROM 테이블명 WHERE 컬러명 BETWEEN 시작 값 AND 끝 값;

회원 아이디가 5~10번인 경우

SELECT * FROM myMember WHERE memberID BETWEEN 5 AND 10;

회원 아이디가 5~10번이 아닌 경우

SELECT * FROM myMember WHERE memberID NOT BETWEEN 5 AND 10;

해당 조건 데이터 불러오기

구문 : SELECT * FROM 테이블명 WHERE 컬러명 IN(4, 6, 13, 14);
SELECT * FROM myMember WHERE memberID IN(4, 6, 13, 14);

패턴 조건 데이터 불러오기

구문 : SELECT * FROM 테이블명 WHERE LIKE 조건;

이메일 중에 naver 텍스트가 포함하는 경우

SELECT * FROM myMember WHERE youEmail LIKE '%naver%';

이메일 중에 web 텍스트로 시작하는 경우

SELECT * FROM myMember WHERE youEmail LIKE 'web%';

이메일 중에 com 텍스트로 끝나는 경우

SELECT * FROM myMember WHERE youEmail LIKE '%com';

이름 중에 김 텍스트로 시작하는 경우

SELECT * FROM myMember WHERE youName LIKE '김%';

이름 중에 김or황 텍스트로 시작하는 경우

SELECT * FROM myMember WHERE youName LIKE '김%' OR youName LIKE '황%';

이름 중에 중간 글자가 '근'인 경우의 맴버를 출력

SELECT * FROM myMember WHERE youName LIKE '_근_';

NULL 데이터 불러오기

구문 : SELECT * FROM 테이블명 IS NULL;
SELECT * FROM myMember WHERE youName IS NULL;
SELECT * FROM myMember WHERE youName IS NOT NULL;

논리 연산자 데이터 불러오기

구문 : SELECT * FROM 테이블명 WHERE 컬럼명 연산자;

나이가 30살 이상이고 또는(OR) 김씨인 맴버

SELECT * FROM myMember WHERE youName LIKE '김%' OR youAge >= 30;

나이가 30살 이상이면서(AND) 김씨인 맴버

SELECT * FROM myMember WHERE youName LIKE '김%' AND youAge >= 30;

데이터 갯수 불러오기

구문 : SELECT * FROM 테이블명 LIMIT 표시할 수;
SELECT * FROM myMember LIMIT 12;
SELECT youEmail FROM myMember LIMIT 5, 10;

내림차순/오름차순(DESC/ASC)

구문 : SELECT * FROM 테이블명 ORDER BY 컬럼명 DESC;
SELECT * FROM myMember ORDER BY youAge DESC;
SELECT * FROM myMember ORDER BY youAge ASC;
UPDATE 테이블명 SET 컬럼명 조건

모든 회원의 비밀번호를 '1234' 변경

UPDATE myMember SET youPass = '1234';

아이디 번호가 3번인 회원의 비밀번호를 '1234' 변경

UPDATE myMember SET youPass = '1234' WHERE boardID = 3;

5회원부터 10회원까지의 비밀번호를 '1234567' 변경

UPDATE myMember SET youPass = '1234567' WHERE memberID BETWEEN 5 AND 10;

11회원부터 10개의 회원까지의 비밀번호를 '****' 변경

UPDATE myMember SET youPass = '****' ORDER BY memberID DESC LIMIT 10;

전체 데이터 삭제

구문 : DELETE FROM 테이블명; or DELETE * FROM 테이블명;
DELETE FROM myMember;

조건 데이터 삭제

구문 : DELETE FROM 테이블명 조건;

조건 삭제 : 회원 아이디가 5번 멤버를 삭제

DELETE FROM myMember WHERE memberID=5;

조건 삭제 : 패스워드가 '****' 멤버를 삭제

DELETE FROM myMember WHERE youPass='****';

정렬 삭제 : 이름은 기준으로 5번째까지 멤버를 삭제

DELETE FROM myMember ORDER BY youName LIMIT 5;

지정 삭제 : 지정한 멤버를 삭제

DELETE FROM myMember WHERE memberID IN(2,8,10);
CREATE TABLE myMember (
    memberID 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,
    youAge int(20) NOT NULL,
    youPhone int(20) NOT NULL,
    regTime int(11) NOT NULL,
    PRIMARY KEY (memberID)
) charset=utf8;

CREATE TABLE myReview (
    reviwID int(10) unsigned auto_increment,
    memberID int(10) unsigned,
    youCont tinytext NOT NULL,
    regTime int(11) NOT NULL,
    PRIMARY KEY (reviwID)
) charset=utf8;

INSERT INTO myReview (memberID, youCont, regTime) VALUES ('4', '리뷰입니다.', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('6', '추천 추천', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('14', '사지마세요.', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('11', '비추에요', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('5', '이거 왜 팜.', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('1', '안녕하세요.', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('19', '또 사러옴', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('2', 'ㅇㅇ', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('17', '리뷰 당첨', '12345');
INSERT INTO myReview (memberID, youCont, regTime) VALUES ('4', '배고파요', '12345');
구문 : SELECT 필드명 FROM 테이블명 엘리어스 JOIN 연결할 테이블명 엘리어스 ON(조건문);
SELECT m.youName, m.youEmail, r.youCont, r.regTime FROM myMember m JOIN myReview r ON(m.memberID = r.memberID);

필드명 바꾸기

구문 : SELECT 필드명 AS 기존 필드명 출력할 필드명 FROM 테이블명;
SELECT m.youName, m.youEmail, r.youCont, r.regTime AS reviwTime FROM myMember m JOIN myReview r ON(m.memberID = r.memberID);

필드 내용이 없는 경우 표시

구문 : LEFT JOIN
SELECT m.youName, m.youEmail, r.youCont, r.regTime AS reviwTime FROM myMember m LEFT JOIN myReview r ON(m.memberID = r.memberID);
CREATE TABLE myMember (
    memberID 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,
    youAge int(20) NOT NULL,
    youPhone int(20) NOT NULL,
    regTime int(11) NOT NULL,
    PRIMARY KEY (memberID)
) charset=utf8;

CREATE TABLE myRecode (
    recodeID int(10) unsigned auto_increment,
    memberID int(10) unsigned,
    javascript tinyint NOT NULL,
    html tinyint NOT NULL,
    css tinyint NOT NULL,
    mysql tinyint NOT NULL,
    react tinyint NOT NULL,
    PRIMARY KEY (recodeID)
) charset=utf8;

INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('4', '50', '50', '50', '80', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('14', '50', '50', '70', '50', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('2', '50', '100', '50', '50', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('6', '50', '70', '50', '70', '30');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('8', '50', '50', '50', '50', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('18', '50', '50', '50', '70', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('20', '50', '50', '50', '30', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('11', '80', '50', '50', '90', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('12', '50', '50', '10', '50', '50');
INSERT INTO myRecode (memberID, javascript, html, css, mysql, react) VALUES ('19', '50', '70', '50', '50', '10');
SELECT COUNT(javascript) FROM myRecode;
SELECT SUM(javascript) FROM myRecode;
SELECT AVG(javascript) FROM myRecode;
SELECT MAX(javascript) FROM myRecode;
SELECT MIN(javascript) FROM myRecode;

GROUP BY

구문 : SELECT 필드명 FROM 테이블명 GROUP BY 필드명;
SELECT memberID AVG(javascript) FROM myRecode WHERE memberID IN(1, 2, 3, 4) GROUP BY memberID;