MySQL은 데이터 베이스 소프트웨어입니다 데이터를 추가하거나 검색, 추출하는 기능을 모두 포함해서 데이터베이스라고 합니다
MySQL은 세계에서 가장 많이 쓰이는 오픈 소스의 관계형 데이터베이스 관리 시스템(RDBMS)입니다 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 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.
MacBook-Air ~ % cd /Applications/MAMP/Library/bin
MacBook-Air bin % sudo ./mysql -uroot -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 186
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.
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> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| mymember |
+--------------------+
1 row in set (0.00 sec)
mysql> drop database text012;
Query OK, 0 rows affected (0.02 sec)
CREATE TABLE myMember (
memberID int(10) unsigned auto_increment,
youEmail varchar(40) NOT NULL,
youName varchar(12) NOT NULL,
youPass varchar(20) NOT NULL,
youBirth int(20) NOT NULL,
youAge int(10) 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 mymember;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
create table myMember1 SELECT * FROM myMember;
create table myMember2 LIKE myMember;
INSERT INTO myMember3 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)
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)
ALTER TABLE mymember2 DROP youGender;
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(40) | NO | | NULL | |
| youAGE | int(2) unsigned | 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('placidcy@gmail.com', '김지섭', '1234', '19900212', '32', '01011112222','20220314');
SELECT * FROM myMember;
SELECT * FROM mymember WHERE memberID = 10;
SELECT * FROM mymember WHERE memberID <> 10;
SELECT * FROM mymember WHERE memberID >= 5 AND memberID < 11;
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 LIKE '__근'; (하나의 문자 검색)
SELECT * FROM myMember WHERE youName IS NULL;
SELECT * FROM myMember WHERE youName IS NOT NULL;
SELECT * FROM mymember WHERE youAge >= 20 OR youName like '김%';
SELECT * FROM mymember WHERE youAge >= 20 AND youName like '김%';
SELECT * FROM mymember WHERE (memberID >= 2 OR memberID = 10) AND youAge >= 30;
SELECT * FROM mymember LIMIT 12;
SELECT * FROM mymember LIMIT 5, 10;
SELECT youEmail FROM mymember LIMIT 5, 10; (이메일만 불러오기)
SELECT * FROM mymember ORDER BY youAge DESC; (오름차순)
SELECT * FROM mymember ORDER BY youAge ASC; (내림차순)
UPDATE mymember SET youPass = '1234';
UPDATE mymember SET youPass = '123456' WHERE memberID >=5 AND memberID <=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(8,9,10);
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(12) NOT NULL,
youPass varchar(20) NOT NULL,
youBirth int(20) NOT NULL,
youAge int(10) 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('6', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('1', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('7', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('10', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('9', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('2', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('4', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('3', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('5', '대입할 아이디 값 입력과 텍스트 출력', '12345');
INSERT INTO myReview(memberID, youCont, regTime) VALUES('7', '대입할 아이디 값 입력과 텍스트 출력', '12345');
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 unsigned NOT NULL,
html tinyint unsigned NOT NULL,
css tinyint unsigned NOT NULL,
mysql tinyint unsigned NOT NULL,
react tinyint unsigned NOT NULL,
PRIMARY KEY (recodeID)
) charset=utf8;
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('7', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('1', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('3', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('5', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('8', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('2', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('4', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('6', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('9', '60', '65', '70', '75', '80');
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES('10', '60', '65', '70', '75', '80');
SELECT count(javascript) FROM myRecode; // javascript 값의 개수
SELECT sum(javascript) FROM myRecode; // javascript 값들의 합
SELECT avg(javascript) FROM myRecode; // javascript 값들의 평균 값
SELECT max(javascript) FROM myRecode; // javascript 값들 중 최대 값
SELECT min(javascript) FROM myRecode; // javascript 값들 중 최소 값