Lumpy Space Princess - Adventure Time

CODING

MySQL에 대해서 알아보자

jongyung 2023. 3. 13. 22:21

“ 지연되는 프로젝트에 인력을 더 투입하면 오히려 더 늦어진다. ”

- Frederick Philips Brooks
Mythical Man-Month 저자
728x90

MySQL이란?

MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템(RDBMS)입니다. 
사용자, 다중 스레드 RDBMS로서, 데이터의 안정성과 보안성을 제공합니다. 
대부분의 운영 체제에서 작동하며, 웹 애플리케이션 개발, 데이터 분석, 빅 데이터, 클라우드 기반 애플리케이션 등에 많이 사용됩니다.

 

SQL(Structured Query Language)을 사용하여 데이터를 관리합니다. 
SQL은 데이터베이스에 저장된 데이터를 조작하기 위한 표준적인 언어로서, 데이터를 검색, 삽입, 업데이트, 삭제하는데 사용됩니다. 
MySQL은 이러한 SQL 문을 이용하여 데이터를 쿼리하고, 데이터의 무결성을 유지하며, 데이터베이스를 관리합니다. 

MySQL은 사용이 간편하며, 대용량 데이터베이스를 처리할 수 있는 고성능 기능을 제공합니다. 
또한, 오픈 소스로서, 다양한 개발자들이 지속적으로 개발 및 유지보수하고 있어, 사용자들이 다양한 문제들을 해결할 수 있는 방법을 제공합니다.

MYSQL은 PHP 스크립트 언어와 상호 연동이 잘 되면서 오픈소스로 개발된 무료 프로그램입니다.
그래서 홈페이지나 쇼핑몰(워드프레스, Cafe24, 그누보드, 제로보드)에 가장 일반적으로 사용하고 있습니다.

MySQL 설치

https://www.mamp.info/en/downloads/ 에서 다운로드 받으면 됩니다.

MAMP란 웹사이트를 개발할 때 쓰이는 기술 스택인 macOS, Apache, MySAL, PHP의 약어이자 솔루션 스택입니다.

MySQL 실행

윈도우와 맥으로 나눠서 설명하겠습니다.

윈도우

cd c:\MAMP\bin\mysql\bin 입력 후, 로그인 : mysql -uroot -proot 하면,

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 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.

이런 결과가 나옵니다.

cd Applications/MAMP/Library/bin 입력 후, 로그인 : ./mysql -uroot -proot 하면,

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 501
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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.

이런 결과가 나옵니다.

 

다음으로는, 데이터와 테이블을 어떻게 다루는 지를 설명하겠습니다.

데이터 베이스 보기

데이터 베이스를 보기 위해서 사용하는 방식입니다.

show databases; 를 입력하면, 

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
4 rows in set (0.00 sec)

이런 결과가 나옵니다. 

데이터 베이스가 아직 없으니, 만들어 보겠습니다.

데이터 베이스 만들기

create database 데이터 베이스 이름;을 입력하면,

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql       |
| performance_schema |
| sample01      |
| sys        |
+--------------------+
5 rows in set (0.00 sec)

이런 결과가 나옵니다. 

위의 결과와 다른 점이 보이죠? 데이터 베이스의 이름은 보이는 것처럼 sample01로 입력했습니다.

저것을 사용하는 방법도 알아보겠습니다.

데이터 베이스 사용

use 데이터베이스 이름;을 입력하면,

mysql> use sample01;
Database changed

이런 결과가 나옵니다.

그리고 이런 데이터 베이스를 삭제하려면 이렇게 해야합니다.

데이터 베에스 삭제

drop 데이터베이스 이름;을 입력하면,

mysql> drop database sample01;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
4 rows in set (0.01 sec)

이런 결과가 나옵니다. 

다시 처음의 박스 안의 모습과 같이 데이터 베이스가 사라진 모습입니다.

 

테이블도 입력할 수 있습니다.

테이블

테이블을 만들기 위해서는 create table 테이블 이름;을 입력합니다.

그 결과는,

mysql> 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.02 sec)

이렇습니다. 

테이블이 어떤 형태일지 보겠습니다.

테이블 전체보기

show tables;를 입력하면,

mysql> show tables;
+--------------------+
| Tables_in_sample01 |
+--------------------+
| member       |
+--------------------+
1 row in set (0.00 sec)

이렇게 나옵니다. 

테이블 보기

desc 테이블 이름;을 입력하면 더 자세하게 볼 수 있습니다.

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)

테이블의 형태를 보여주고 있습니다.

테이블 삭제

테이블도 데이터 베이스처럼 삭제해보겠습니다.

drop 테이블 이름;을 입력하면,

mysql> drop table member;
Query OK, 0 rows affected (0.02 sec)

이렇게 나옵니다.

테이블이 삭제된 것입니다.