MySQL은 데이터베이스 소프트웨어입니다. 일반적으로 데이터를 추가하거나 검색, 추츨하는 기능을 모두 포함해서 데이터베이스라고 부릅니다.
MySQL은 세계에서 가장 많이 쓰이는 오픈 소스의 관계형 데이터베이스 관리 시스템(RDBMS)입니다. MySQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스로 개발된 무료 프로그램입니다. 그래서 홈페이지나 쇼핑몰(워드프로세스, cafe24, 제로보드, 그누보드)등 일반적으로 웹 개발에 널리 사용하고 있습니다.
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.
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 sample01;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample01 |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use sample01;
Database changed
mysql> drop database sample01;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
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 comment "회원 정보 테이블";
CREATE TABLE myReview (
reviewID int(10) unsigned auto_increment,
memberID int(10) unsigned,
youCont tinytext NOT NULL,
regTime int(11) NOT NULL,
PRIMARY KEY (reviewID)
) charset=utf8;
INSERT INTO myreview(memberID, youCont, regTime) VALUES('4', '정말 감사합니다. 너무 좋네요!', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('7', '이거 정말 사야돼!!', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('1', '이거 가방 오나전 별루예요.. 사진빨', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('9', '이거 좋다! 좋다! 좋다!', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('12', '마라탕 먹고싶다.', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('3', '오늘은 정말 마라탕 먹고싶다.', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('16', '가성비 짱입니다. 당장 사세요.', '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('14', '빨리빨리빨리.', '12345');
show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| mymember |
+--------------------+
1 row in set (0.00 sec)
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.00 sec)
create table mymember2 (
-> myMemberID int(10) NOT NULL comment "회원 아이디");
Query OK, 0 rows affected (0.08 sec)
mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| mymember |
| mymember2 |
+--------------------+
2 rows in set (0.00 sec)
mysql> desc mymember2;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| myMemberID | int(10) | NO | | NULL | |
+------------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> drop table mymember2;
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| mymember |
+--------------------+
1 row in set (0.00 sec)
create table myMember1 select * from myMember;
Query OK, 20 rows affected (0.05 sec)
Records: 20 Duplicates: 0 Warnings: 0
select * from myMember1;
+----------+-----------------------+---------+-----------+----------+--------+------------+----------+
| memberID | youEmail | youName | youPass | youBirth | youAge | youPhone | regTime |
+----------+-----------------------+---------+-----------+----------+--------+------------+----------+
| 1 | ohwehoh@naver.com | 이연우 | dldus1358 | 19950720 | 28 | 1014141441 | 12341234 |
| 2 | go_goa@naver.com | 김현우 | 159753 | 19920126 | 31 | 1087361836 | 40250371 |
| 3 | jsjh442@naver.com | 박지선 | 1234 | 19980423 | 25 | 1057421662 | 12341234 |
| 4 | illie@naver.com | 류성일 | ryuPass | 19940426 | 29 | 1055327391 | 1440 |
| 5 | wwuu123@naver.com | 최가영 | 12345678 | 19970506 | 26 | 1066179991 | 12345678 |
| 6 | sbxjs6999@naver.com | 박근희 | 1234qwer | 19970214 | 26 | 1041046999 | 12345678 |
| 7 | sna12345@naver.com | 배석찬 | 1234 | 19960727 | 27 | 1033125279 | 3270327 |
| 8 | GarenChoi@naver.com | 최근영 | 1234 | 19970117 | 26 | 1054651345 | 234 |
| 9 | dhtpfls321@naver.com | 오세린 | 1234 | 19980214 | 25 | 1032229944 | 12345678 |
| 10 | dnstjq6305@naver.com | 조운섭 | qweasdf | 19911110 | 32 | 1024068904 | 12345678 |
| 11 | dnwls7738@naver.com | 천우진 | 12asdf | 19960324 | 27 | 1055805274 | 45678941 |
| 12 | seongwoo@lycos.com | 박성우 | 123456asd | 19910506 | 32 | 1044779156 | 54216451 |
| 13 | apdlvmf8044@gmail.com | 송준섭 | qweasdf | 19931120 | 30 | 1088648044 | 21846351 |
| 14 | rmsgur2275@gmail.com | 박근혁 | alcls2860 | 19980611 | 25 | 1058122860 | 12345678 |
| 15 | ik6623@naver.com | 박종호 | 1234qwer | 19950909 | 28 | 1040976623 | 10293857 |
| 16 | jungsik321@naver.com | 김정식 | 1234 | 19930209 | 30 | 1025738580 | 20220314 |
| 17 | 96tmdqh@naver.com | 한승보 | 1234 | 19960716 | 27 | 1020153932 | 1234 |
| 18 | placidcy@gmail.com | 김지섭 | 1234 | 19900212 | 32 | 1011112222 | 20220314 |
| 19 | go_goa@naver.com | 김현우 | 159753 | 19920126 | 31 | 1087361836 | 40250371 |
| 20 | ik6623@naver.com | 박종호 | 1234qwer | 19950909 | 28 | 1040976623 | 10293857 |
+----------+-----------------------+---------+-----------+----------+--------+------------+----------+
20 rows in set (0.00 sec)
ALTER TABLE myMember ADD youGender enum('m', 'w', 'x') default 'x' comment "남성은 m, 여성은 w" AFTER youBirth;
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)
mysql> desc mymember2;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youName | varchar(12) | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> ALTER TABLE mymember2 MODIFY youName int(20);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc mymember2;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youName | int(20) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> ALTER TABLE mymember2 drop youGender;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc mymember2;
+------------+------------------+------+-----+---------+----------------+
| 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 | |
| youAddress | varchar(30) | NO | | NULL | |
| youAge | int(2) | NO | | NULL | |
| regTime | int(11) | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhone, regTime) VALUES('ohwehoh@naver.com','이연우','dldus1358','19950720','28','01014141441','12341234');
mysql> select * from myMember;
mysql> SELECT * FROM myMember WHERE memberID = 10;
mysql> SELECT * FROM myMember WHERE memberID <> 10;
mysql> SELECT * FROM myMember WHERE memberID >= 5 AND <=10;
mysql> SELECT * FROM myMember WHERE memberID BETWEEN 5 AND 10;
mysql> SELECT * FROM myMember WHERE memberID NOT BETWEEN 5 AND 10;
mysql> SELECT * FROM myMember WHERE memberID IN(4, 6, 13, 14);
mysql> select * from mymember where youEmail like '%naver%';
mysql> select * from mymember where youEmail like 'web%';
mysql> select * from mymember where youEmail like '%com';
mysql> select * from mymember where youName like '김%';
mysql> select * from mymember where youName like '김%' OR youName like '황%';
mysql> select * from mymember where youName like '_근_';
mysql> select * from mymember where youName like '__연';
mysql> select * from mymember where youName IS NULL;
mysql> select * from mymember where youName IS NOT NULL;
mysql> select * from mymember where youAge >= 30 or youName like '김__';
mysql> select * from mymember where youAge >= 20 and youName like '김__';
mysql> select * from mymember where (memberID=2 OR memberID=11) AND youAge >= 30;
mysql> select * from mymember LIMIT 12;
mysql> select * from mymember LIMIT 10, 20;
mysql> select youEmail from mymember LIMIT 10, 20;
mysql> select * from mymember ORDER by youAge;
mysql > UPDATE mymember SET youPass = '1234';
mysql > UPDATE mymember SET youPass = '1234' WHERE boardID = 3;
mysql > UPDATE mymember SET youPass = '1234567' where memberID BETWEEN 5 and 10;
mysql > UPDATE mymember SET youPass = '****' where DESC LIMIT 10;
DELETE FROM mymember;
DELETE FROM mymember WHERE memberID = 5;
DELETE FROM mymember WHERE myPass LIKE '****';
DELETE FROM mymember ORDER BY youName LIMIT 5;
delete from mymember where memberID in (8,9,10,11);
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 comment "회원 정보 테이블";
CREATE TABLE myReview (
reviewID int(10) unsigned auto_increment,
memberID int(10) unsigned,
youCont tinytext NOT NULL,
regTime int(11) NOT NULL,
PRIMARY KEY (reviewID)
) charset=utf8;
INSERT INTO myreview(memberID, youCont, regTime) VALUES('4', '정말 감사합니다. 너무 좋네요!', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('7', '이거 정말 사야돼!!', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('1', '이거 가방 오나전 별루예요.. 사진빨', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('9', '이거 좋다! 좋다! 좋다!', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('12', '마라탕 먹고싶다.', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('3', '오늘은 정말 마라탕 먹고싶다.', '12345');
INSERT INTO myreview(memberID, youCont, regTime) VALUES('16', '가성비 짱입니다. 당장 사세요.', '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('14', '빨리빨리빨리.', '12345');
SELECT m.youName, m.youEmail, r.youCont, r.regTime FROM mymember m JOIN myReview r ON(m.memberID = r.memberID);
SELECT m.youName, m.youEmail, r.youCont, r.regTime AS reviewTime FROM mymember m JOIN myReview r ON(m.memberID = r.memberID);
SELECT m.youName, m.youEmail, r.youCont, r.regTime FROM mymember m LEFT JOIN myReview r ON(m.memberID = r.memberID);
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(1, 50, 30, 10, 30, 10);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(2, 60, 30, 30, 10, 20);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(3, 50, 36, 50, 30, 30);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(4, 80, 30, 70, 20, 60);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(5, 50, 40, 90, 30, 40);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(6, 20, 30, 40, 90, 50);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(7, 50, 90, 50, 50, 60);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(8, 40, 30, 20, 80, 70);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(9, 50, 70, 40, 30, 80);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(10, 20, 30, 40, 30, 20);
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;
SELECT memberID avg(javascript) from myrecode WHERE memberID IN(1,2,3,4) GROUP BY memberID;