MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템(RDBMS)입니다.
MySQL은 다양한 운영 체제에서 사용할 수 있으며, 대부분의 웹 응용 프로그램에서 데이터베이스 시스템으로 많이 사용됩니다. MySQL은 클라이언트/서버 모델을 기반으로합니다. 이것은 MySQL 서버가 데이터베이스를 호스팅하고, 클라이언트 애플리케이션이 서버에 연결하여 데이터베이스에 액세스할 수 있도록 합니다. MySQL은 다양한 클라이언트 프로그램 및 API를 지원하여, 여러 다른 프로그래밍 언어로 쉽게 데이터베이스를 사용할 수 있습니다.
MySQL은 다양한 기능을 제공합니다. 이 중 일부 기능은 다음과 같습니다.
1. 데이터의 저장과 검색: MySQL은 테이블로 구성된 데이터베이스에서 데이터를 저장하고 검색할 수 있습니다.
2. 데이터의 백업과 복원: MySQL은 데이터베이스를 백업하고 복원하는 기능을 제공합니다.
3. 보안: MySQL은 데이터베이스 보안을 위한 다양한 기능을 제공합니다.
4.병행성 제어: MySQL은 여러 사용자가 동시에 데이터베이스에 액세스하는 것을 허용하며, 이를 위해 병행성 제어 기능을 제공합니다.
5. 트랜잭션 처리: MySQL은 트랜잭션 처리를 지원하며, 데이터베이스의 일관성과 무결성을 유지할 수 있습니다.
MySQL은 다양한 용도로 사용됩니다. 대표적으로 웹 응용 프로그램, 데이터 분석, 사무실 자동화 등에서 사용됩니다. MySQL은 또한 다른 데이터베이스 시스템과 함께 사용될 수 있으며, 다양한 플러그인 및 확장 기능을 제공합니다.
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 5
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.
show
databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec) //현재 데이터베이스 목록
create
database
데이터베이스 이름;mysql> create database sample01;
Query OK, 1 row affected (0.01 sec) //"sample01" 데이터 생성
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample01 |
| sys |
+--------------------+
5 rows in set (0.00 sec)
use
데이터베이스 이름;
mysql> use sample01;
Database changed //use로 데이터베이스 사용
drop
database
데이터베이스 이름;
mysql> drop database sample01;
Query OK, 0 rows affected (0.03 sec) //drop으로 삭제
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec) //목록에서 sample01 없어진 걸 확인
create
table
테이블 이름;create table members(
memberID int(10) unsigned auto_increment,
youEmail varchar(40) NOT NULL,
youName varchar(10) NOT NULL,
youPass varchar(20) NOT NULL,
youBirth int(10) NOT NULL,
youAge int(5) NOT NULL,
regTime int(30) NOT NULL,
PRIMARY KEY(memberID)
) charset=utf8;
create table reviews(
reviewID int(10) unsigned auto_increment,
memberID int(10) unsigned,
youText tinytext NOT NULL,
regTime int(30) NOT NULL,
PRIMARY KEY(reviewID)
) charset=utf8;
show
tables;
mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| members |
+--------------------+
1 row in set (0.00 sec)
desc
테이블 이름;mysql> desc members;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| memberID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| youEmail | varchar(40) | NO | | NULL | |
| youName | varchar(10) | NO | | NULL | |
| youPass | varchar(20) | NO | | NULL | |
| youBirth | int(10) | NO | | NULL | |
| youAge | int(5) | NO | | NULL | |
| regTime | int(30) | NO | | NULL | |
+----------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
drop
table
테이블 이름;mysql> drop table members;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
TRUNCATE
table
테이블 이름;mysql> TRUNCATE table members;
INSERT
INTO
테이블명(필드명) VALUES();
mysql> INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("webstoryboy@naver.com","황상연","1234","19990304","26","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("dlscks0930@naver.com","황인찬","1234","19980930","26","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("gp2617@naver.com","천설희","1234","19950313","29","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("dsy1943@naver.com","여다슬","1943","20011227","23","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("1346zany@gmail.com","이승연","1234","19991216","25","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("ebmdols@naver.com","장진용","1234","19940401","26","03140316");
INSERT INTO members(youEmail, youName, youpass, youBirth, youAge, regTime) VALUES("jo0132@naver.com","노지영","1234","19950323","29","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("skadldldl123@gmail.com","안교남","1234","19920923","32","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("ehcjswo1@gmail.com","김도현","123","19951003","29","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("goed0522@gmail.com","변우현","1234","20000629","24","03141516");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("yuna243441@naver.com","이유나","1234","20020508","22","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("im.kebab00@gmail.com","이요셉","1234","19990315","26","03140316");
INSERT INTO members(youEmail, youName, youpass, youBirth, youAge, regTime) VALUES("ture403@gmail.com","전윤기","1234","19920210","32","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("dongjin6539@naver.com","신동진","1234","19960709","28","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("gnsrbdi@naver.com","이훈규","1234","19950227","29","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("jeongyouna_@naver.com","정유나","1234","19950606","29","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("daanbi1345@gmail.com","조은경","1234","19930205","31","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("hunmi961119@gmail.com","진현미","1234","19961119","28","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("getgrovy@gmail.com","정희석","1234","19990101","26","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("kshkmn0929@naver.com","김도현","0929","20020929","22","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("hyejeong3283@gmail.com","이혜정","1234","19940622","30","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("didrn94553@gmail.com","이양구","1234","19941212","30","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("ghkddn132@naver.com","정황우","1234","19970130","25","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("esansi@naver.com","김현빈","1234","19970524","27","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("lee3ll@naver.com","이은지","1234","19930311","31","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("dripidea77@gmail.com","김시연","1234","19930313","31","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("itshena01@gmail.com","조한미","1234","19930211","31","03140316");
INSERT INTO members(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES("aimee00418@gmail.com","권현정","1234","19830211","39","03140316");
mysql> INSERT INTO reviews(memberID, youText, regTime) VALUES("4", "정말 감사합니다. 잘 봤어요", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("1", "정말 감사합니다람쥐", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("5", "안교남 파이팅!", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("9", "어떻게 사람 이름이...", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("8", "레몬 한 개에는 자그마치 레몬 하나만큼의 비타민이 들어있다구!", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("7", "혹시 으데 김 씨입니까?", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("6", "어디로 가야 하오", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("3", "데마시아를 위하여!", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("2", "경찰을 부르겠습니다!!", "123456");
INSERT INTO reviews(memberID, youText, regTime) VALUES("1", "저는 탐정, 남도일이죠.", "123456");
SELECT
필드명 FROM
테이블명 WHERE
조건;SELECT * FROM members; //전체 필드명 = *
SELECT * FROM members WHERE memberID=1;
SELECT * FROM members WHERE memberID <> 1;
mysql> SELECT * FROM members WHERE youAge >=30 AND youAge <= 40;
//==SELECT * FROM members WHERE youAge BETWEEN 30 AND 40;
mysql> SELECT * FROM members WHERE youAge NOT BETWEEN 30 AND 40;
mysql> SELECT * FROM members WHERE memberID IN(1,5,10,15);
mysql> SELECT * FROM members WHERE youName IS NULL;
mysql> SELECT * FROM members WHERE youName IS NOT NULL;
mysql> SELECT * FROM members WHERE youName LIKE '%연%';
mysql> SELECT * FROM members WHERE youName LIKE '김%';
mysql> SELECT * FROM youName LIKE '%현';
mysql> SELECT * FROM members WHERE youName LIKE "_상_";
//밑줄(_)은 문자 개수를 의미한다.
mysql> SELECT * FROM members WHERE youName LIKE "김%" OR youName LIKE "이%";
//두 조건을 쓸 때 필드명을 한 번 더 적어야 함!!
mysql> SELECT * FROM members WHERE youAge >=25 AND youName LIKE "김%";
mysql> SELECT * FROM members WHERE youAge >= 25 AND memberID IN(2, 9);
//SELECT * FROM members WHERE youAge >= 25 AND (memberID = 2 OR memberID = 9);
mysql> SELECT * FROM members LIMIT 5;
mysql> SELECT * FROM members LIMIT 5, 5;
mysql> SELECT * FROM members LIMIT 10, 10;
mysql> SELECT youName FROM members; // * 대신 column 선택
mysql> SELECT youName, youAge FROM members; //복수 선택은 쉼표(,)
mysql> SELECT DISTINCT youAge FROM members;
mysql> SELECT count(youName) FROM members;
mysql> SELECT count(*) FROM members;
mysql> SELECT youName FROM members ORDER BY youName DESC;
mysql> SELECT youName FROM members ORDER BY youName ASC;
UPDATE
테이블명 SET
컬럼명 조건UPDATE members SET youPass = "1234"
mysql> UPDATE members SET youPass="9999" WHERE memberID="3";
mysql> UPDATE members SET youPass="7777" WHERE memberID BETWEEN 5 AND 10;
mysql> UPDATE members SET youPass="1234567" ORDER BY youPass LIMIT 10;
DELETE
FROM
테이블명 WHERE
조건mysql> DELETE FROM members WHERE memberID=5;
mysql> DELETE FROM members WHERE youPass="1234";
mysql> DELETE FROM members ORDER BY memberID LIMIT 5;
mysql> DELETE FROM members WHERE memberID IN(11, 13, 15);
mysql> UPDATE members SET youPass="9999" ORDER BY youName DESC LIMIT 5;
ALTER
TABLE
[테이블명] ADD
[추가할 필드명] AFTER
[필드 위치]ALTER TABLE members ADD youPhone varchar(20) NOT NULL;
ALTER TABLE members ADD youPhone varchar(20) NOT NULL AFTER youAge;
ALTER
TABLE
[테이블명] MODIFY
[수정할 필드명]ALTER TABLE members MODIFY youPhone int(20) NOT NULL;
ALTER
TABLE
[테이블명] DROP
[삭제할 필드명]ALTER TABLE members DROP youPhone;
SELECT
[필드명] FROM
[테이블명][앨리어스] JOIN
[연결할 테이블명 앨리어스] ON
[기준이 되는 조건문]SELECT m.youEmail, m.youName, r.youText, r.regTime FROM members m JOIN reviews r ON (m.memberID = r.memberID);
SELECT m.youEmail, m.youName, r.youText, r.regTime AS reviewRegtime FROM members m JOIN reviews r ON (m.memberID = r.memberID);
//[필드명] AS [변경할 필드명]
SELECT m.youEmail, m.youName, r.youText, r.regTime FROM members m LEFT JOIN reviews r ON (m.memberID = r.memberID);
//JOIN 옆에 LEFT
CREATE TABLE schoolRecode (
studentID int(10) unsigned NOT NULL AUTO_INCREMENT comment "학생번호",
class tinytext comment "소속클래스(반)",
english tinytext NOT NULL comment "영어점수",
math tinytext NOT NULL comment "수학점수",
science tinytext NOT NULL comment "과학점수",
coding tinytext NOT NULL comment "코딩점수",
PRIMARY KEY(studentID)
) charset=utf8;
INSERT INTO schoolRecode(english, math, science, coding) VALUES(50, 10, 20, 100);
INSERT INTO schoolRecode(class, english, math, science, coding) VALUES(1, 50, 10, 20, 100);
INSERT INTO schoolRecode(class, english, math, science, coding) VALUES(2, 40, 30, 22, 90);
INSERT INTO schoolRecode(class, english, math, science, coding) VALUES(2, 50, 40, 23, 80);
INSERT INTO schoolRecode(class, english, math, science, coding) VALUES(3, 60, 50, 24, 70);
INSERT INTO schoolRecode(class, english, math, science, coding) VALUES(4, 70, 60, 25, 60);
INSERT INTO schoolRecode(class, english, math, science, coding) VALUES(5, 80, 70, 26, 50);
SELECT * FROM schoolRecode;
+-----------+-------+---------+------+---------+--------+
| studentID | class | english | math | science | coding |
+-----------+-------+---------+------+---------+--------+
| 1 | NULL | 50 | 10 | 20 | 100 |
| 2 | 1 | 50 | 10 | 20 | 100 |
| 3 | 2 | 40 | 30 | 22 | 90 |
| 4 | 2 | 50 | 40 | 23 | 80 |
| 5 | 3 | 60 | 50 | 24 | 70 |
| 6 | 4 | 70 | 60 | 25 | 60 |
| 7 | 5 | 80 | 70 | 26 | 50 |
+-----------+-------+---------+------+---------+--------+
SELECT count(class) FROM schoolRecode; //6
SELECT count(*) FROM schoolRecode; //7 NULL값 포함
SELECT sum(coding) FROM schoolRecode; //450
SELECT sum(coding) FROM schoolRecode WHERE studentID >= 1 AND studentID <= 4;
SELECT max(coding) FROM schoolRecode;
SELECT min(coding) FROM schoolRecode;
SELECT avg(coding) FROM schoolRecode;
SELECT
[필드명] FROM
[테이블명] GROUP BY
[필드명];SELECT class, avg(english) FROM schoolRecode WHERE class IN(1,2,3,4,5) GROUP BY class;
SELECT class, sum(math) FROM schoolRecode WHERE class IN(1,2,3,4,5) GROUP BY class;
SELECT class, sum(math) FROM schoolRecode WHERE class IN(1,2,3,4,5) GROUP BY class HAVING sum(math) >= 70;
SELECT
[필드명] FROM
[테이블명] ORDER BY
[필드명] DESC 또는 ASC
;SELECT studentID, english FROM schoolRecode ORDER BY english DESC;
SELECT studentID, english FROM schoolRecode ORDER BY english ASC;
SELECT class, sum(math) FROM schoolRecode WHERE class IN(1,2,3,4,5) GROUP BY class ORDER BY sum(math) DESC;