Skip to main content

MYSQL v1.0 </doc>

richclub8.dothome.co.kr

MySQL은 데이터베이스 소프트웨어입니다. 일반적으로 데이터를 추가나 검색, 추출하는 기능을 모두 포함해서 데터베이스라고 합니다.

MySQL은 세계에서 가장 많이 쓰이는 오픈 소스의 관계형 데이터베이스 관리시스템(RDBMS)입니다. MySQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스를 개발된 무료 프로그램입니다. 그래서 홈페이지나 쇼핑물(워드프로세스, cafe24, 제로보드, 그누보드)등 일반적으로 웹 개발에 널리 사용하고 있습니다.

MAMP란 웹사이트를 개발할 때 쓰이는 기술 스택인 maxOS, Apache, MySQL,PHP의 약어이자 솔루션 스택이다.
https://www.mamp.info/en/downloads/
MAMP란 웹사이트를 개발할 때 쓰이는 기술 스택인 maxOS, Apache, MySQL,PHP의 약어이자 솔루션 스택이다.
https://www.mamp.info/en/downloads/
윈도우 : cd MAMP/bin/mysql/bin
로그인 : mysql -uroot -proot
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.
MAC : cd Applications/MAMP/Library/bin
로그인 : ./mysql -uroot -proot
webstoryboyhwang@Webstoryboyui-MacBookPro 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 188
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.
create database 데이터베이스 이름;
mysql> create database sample01;
Query OK, 1 row affected (0.00 sec)
show databases;
mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample01           |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
use 데이터베이스이름;
mysql> use sample01;
Database changed
drop 데이터베이스이름;
mysql> drop database sample02;
Query OK, 0 rows affected (0.00 sec)
create table 테이블 이름;
create table member (
    myMemberID 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(5) NOT NULL,
    regTime int(20) NOT NULL,
    PRIMARY KEY (myMemberID)
) charset=utf8;
Query OK, 0 rows affected (0.03 sec)
show tables;
show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| member             |
+--------------------+
1 row in set (0.00 sec)
desc 테이블 이름;
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    |                |
| regTime    | int(20)          | NO   |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
drop table 테이블 이름;
drop table  member;
Query OK, 0 rows affected (0.01 sec)
CREATE TABLE IF NOT EXISTS 복사 테이블 LIKE 원본 테이블;
CREATE TABLE 복사테이블 SELECT * FROM 원본 테이블;
CREATE TABLE IF NOT EXISTS member2 LIKE member;
CREATE TABLE member3 SELECT * FROM member;
INSERT INTO 테이블이름(필드명) VALUE(데이터)
INSERT INTO member(youEmail, youName, youPass, youBirth,youAge, regTime) VALUES('***@naver.com','김개똥','1234','19990303','24','1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('abs@naver.com', '손시려', '1234', '19970415', '26', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('web@naver.com', '김감기', '1234', '19970530', '26', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('gds@naver.com', '김바보', '1234', '19941009', '29', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('qwe1@naver.com', '정상수', '1234', '19990303', '24', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('gjkdfg@naver.com','이친구','1234','19970205', '26', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('bvbvxa@naver.com','박수선','1234','19990303', '24', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('qwert@naver.com', '김치박', '1234', '19970809', '26',  '04');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('test@naver.com','권투가','1234','19990303', '24', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('zxxc@gmail.com', '문단서', '1234', '19990303', '24',  '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('vvfs@gmail.com', '황소왕', '1234', '19990303', '24',  '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('vip@naver.com','김대갈','1234','19960530', '27', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('qwer@gmail.com','전봇대','1234','19971007', '26', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('ttr@gmail.com','권상식','1234','19960331', '27', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('yyt@gmail.com','강해요','1234','19990303', '24', '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('ysu@gmail.com', '박마음', '1234', '19981010', '25',  '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUE('ccsa@gmail.com', '김두박', '1234', '19700101', '26',  '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('nbnv@gmail.com', '이빨대', '1234', '19990303', '24',  '1234567');
Query OK, 1 row affected (0.00 sec)
INSERT INTO member(youEmail, youName, youPass, youBirth, youAge, regTime) VALUES('tos@gmail.com','이불방','1234','19970731','26','1234567');
Query OK, 1 row affected (0.00 sec)
SELECT * FROM 테이블 이름;
SELECT * FROM member;
+------------+--------------------------+-----------+---------+----------+---------+
| myMemberID | youEmail                 | youName   | youPass | youBirth | regTime |
+------------+--------------------------+-----------+---------+----------+---------+
|          1 | webstoryboy@naver.com    | 황상연    | 1234    | 19990303 | 1234567   |
|          2 | thdtjdgml415@naver.com   | 송성희    | 1234    | 19970415 | 1234567   |
|          3 | mo0647@naver.com         | 김민정    | 1234    | 19970530 | 1234567   |
|          4 | tjrwnsrkdtj@naver.com    | 김석준    | 1234    | 19941009 | 1234567   |
|          5 | jwor124@naver.com        | 정은비    | 1234    | 19990303 | 1234567   |
|          6 | leesh3432@naver.com      | 이영환    | 1234    | 19970205 | 1234567   |
|          7 | sshin4882@naver.com      | 박현신    | 1234    | 19990303 | 1234567   |
|          8 | to_before@naver.com      | 김상준    | 1234    | 19970809 |       4   |
|          9 | kkb7528@naver.com        | 권규비    | 1234    | 19990303 | 1234567   |
|         10 | piowm123@gmail.com       | 문병내    | 1234    | 19990303 | 1234567   |
|         11 | ghkddbwls96@gmail.com    | 황유진    | 1234    | 19990303 | 1234567   |
|         12 | eodnjs9605@naver.com     | 김대원    | 1234    | 19960530 | 1234567   |
|         13 | wjsqhdus971007@gmail.com | 전보연    | 1234    | 19971007 | 1234567   |
|         14 | noeyheyh@gmail.com       | 권혜현    | 1234    | 19960331 | 1234567   |
|         15 | hjkang306@gmail.com      | 강현지    | 1234    | 19990303 | 1234567   |
|         16 | sunhey9810@gmail.com     | 박선혜    | 1234    | 19981010 | 1234567   |
|         17 | kde66034@gmail.com       | 김동언    | 1234    | 19700101 | 1234567   |
|         18 | praise1109@gmail.com     | 이하은    | 1234    | 19990303 | 1234567   |
|         19 | happyham52@gmail.com     | 이중호    | 1234    | 19970731 | 1234567   |
+------------+--------------------------+-----------+---------+----------+---------+
19 rows in set (0.00 sec)
SELECT * FROM 테이블 이름 WHERE 조건;
SELECT * FROM member WHERE myMemberID = 6;
+------------+---------------------+-----------+---------+----------+---------+
| myMemberID | youEmail            | youName   | youPass | youBirth | regTime |
+------------+---------------------+-----------+---------+----------+---------+
|          6 | leesh3432@naver.com | 이영환      | 1234    | 19970205 | 1234567 |
+------------+---------------------+-----------+---------+----------+---------+
1 row in set (0.00 sec)
SELECT * FROM 테이블 이름 WHERE 조건;
SELECT * FROM member WHERE youEmail LIKE "%naver%";
10 rows in set (0.00 sec)
SELECT * FROM member WHERE myMemberID <> 6;
SELECT * FROM member WHERE myMemberID  >= 5 AND myMemberID  <= 10;
6 rows in set (0.00 sec)
SELECT * FROM member WHERE myMemberID  BETWEEN 5 AND 10;
6 rows in set (0.00 sec)
SELECT * FROM member WHERE myMemberID  NOT BETWEEN 5 AND 10;
13 rows in set (0.00 sec)
SELECT * FROM member WHERE myMemberID IN(1,3,10,14)
SELECT * FROM member WHERE youEmaill LIKE 'web%';
SELECT * FROM member WHERE youEmaill LIKE '%com';
SELECT * FROM member WHERE youName LIKE '김%';
SELECT * FROM member WHERE youName LIKE '김%' OR LIKE '황%';
SELECT * FROM member WHERE youName LIKE '_상_';
SELECT * FROM member WHERE youName IS NULL;
SELECT * FROM member WHERE youAge >= 20 AND youName LIKE "김%";
SELECT * FROM member WHERE youAge >= 20 OR youName LIKE "김%";
SELECT * FROM member WHERE (myMemberID = 2 OR myMemberID = 10) AND  youAge >= 20;
SELECT * FROM member LIMIT 5;
SELECT * FROM member LIMIT 5, 5;
SELECT youEmail FROM member LIMIT 5;
UPDATE 테이블명(필드명) SET 컬럼명 조건
UPDATE member SET youPhone = '010-9999-9999'
UPDATE member SET youPass = '0123' WHERE myMemberID = 3;
UPDATE member SET youPass = '12345' WHERE myMemberID BETWEEN 5 AND 10;
UPDATE member SET youPass = '1234567' ORDER BY youPass DESC LIMIT 10;
DELETE 테이블명 WHERE 조건;
DELETE FROM member WHERE myMemberID = 5;
Query OK, 1 rows affected (0.00 sec)
DELETE FROM member WHERE youPass = 1234;
DELETE FROM member ORDER BY youName LIMIT 5;
DELETE FROM member WHERE myMemberID = IN(7,9,10);
ALTER TABLE 테이블명 ADD 추가할 필드명 AFTER 월드 위치
ALTER TABLE member ADD youPhone INT(20) NOT NULL AFTER youAge;
+------------+--------------------------+-----------+---------+----------+--------+----------+---------+
| myMemberID | youEmail                 | youName   | youPass | youBirth | youAge | youPhone | regTime |
+------------+--------------------------+-----------+---------+----------+--------+----------+---------+
ALTER TABLE member ADD youGender enum('m','f','x') DEFAULT 'x' comment "남자 m, 여자 w" AFTER youPhone;
+------------+--------------------------+-----------+---------+----------+--------+----------+-----------+---------+
| myMemberID | youEmail                 | youName   | youPass | youBirth | youAge | youPhone | youGender | regTime |
+------------+--------------------------+-----------+---------+----------+--------+----------+-----------+---------+
|          1 | ******@naver.com         | ***       | 1234    | 19970415 |     26 |        0 | x         | 1234567 |
+------------+--------------------------+-----------+---------+----------+--------+----------+-----------+---------+
ALTER TABLE 테이블명 MODIFY 수정할필드명
ALTER TABLE member MODIFY youAge int(10);
+------------+-------------------+------+-----+---------+----------------+
| 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(10)           | YES  |     | NULL    |                |
| youPhone   | int(20)           | NO   |     | NULL    |                |
| youGender  | enum('m','f','x') | YES  |     | x       |                |
| regTime    | int(20)           | NO   |     | NULL    |                |
+------------+-------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
ALTER TABLE 테이블명 DROP 필드명;
ALTER TABLE member DROP youGender;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

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(10)          | YES  |     | NULL    |                |
| youPhone   | int(20)          | NO   |     | NULL    |                |
| regTime    | int(20)          | NO   |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
SELECT 필드명 FROM 테이블 명 엘리어스 JOIN 연결할 테이블명 엘리어스 ON(조건문)
create table myMember (
    myMemberID 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(5) NOT NULL,
    youPhon int(20) NOT NULL,
    regTime int(20) NOT NULL,
    PRIMARY KEY (myMemberID)
) charset=utf8;
create table myReview (
    myReviewID int(10) UNSIGNED AUTO_INCREMENT,
    myMemberID int(10) UNSIGNED,
    youText tinytext NOT NULL,
    regTime int(20) NOT NULL,
    PRIMARY KEY (myReviewID)
) charset=utf8;
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhon, regTime) VALUES('***@naver.com','김개똥','1234','19990303','24', '01012341234', '1234567'); 
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhon, regTime) VALUES('abs@naver.com', '손시려', '1234', '19970415','26', '01012341234','1234567'); 
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhon, regTime) VALUES('web@naver.com', '김감기', '1234', '19970530', '26', '01012341234','1234567'); 
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhon, regTime) VALUES('gds@naver.com','김바보', '1234', '19941009', '29', '01012341234','1234567'); 
INSERT INTO myMember(youEmail, youName, youPass, youBirth, youAge, youPhon, regTime) VALUES('qwe1@naver.com', '정상수', '1234', '19990303', '24', '01012341234','1234567'); 


INSERT INTO myReview(myMemberID, youText, regTime) VALUES('1', "정말 감사합니다. 너무좋네요!!", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('2', "즐추", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('3', "감사", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('4', "너무 멋져요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('5', "베리굿", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('1', "재밌네요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('1', "굿", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('3', "행복해요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('3', "사랑해요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('5', "좋아해요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('4', "사모해요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('2', "흠모해요", '1234567');
INSERT INTO myReview(myMemberID, youText, regTime) VALUES('3', "힘들어요", '1234567');

SELECT mb.youName mb.youEmail mr.youText mr.regTime FROM myMember mb JOIN myReview mr ON(mb.myMemberID = mr.myMemberID);