사용자 또는 응용 프로그램이 데이터베이스에 접속하여 데이터를 조회, 삽입, 수정, 삭제 등의 작업을 수행할 수 있도록 도움
MySQL Client
MySQL Monitor - CLI기반
MySQL Workbench - GUI기반
SQL 분류
DML(Data Manipulation Language)
데이터 조작 언어
SELECT, INSERT, UPDATE, DELETE
DDL(Data Definition Language)
데이터 정의 언어, 데이터베이스 개체를 생성, 삭제, 변경
CREATE, DROP, ALTER
DCL(Data Control Language)
데이터 제어 언어, 데이터를 조회할 수 있는 권한을 부여하거나 빼앗을 때 사용
GRANT, REVOKE
MYSQL 구조
데이터는 표(table)에 저장
표들은 데이터 베이스(스키마, shema)
이러한 스키마를 모아놓은 것이 데이터 베이스 서버
1. CREATE
스키마 생성
create database opentutorials;
테이블 생성
CREATE TABLE topic();
CREATE TABLE topic (
id INT(11) NOT NULL AUTO_INCREMENT, //자동으로 1씩 증가해서 중복없게
title VARCHAR(100) NOT NULL,
description TEXT NULL,
created DATETIME NOT NULL,
author VARCHAR(30) NULL,
profile VARCHAR(100) NULL,
PRIMARY KEY(id));
//이름 타입(길이) 널여부
생성된 스키마 확인
show databases;
데이터베이스 선택
use opentutorials;
테이블 리스트 조회
SHOW TABLES;
DESC 테이블명; #테이블의 열 리스트 조회
테이블에 데이터 추가
INSERT INTO 테이블명 (컬럼명1, 컬럼명2) VALUES (넣을 데이터1, 넣을 데이터2);
insert into topic (title,description,created,author,profile)
values('MySQL','MySQL is...',now(),'egoing','developer');
///id는 AUTO_INCREMENT했으므로 사용x
2. READ
입력한 데이터 가져오기
SELECT 컬럼명(*) FROM 테이블명;
mysql> select * from topic;
+----+------------+-------------------------+---------------------+--------+---------------------------+
| id | title | description | created | author | profile |
+----+------------+-------------------------+---------------------+--------+---------------------------+
| 1 | MySQL | MySQL is... | 2023-07-01 22:14:46 | egoing | developer |
| 2 | ORACLE | ORACLE is | 2023-07-01 22:25:39 | egoing | developer |
| 3 | SQL Server | SQL Server is... | 2023-07-01 22:28:29 | duru | data administrator |
| 4 | PostgreSQL | PostgreSQL Server is... | 2023-07-01 22:30:38 | taeho | data scientist, developer |
| 5 | MongoDB | MongoDB is... | 2023-07-01 22:31:32 | egoing | developer |
+----+------------+-------------------------+---------------------+--------+---------------------------+
5 rows in set (0.00 sec)
조건 추가
SELECT 컬럼명 FROM 테이블명 WHERE author=’egoing’;
mysql> select id,title,created,author from topic where author='egoing';
+----+---------+---------------------+--------+
| id | title | created | author |
+----+---------+---------------------+--------+
| 1 | MySQL | 2023-07-01 22:14:46 | egoing |
| 2 | ORACLE | 2023-07-01 22:25:39 | egoing |
| 5 | MongoDB | 2023-07-01 22:31:32 | egoing |
+----+---------+---------------------+--------+
3 rows in set (0.00 sec)
select id,title,created,author from topic where author='egoing' order by id DESC; #id를 기준으로 내림차순
mysql> select id,title,created,author from topic where author='egoing' order by id DESC;
+----+---------+---------------------+--------+
| id | title | created | author |
+----+---------+---------------------+--------+
| 5 | MongoDB | 2023-07-01 22:31:32 | egoing |
| 2 | ORACLE | 2023-07-01 22:25:39 | egoing |
| 1 | MySQL | 2023-07-01 22:14:46 | egoing |
+----+---------+---------------------+--------+
3 rows in set (0.01 sec)