MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템(RDBMS)입니다.
MySQL은 다중 사용자, 다중 스레드 RDBMS로서, 데이터의 안정성과 보안성을 제공합니다. MySQL은 대부분의 운영 체제에서 작동하며, 웹 애플리케이션 개발, 데이터 분석, 빅 데이터, 클라우드 기반 애플리케이션 등에 많이 사용됩니다.
MySQL은 SQL(Structured Query Language)을 사용하여 데이터를 관리합니다. SQL은 데이터베이스에 저장된 데이터를 조작하기 위한 표준적인 언어로서, 데이터를 검색, 삽입, 업데이트, 삭제하는데 사용됩니다. MySQL은 이러한 SQL 문을 이용하여 데이터를 쿼리하고, 데이터의 무결성을 유지하며, 데이터베이스를 관리합니다.
MySQL은 사용이 간편하며, 대용량 데이터베이스를 처리할 수 있는 고성능 기능을 제공합니다. 또한, 오픈 소스로서, 다양한 개발자들이 지속적으로 개발 및 유지보수하고 있어, 사용자들이 다양한 문제들을 해결할 수 있는 방법을 제공합니다.
MySQL은 PHP 스크립트 언어와 상호 연동이 잘 됨녀서 오픈소스로 개발된 무료 프로그램입니다. 그래서 홈페이지나 쇼핑몰(워드프레스, CAFE24, 그누보드, 제로보드)에 일반적으로 웹개발에 널리 사용하고 있습니다.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
mysql> create database sample01;
Query OK, 1 row affected (0.00 sec)
mysql> use sample01;
Database changed
mysql> drop database sample01;
Query OK, 0 rows affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
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(20) NOT NULL,
PRIMARY KEY (reviewID)
) charset=utf8;
mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| member |
+--------------------+
1 row in set (0.00 sec)
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 | |
| youAge | int(5) | NO | | NULL | |
| regTime | int(20) | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
mysql> drop table member;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> truncate table member;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
Empty set (0.00 sec)
INSERT
INTO
테이블이름(필드명)
VALUE
INSERT INTO member(youEmail, youName, youpass, youBirth, youAge, regTime) VALUES("jo0132@naver.com","노지영","1234","19950323","29","03140316");
INSERT INTO reviews(memberID, youText, regTime) VALUES("4","정말 감사합니다. 잘 봤어요","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("14","잘 봤어요","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("2","좋은 정보감사합니다","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("5","정말 감사합니다. 잘 봤어요","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("6","이건 사야합니다.","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("7","정말 감사합니다. 너무좋아요","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("8","정말 감사합니다. 잘 봤어요","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("9","잘 봤어요","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("10","정말 감사합니다.","1234567");
INSERT INTO reviews(memberID, youText, regTime) VALUES("4","정말 별로네요. 다시는 안살듯?","1234567");
SELECT
필드명 FROM
테이블명 WHERE
조건SELECT * FROM members;
SELECT * FROM members WHERE memberID = 1;
SELECT * FROM members WHERE memberID <> 1;
SELECT * FROM members WHERE memberID >= 5 AND memberID <= 10;
SELECT * FROM members WHERE youAge BETWEEN 30 AND 40;
SELECT * FROM members WHERE youAge NOT BETWEEN 30 AND 40;
SELECT * FROM members WHERE memberID IN(1,5,10,15);
SELECT * FROM members WHERE youName IS NULL;
SELECT * FROM members WHERE youName IS NOT NULL;
SELECT * FROM members WHERE youName LIKE '%연%';
SELECT * FROM members WHERE youName LIKE '김%';
SELECT * FROM members WHERE youName LIKE '%현';
SELECT * FROM members WHERE youName LIKE '_상_';
SELECT * FROM members WHERE youName LIKE '김%' OR youName LIKE '이%';
SELECT youName FROM members;
SELECT youName FROM members ORDER BY youName DESC;
SELECT youName FROM members ORDER BY youName ASC;
SELECT count(youName) FROM members;
SELECT count(*) FROM members;
SELECT youName, youAge FROM members;
SELECT youName, youAge FROM members WHERE youAge >= 30;
SELECT youName FROM members WHERE youAge >= 25 AND youName LIKE '김%';
SELECT youName FROM members WHERE youAge >= 25 OR youName LIKE '김%';
SELECT * FROM members WHERE (memberID = 2 OR memberID = 9) AND youAge >= 25;
SELECT * FROM members WHERE youAge >=25 AND memberID IN(2,9);
SELECT * FROM members LIMIT 5;
SELECT * FROM members LIMIT 5, 5;
SELECT youEmail FROM members LIMIT 10, 10;
SELECT DISTINCT youAge FROM members
SELECT
count
필드명 FROM
테이블명SELECT count(youPhone) FROM members;
SELECT count(youEmail) FROM members;
UPDATE
테이블명 SET
컬럼명 조건UPDATE members SET youPass = '1234';
UPDATE members SET youPass = '9999' WHERE memberID = '3';
UPDATE members SET youPass = '7777' WHERE memberID BETWEEN 5 AND 10;
UPDATE members SET youPass = '9999' ORDER BY youName DESC LIMIT 5;
DELETE
FROM
테이블명 WHERE
조건DELETE FROM membesr WHERE memberID = 5;
DELETE FROM members WHERE youPass = '1234';
DELETE FROM members ORDER BY memberID LIMIT 5;
DELETE FROM members WHERE memberID IN(11, 13, 15);
ALTER TABLE members ADD youPhone varchar(20) NOT NULL;
ALTER TABLE members ADD youPhone varchar(20) NOT NULL AFTER youAge;
ALTER TABLE members MODIFY youPhone int(20) NOT NULL;
ALTER TABLE members DROP youPhone;
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);
SELECT m.youEmail, m.youName, r.youText, r.regTime FROM members m LEFT JOIN reviews r ON (m.memberID = r.memberID);
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 count(class) FROM schoolRecode;
SELECT count(*) FROM schoolRecode;
SELECT sum(coding) FROM schoolRecode;
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 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 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;