MySQL은 데이터베이스 소프트웨어입니다. 일반적으로 데이터를 추가하거나 검색, 추출하는 기능을 모두 포함해서 데이터베이스라고 부릅니다.
MySQL은 세계에서 가장 많이 쓰이는 오픈 소스의 관계형 데이터베이스 관리 시스템(RDBMS)입니다. MySQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스로 개발된 무료 프로그램입니다. 그래서 홈페이지나 쇼핑몰(워드프레스, cafe24, 제로보드, 그누보드)등 일반적으로 웹 개발에 널리 사용하고 있습니다.
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 6
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 sample02;
Query OK, 0 rows affected (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 varchar(12) NOT NULL,
youAge varchar(20) NOT NULL,
youPhone int(20) NOT NULL,
regTime int(11) NOT NULL,
PRIMARY KEY (memberID)
) charset=utf8;
mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| mymember |
+--------------------+
1 row in set (0.00 sec)
mysql> 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)
mysql> drop table myMember02;
Query OK, 0 rows affected (0.01 sec)
create table myMember1 select * from myMember;
CREATE TABLE myMember2 LIKE myMember;
INSERT INTO myMember2 SELECT * FROM myMember;
ALTER TABLE myMember ADD youGender enum('m','w','x') default 'x' comment "남성은 m, 여성은 w" AFTER youBirth;
mysql> 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)
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youName | varchar(40) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
ALTER TABLE myMember02 MODIFY youName int(20);
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youName | int(20) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youName | int(20) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
ALTER TABLE myMember02 DROP youName;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| myMemberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
+------------+------------------+------+-----+---------+----------------+
INSERT INTO mymember(youEmail, youName, youPass, youBirth, youAge,youPhone, regTime) VALUES('GarenChoi@naver.com','최근영','1234','19970117','26','01054651345','234');
SELECT * FROM myMember;
SELECT * FROM myMember WHERE memberID = 10;
SELECT * FROM myMember WHERE memberID <> 10;
SELECT * FROM myMember WHERE memberID >=5 AND memberID <=10;
SELECT * FROM myMember WHERE memberID BETWEEN 5 AND 10;
SELECT * FROM myMember WHERE memberID NOT BETWEEN 5 AND 10;
SELECT * FROM myMember WHERE memberID IN(4, 6, 13, 14);
SELECT * FROM myMember WHERE youEmail LIKE '%naver%';
SELECT * FROM myMember WHERE youEmail LIKE 'web%';
SELECT * FROM myMember WHERE youEmail LIKE '%com';
SELECT * FROM myMember WHERE youName LIKE '김%';
SELECT * FROM myMember WHERE youName LIKE '김%' OR youName LIKE '황%';
SELECT * FROM myMember WHERE youName LIKE '_근_;
SELECT * FROM myMember WHERE youName LIKE '__연;
SELECT * FROM myMember WHERE youName IS NULL;
SELECT * FROM myMember WHERE youName IS NOT NULL;
SELECT * FROM myMember WHERE youAge >= 30 || youName LIKE '김%';
SELECT * FROM myMember WHERE youAge >= 30 && youName LIKE '김%';
SELECT * FROM myMember WHERE (memberID =2 OR memberID =10) AND youAge >= 30;
SELECT * FROM myMember LIMIT 12;
SELECT * FROM myMember LIMIT 10, 20;
SELECT youEmail FROM myMember LIMIT 10, 20;
SELECT * FROM myMember ORDER BY youAge DESC;
SELECT * FROM myMember ORDER BY youAge ASC;
UPDATE myMember SET youPass = '1234';
UPDATE myMember SET youPass = '1234' WHERE memberID = 3;
UPDATE myMember SET youPass = '1234567' WHERE memberID BETWEEN 5 AND 10;
UPDATE myMember SET youPass = '****' ORDER BY memberID DESC LIMIT 10;
DELETE FROM myMember;
DELETE FROM myMember WHERE memberID = 5;
DELETE FROM myMember WHERE youPass = '****';
DELETE FROM myMember ORDER BY youName LIMIT 5;
DELETE FROM myMember WHERE memberID IN(7,8,9);
SELECT m.youName, m.youEmail, r.youCont, r.regTime FROM myMember m 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 varchar(12) NOT NULL,
youAge varchar(20) NOT NULL,
youPhone int(20) NOT NULL,
regTime int(11) NOT NULL,
PRIMARY KEY (memberID)
) charset=utf8;
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', '정말 감사합니다', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('17', '좋아요~', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('15', '잘 쓸게요', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('7', '너무 싸네용', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('9', '배송이 빨라요!', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('5', '감사합니다.', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('11', '배송이 빨라서 좋아요', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('1', '가성비 쵝오', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('3', '감사해요', '1234');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('18', '잘 쓰겠습니다.', '1234');
SELECT regTime AS reviewTime FROM myReview;
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 unsigned NOT NULL,
html tinyint unsigned NOT NULL,
css tinyint unsigned NOT NULL,
myspl tinyint unsigned NOT NULL,
react tinyint unsigned NOT NULL,
PRIMARY KEY (recodeID)
) charset=utf8;
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(1, 80, 40, 10, 10, 90);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(2, 90, 20, 50, 80, 30);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(3, 30, 30, 80, 80, 40);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(4, 40, 90, 90, 70, 10);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(5, 10, 80, 50, 90, 30);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(6, 70, 70, 60, 80, 50);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(7, 50, 70, 40, 40, 60);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(8, 80, 60, 20, 60, 40);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(9, 90, 30, 60, 50, 30);
INSERT INTO myRecode(memberID, javascript, html, css, myspl, react) VALUES(10, 60, 10, 0, 30, 50);
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