728x90
MYSQL
MySQL은 데이터베이스 소프트웨어입니다. 일반적으로 데이터를 추가하거나 검색, 추출하는 기능을 포함해서 데이터베이스라고 부릅니다.
MySQL은 세계에서 가장 많이 쓰이는 오픈 소스 관계형 데이터베이스 관리시스템(RDBMS)입니다. MySQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스를 개발된 무료 프로그램입니다. 그래서 홈페이지나 쇼핑몰(워드프레스, cafe24, 제로보드, 그누보드) 등 일반적으로 웹 개발에 널리 사용하고 있습니다.
MYSQL 설치
MAMP란 웹사이트를 개발할 때 쓰이는 기술 스택인 maxOS, Apache, MySQL, PHP 의 약어이자 솔루션 스택이다.
https://www.mamp.info/en/downloads/
https://www.mamp.info/en/downloads/
MYSQL 실행
윈도우
윈도우 : cs MAMP/bin/mysql/bin
로그인 : mysql -uroot -proot
윈도우 : cs MAMP/bin/mysql/bin
로그인 : mysql -uroot -proot
//윈도우
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.
맥
윈도우 : cs Applicantion/MAMP/Libary/bin
로그인 : ./mysql -uroot -proot
윈도우 : cs Applicantion/MAMP/Libary/bin
로그인 : ./mysql -uroot -proot
//맥
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 sample02;
Query OK, 1 row affected (0.00 sec)
데이터베이스 보기
show database;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample01 |
| sample02 |
| sys |
+--------------------+
6 rows in set (0.00 sec)
데이터베이스 사용
use 데이터베이스 이름;
mysql> use sample01;
Database changed
데이터베이스 삭제
drop database 데이터베이스 이름;
mysql> drop database sample02;
Query OK, 0 rows affected (0.02 sec)
mysql> create database sample02;
Query OK, 1 row affected (0.00 sec)
show database;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample01 |
| sample02 |
| sys |
+--------------------+
6 rows in set (0.00 sec)
데이터베이스 사용
use 데이터베이스 이름;
mysql> use sample01;
Database changed
데이터베이스 삭제
drop database 데이터베이스 이름;
mysql> drop database sample02;
Query OK, 0 rows affected (0.02 sec)
mysql> use sample01;
Database changed
drop database 데이터베이스 이름;
mysql> drop database sample02;
Query OK, 0 rows affected (0.02 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,
youAge 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)
테이블 복사
CREATE TABLE 새로운 테이블명 SELECT * FROM 복사할 테이블 명
CREATE TABLE member3 SELECT * FROM member;
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,
youAge 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)
테이블 복사
CREATE TABLE 새로운 테이블명 SELECT * FROM 복사할 테이블 명
CREATE TABLE member3 SELECT * FROM member;
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)
테이블 복사
CREATE TABLE 새로운 테이블명 SELECT * FROM 복사할 테이블 명
CREATE TABLE member3 SELECT * FROM member;
CREATE TABLE member3 SELECT * FROM member;
테이블 데이터
테이블 입력하기
INSERT INTO 테이블이름(필드명) VALUE(데이터)
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('piowm123@gmail.com', '문병내', '1234', '19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('webstoryboy@naver.com','황상연','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('thdtjdgml415@naver.com', '송성희', '1234', '19970415', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('mo0647@naver.com', '김민정', '1234', '19970530', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('tjrwnsrkdtj@naver.com', '김석준', '1234', '19941009', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('jwor124@naver.com', '정은비', '1234', '19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('leesh3432@naver.com','이영환','1234','19970205', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('sshin4882@naver.com','박현신','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('to_before@naver.com', '김상준', '1234', '19970809', '20', '04');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('kkb7528@naver.com','권규비','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('ghkddbwls96@gmail.com', '황유진', '1234', '19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('eodnjs9605@naver.com','김대원','1234','19960530', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('wjsqhdus971007@gmail.com','전보연','1234','19971007', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('noeyheyh@gmail.com','권혜현','1234','19960331', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('hjkang306@gmail.com','강현지','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('sunhey9810@gmail.com', '박선혜', '1234', '19981010', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUE('kde66034@gmail.com', '김동언', '1234', '19700101', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('praise1109@gmail.com', '이하은', '1234', '19990303', '20', '1234567');
테이블 불러오기
SELECT 필드명 FROM 테이블명 WHERE 조건
전체 데이터 불러오기
mysql> SELECT * FROM member;
+------------+--------------------------+---------+---------+----------+--------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | regTime |
+------------+--------------------------+---------+---------+----------+--------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 1234567 |
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+---------+
17 rows in set (0.00 sec)
myMemberID가 6번인 경우
mysql> SELECT * FROM member WHERE myMemberID = 6;
+------------+-------------------+---------+---------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+-------------------+---------+---------+----------+---------+
| 6 | jwor124@naver.com | 정은 비 | 1234 | 19990303 | 1234567 |
+------------+-------------------+---------+---------+----------+---------+
1 row in set (0.00 sec)
myMemberID가 6번이 아닌 경우
SELECT * FROM member WHERE myMemberID <>6;
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 0 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 0 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 0 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 0 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 0 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
16 rows in set (0.00 sec)
//SELECT * FROM member WHERE myMemberID != 6; 으로도 가능
myMemberID가 5~10번인 경우
SELECT * FROM member WHERE myMemberID >=5 AND myMemberID <=10;
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
6 rows in set (0.00 sec)
myMemberID가 5~10번인 경우(범위 조건)
SELECT * FROM member WHERE myMemberID BETWEEN 5 AND 10;
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
6 rows in set (0.00 sec)
myMemberID가 5~10번이 아닌 경우
SELECT * FROM member WHERE myMemberID NOT BETWEEN 5 AND 10;
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 0 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 0 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 0 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 0 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 0 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
11 rows in set (0.00 sec)
myMemberID (해당 조건 불러오기)
SELECT * FROM member WHERE myMemberID IN (1, 4, 10, 14);
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
4 rows in set (0.00 sec)
email 중에 naver 텍스트를 포함하고 있는 경우
mysql> SELECT * FROM member WHERE youEmail LIKE '%naver%'; //LIKE는 포함된 문자열을 찾는거고 %는 문자열 쓰는 법
+------------+------------------------+---------+---------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+------------------------+---------+---------+----------+---------+
| 2 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 1234567 |
| 3 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 1234567 |
| 4 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 1234567 |
| 5 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 1234567 |
| 6 | jwor124@naver.com | 정은 비 | 1234 | 19990303 | 1234567 |
| 7 | kkk5993@naver.com | 김성현 | 1234 | 19960617 | 1234567 |
| 8 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 1234567 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 1234567 |
| 18 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 1234567 |
| 19 | oranssy@naver.com | 최진주 | 3950 | 20010415 | 1234567 |
+------------+------------------------+---------+---------+----------+---------+
11 rows in set (0.00 sec)
email 중에 web 텍스트로 시작하는 경우
SELECT * FROM member WHERE youEmail LIKE 'web%';
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
1 row in set (0.00 sec)
email 중에 com 텍스트로 끝나는 경우
SELECT * FROM member WHERE youEmail LIKE '%com';
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 0 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 0 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 0 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 0 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 0 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
17 rows in set (0.00 sec)
이름 중에 '김'으로 시작하는 멤버 출력
SELECT * FROM member WHERE youName LIKE '김%';
이름 중에 '김'씨 이거나 '황'씨인 경우
SELECT * FROM member WHERE youName LIKE '김%' OR youName LIKE '황%';
이름 중간에 '상'이 있는 멤버 출력
SELECT * FROM member WHERE youName LIKE '_상_';
또는 연으로 끝나는 경우
SELECT * FROM member WHERE youName LIKE '__연';
이름 중에 데이터가 없는 경우 출력
SELECT * FROM member WHERE youName IS NULL;
반대의 경우
SELECT * FROM member WHERE youName IS NOT NULL;
멤버 중에 나이가 18세 이상이면서 김씨인 멤버를 출력
SELECT * FROM member WHERE youAge >= 18 AND youName LIKE '김%';
멤버 중에 나이가 18세 이상이거나 김씨인 멤버를 출력
SELECT * FROM member WHERE youAge >= 18 OR youName LIKE '김%';
멤버 중에 나이가 20세 이상이면서 아이디가 2 또는 10인 멤버
SELECT * FROM member WHERE (myMemberID = 2 OR myMemberID = 10) AND youAge >= 20;
회원 데이터 5개만 불러오기(1~5)
SELECT * FROM member LIMIT 5;
회원 데이터 5개만 불러오기(6~10)
SELECT * FROM member LIMIT 5, 5;
회원 이메일을 기준으로 5개만 불러오기(1~5)
SELECT youMail FROM member LIMIT 5;
테이블 수정하기
UPDATE 테이블명(필드명) SET 컬럼명 조건
모든 회원의 핸드폰 번호 변경
UPDATE member SET youPhone = "010-0000-0000";
아이디가 3번인 회원의 비밀번호를 '0123'
UPDATE member SET youPass = "0123" WHERE myMemberID = 3;
회원 아이디 5~10번까지 비밀번호를 "1234567"
UPDATE member SET youPass = "1234567" WHERE myMemberID BETWEEN 5 AND 10;
비밀번호를 "1234567"로 변경하고 내림차순으로 10개만 변경하기
UPDATE member SET youPass = "1234567" ORDER BY youPass DESC LIMIT 10;
테이블 삭제하기
DELETE FROM 테이블명 조건
회원 아이디가 5번인 멤버 삭제
DELETE FROM member WHERE myMemberID = 5
회원 비밀번호가 '1234'인 멤버 삭제
DELETE FROM member WHERE youPass = '1234';
회원 이름을 기준으로 5번째까지 멤버 삭제
DELETE FROM member ORDER BY youName LIMIT 5;
지정한 멤버 삭제
DELETE FROM member WHERE myMemberID IN (7, 8, 9);
INSERT INTO member(youEmail, youName, youPass, youBirth, regTime) VALUES('piowm123@gmail.com', '문병내', '1234', '19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('webstoryboy@naver.com','황상연','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('thdtjdgml415@naver.com', '송성희', '1234', '19970415', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('mo0647@naver.com', '김민정', '1234', '19970530', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('tjrwnsrkdtj@naver.com', '김석준', '1234', '19941009', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('jwor124@naver.com', '정은비', '1234', '19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('leesh3432@naver.com','이영환','1234','19970205', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('sshin4882@naver.com','박현신','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('to_before@naver.com', '김상준', '1234', '19970809', '20', '04');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('kkb7528@naver.com','권규비','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('ghkddbwls96@gmail.com', '황유진', '1234', '19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('eodnjs9605@naver.com','김대원','1234','19960530', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('wjsqhdus971007@gmail.com','전보연','1234','19971007', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('noeyheyh@gmail.com','권혜현','1234','19960331', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('hjkang306@gmail.com','강현지','1234','19990303', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('sunhey9810@gmail.com', '박선혜', '1234', '19981010', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUE('kde66034@gmail.com', '김동언', '1234', '19700101', '20', '1234567');
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('praise1109@gmail.com', '이하은', '1234', '19990303', '20', '1234567');
SELECT 필드명 FROM 테이블명 WHERE 조건
전체 데이터 불러오기
mysql> SELECT * FROM member;
+------------+--------------------------+---------+---------+----------+--------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | regTime |
+------------+--------------------------+---------+---------+----------+--------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 1234567 |
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+---------+
17 rows in set (0.00 sec)
myMemberID가 6번인 경우
mysql> SELECT * FROM member WHERE myMemberID = 6;
+------------+-------------------+---------+---------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+-------------------+---------+---------+----------+---------+
| 6 | jwor124@naver.com | 정은 비 | 1234 | 19990303 | 1234567 |
+------------+-------------------+---------+---------+----------+---------+
1 row in set (0.00 sec)
myMemberID가 6번이 아닌 경우
SELECT * FROM member WHERE myMemberID <>6;
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 0 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 0 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 0 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 0 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 0 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
16 rows in set (0.00 sec)
//SELECT * FROM member WHERE myMemberID != 6; 으로도 가능
myMemberID가 5~10번인 경우
SELECT * FROM member WHERE myMemberID >=5 AND myMemberID <=10;
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
6 rows in set (0.00 sec)
myMemberID가 5~10번인 경우(범위 조건)
SELECT * FROM member WHERE myMemberID BETWEEN 5 AND 10;
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
6 rows in set (0.00 sec)
myMemberID가 5~10번이 아닌 경우
SELECT * FROM member WHERE myMemberID NOT BETWEEN 5 AND 10;
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 0 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 0 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 0 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 0 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 0 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
11 rows in set (0.00 sec)
myMemberID (해당 조건 불러오기)
SELECT * FROM member WHERE myMemberID IN (1, 4, 10, 14);
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
4 rows in set (0.00 sec)
email 중에 naver 텍스트를 포함하고 있는 경우
mysql> SELECT * FROM member WHERE youEmail LIKE '%naver%'; //LIKE는 포함된 문자열을 찾는거고 %는 문자열 쓰는 법
+------------+------------------------+---------+---------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | regTime |
+------------+------------------------+---------+---------+----------+---------+
| 2 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 1234567 |
| 3 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 1234567 |
| 4 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 1234567 |
| 5 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 1234567 |
| 6 | jwor124@naver.com | 정은 비 | 1234 | 19990303 | 1234567 |
| 7 | kkk5993@naver.com | 김성현 | 1234 | 19960617 | 1234567 |
| 8 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 1234567 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 1234567 |
| 18 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 1234567 |
| 19 | oranssy@naver.com | 최진주 | 3950 | 20010415 | 1234567 |
+------------+------------------------+---------+---------+----------+---------+
11 rows in set (0.00 sec)
email 중에 web 텍스트로 시작하는 경우
SELECT * FROM member WHERE youEmail LIKE 'web%';
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+-----------------------+---------+---------+----------+--------+----------+---------+
1 row in set (0.00 sec)
email 중에 com 텍스트로 끝나는 경우
SELECT * FROM member WHERE youEmail LIKE '%com';
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| myMemberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
| 1 | webstoryboy@naver.com | 황상연 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 2 | thdtjdgml415@naver.com | 송성희 | 1234 | 19970415 | 20 | 0 | 1234567 |
| 3 | mo0647@naver.com | 김민정 | 1234 | 19970530 | 20 | 0 | 1234567 |
| 4 | tjrwnsrkdtj@naver.com | 김석준 | 1234 | 19941009 | 20 | 0 | 1234567 |
| 5 | jwor124@naver.com | 정은비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 6 | leesh3432@naver.com | 이영환 | 1234 | 19970205 | 20 | 0 | 1234567 |
| 7 | sshin4882@naver.com | 박현신 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 8 | to_before@naver.com | 김상준 | 1234 | 19970809 | 20 | 0 | 4 |
| 9 | kkb7528@naver.com | 권규비 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 10 | ghkddbwls96@gmail.com | 황유진 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 11 | eodnjs9605@naver.com | 김대원 | 1234 | 19960530 | 20 | 0 | 1234567 |
| 12 | wjsqhdus971007@gmail.com | 전보연 | 1234 | 19971007 | 20 | 0 | 1234567 |
| 13 | noeyheyh@gmail.com | 권혜현 | 1234 | 19960331 | 20 | 0 | 1234567 |
| 14 | hjkang306@gmail.com | 강현지 | 1234 | 19990303 | 20 | 0 | 1234567 |
| 15 | sunhey9810@gmail.com | 박선혜 | 1234 | 19981010 | 20 | 0 | 1234567 |
| 16 | kde66034@gmail.com | 김동언 | 1234 | 19700101 | 20 | 0 | 1234567 |
| 17 | praise1109@gmail.com | 이하은 | 1234 | 19990303 | 20 | 0 | 1234567 |
+------------+--------------------------+---------+---------+----------+--------+----------+---------+
17 rows in set (0.00 sec)
이름 중에 '김'으로 시작하는 멤버 출력
SELECT * FROM member WHERE youName LIKE '김%';
이름 중에 '김'씨 이거나 '황'씨인 경우
SELECT * FROM member WHERE youName LIKE '김%' OR youName LIKE '황%';
이름 중간에 '상'이 있는 멤버 출력
SELECT * FROM member WHERE youName LIKE '_상_';
또는 연으로 끝나는 경우
SELECT * FROM member WHERE youName LIKE '__연';
이름 중에 데이터가 없는 경우 출력
SELECT * FROM member WHERE youName IS NULL;
반대의 경우
SELECT * FROM member WHERE youName IS NOT NULL;
멤버 중에 나이가 18세 이상이면서 김씨인 멤버를 출력
SELECT * FROM member WHERE youAge >= 18 AND youName LIKE '김%';
멤버 중에 나이가 18세 이상이거나 김씨인 멤버를 출력
SELECT * FROM member WHERE youAge >= 18 OR youName LIKE '김%';
멤버 중에 나이가 20세 이상이면서 아이디가 2 또는 10인 멤버
SELECT * FROM member WHERE (myMemberID = 2 OR myMemberID = 10) AND youAge >= 20;
회원 데이터 5개만 불러오기(1~5)
SELECT * FROM member LIMIT 5;
회원 데이터 5개만 불러오기(6~10)
SELECT * FROM member LIMIT 5, 5;
회원 이메일을 기준으로 5개만 불러오기(1~5)
SELECT youMail FROM member LIMIT 5;
테이블 수정하기
UPDATE 테이블명(필드명) SET 컬럼명 조건
모든 회원의 핸드폰 번호 변경
UPDATE member SET youPhone = "010-0000-0000";
아이디가 3번인 회원의 비밀번호를 '0123'
UPDATE member SET youPass = "0123" WHERE myMemberID = 3;
회원 아이디 5~10번까지 비밀번호를 "1234567"
UPDATE member SET youPass = "1234567" WHERE myMemberID BETWEEN 5 AND 10;
비밀번호를 "1234567"로 변경하고 내림차순으로 10개만 변경하기
UPDATE member SET youPass = "1234567" ORDER BY youPass DESC LIMIT 10;
테이블 삭제하기
DELETE FROM 테이블명 조건
회원 아이디가 5번인 멤버 삭제
DELETE FROM member WHERE myMemberID = 5
회원 비밀번호가 '1234'인 멤버 삭제
DELETE FROM member WHERE youPass = '1234';
회원 이름을 기준으로 5번째까지 멤버 삭제
DELETE FROM member ORDER BY youName LIMIT 5;
지정한 멤버 삭제
DELETE FROM member WHERE myMemberID IN (7, 8, 9);
UPDATE member SET youPhone = "010-0000-0000";
UPDATE member SET youPass = "0123" WHERE myMemberID = 3;
UPDATE member SET youPass = "1234567" WHERE myMemberID BETWEEN 5 AND 10;
UPDATE member SET youPass = "1234567" ORDER BY youPass DESC LIMIT 10;
DELETE FROM 테이블명 조건
회원 아이디가 5번인 멤버 삭제
DELETE FROM member WHERE myMemberID = 5
회원 비밀번호가 '1234'인 멤버 삭제
DELETE FROM member WHERE youPass = '1234';
회원 이름을 기준으로 5번째까지 멤버 삭제
DELETE FROM member ORDER BY youName LIMIT 5;
지정한 멤버 삭제
DELETE FROM member WHERE myMemberID IN (7, 8, 9);
테이블 수정하기
필드 추가하기
ALTER TABLE 테이블명 ADD 추가할 필드명 AFTER 필드 위치;
member 테이블 나이 뒤에 핸드폰 번호 필드를 추가
ALTER TABLE member ADD youPhone int(20) NOT NULL AFTER youAge;
member 테이블 핸드폰 번호 뒤에 성별 필드를 추가
ALTER TABLE member ADD youGender enum('m', 'w', 'x') default 'x' comment "남자 m, 여자 w" AFTER youPhone;
필드 수정하기
ALTER TABLE 테이블명 MODIFY 수정할 필드명;
ALTER TABLE member MODIFY youAge int(10);
필드 삭제하기
ALTER TABLE 테이블명 DROP 삭제할 필드명;
ALTER TABLE member DROP youGender;
ALTER TABLE member ADD youPhone int(20) NOT NULL AFTER youAge;
ALTER TABLE member ADD youGender enum('m', 'w', 'x') default 'x' comment "남자 m, 여자 w" AFTER youPhone;
ALTER TABLE 테이블명 MODIFY 수정할 필드명;
ALTER TABLE member MODIFY youAge int(10);
필드 삭제하기
ALTER TABLE 테이블명 DROP 삭제할 필드명;
ALTER TABLE member DROP youGender;
ALTER TABLE member DROP youGender;
테이블 합치기
필드 JOIN
SELECT 필드명 FROM 테이블 명 앨리어스 JOIN 연결할 테이블 명 앨리어스 ON (조건문);
create table myMember (
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,
youAge int(20) NOT NULL,
youPhone varchar(20) NOT NULL,
regTime int(20) NOT NULL,
PRIMARY KEY (myMemberID)
) charset=utf8;
create table myReview (
myReviewID int(10) unsigned auto_increment,
myMemberID int(10) unsigned,
youText tinytext NOT NULL,
regTime int(20) NOT NULL,
PRIMARY KEY (myReviewID)
) charset=utf8;
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('piowm123@gmail.com', '문병내', '1234', '19990303', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('webstoryboy@naver.com','황상연','1234','19990303', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('thdtjdgml415@naver.com', '송성희', '1234', '19970415', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('mo0647@naver.com', '김민정', '1234', '19970530', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('tjrwnsrkdtj@naver.com', '김석준', '1234', '19941009', '20', '인트로', '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("1", "정말 감사합니다. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("2", "정말 잘봤습니다. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("3", "정말 잘봤어요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("4", "정말 좋네요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("5", "정말 도움이 됐어요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("6", "정말 그렇습니다. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("7", "정말 그래요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("8", "정말 네. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("9", "정말 그래. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("10", "정말 정말. 너무 좋네요!", '1234567');
SELECT m.youName, m.youEmail, r.youText, r.regTime FROM myMember m JOIN myReview r ON(m.myMemberID = r.myMemberID) ORDER BY myReviewID DESC LIMIT 5;
create table myMember (
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,
youAge int(20) NOT NULL,
youPhone varchar(20) NOT NULL,
regTime int(20) NOT NULL,
PRIMARY KEY (myMemberID)
) charset=utf8;
create table myReview (
myReviewID int(10) unsigned auto_increment,
myMemberID int(10) unsigned,
youText tinytext NOT NULL,
regTime int(20) NOT NULL,
PRIMARY KEY (myReviewID)
) charset=utf8;
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('piowm123@gmail.com', '문병내', '1234', '19990303', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('webstoryboy@naver.com','황상연','1234','19990303', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('thdtjdgml415@naver.com', '송성희', '1234', '19970415', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('mo0647@naver.com', '김민정', '1234', '19970530', '20', '인트로', '1234567');
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('tjrwnsrkdtj@naver.com', '김석준', '1234', '19941009', '20', '인트로', '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("1", "정말 감사합니다. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("2", "정말 잘봤습니다. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("3", "정말 잘봤어요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("4", "정말 좋네요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("5", "정말 도움이 됐어요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("6", "정말 그렇습니다. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("7", "정말 그래요. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("8", "정말 네. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("9", "정말 그래. 너무 좋네요!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES("10", "정말 정말. 너무 좋네요!", '1234567');
SELECT m.youName, m.youEmail, r.youText, r.regTime FROM myMember m JOIN myReview r ON(m.myMemberID = r.myMemberID) ORDER BY myReviewID DESC LIMIT 5;
SQL 속성 정리
SQL에서 사용되는 속성입니다.
- UNSIGNED : 칼럼내에서 음수를 포함하지 않거나 혹은 수의 range를 양수쪽으로 더 넓게 가지고싶을때 사용
-
- AUTO_INCREMENT : 테이블 자동 증가
자동증가 속성이 부여된 열은 새 행이 추가될 때마다 값이 1씩 자동으로 증가합니다.
자동 증가 값은 테이블 행의 삭제와 무관하게 새 행이 추가되면 값이 계속 증가합니다. - int( ) : 숫자값. 괄호안에 숫자를 넣으면 값의 자리 범위 설정
- varchar( ) : 문자값. 괄호안에 숫자를 넣으면 값의 자리 범위 설정(가변 길이)
- PRIMARY KEY : 기본값. 절대로 중독될 수 없는 값을 정함
- enum : 정해진 특정한 값만 선택해 적을 수 있도록 하는 타입
- DEFAULT : 데이터 입력시 값을 빠트릴 때 정해진 기본값을 사용함
- ALTER TABLE : 테이블을 수정할 때 사용하는 공식
- ORDER BY : 데이터를 정렬할 때 사용(ex. 오름차순, 내림차순)
- SELECT LIMIT : 위에서부터 지정한 갯수만큼 선택 (LIMIT의 앞 숫자는 불러올 갯수, 뒷 숫자는 몇번부터 불러올지 선택 LIMIT 5, 5는 6~10번을 불러옴)
- LIKE : 문자열의 패턴을 검색 (%와 _을 사용할 수 있음)
- TINYTEXT : 최대 255 글자의 범위
댓글