๐ SQL(Structured Query Language)
- ๊ด๊ณํ, Relational DBMS(RDBMS)
- ํค์ ๊ฐ์ ๊ด๊ณ๋ฅผ ํ ์ด๋ธํ ์ํจ ์์น์ ํ ๋๋ก DB๋ฅผ ๊ตฌ์ฑ
- ์ฅ์ : ๊ตฌ์กฐํ๊ฐ ๋ช ํํ์ฌ ์์ธ๊ฐ ์์, ๋ฐ์ดํฐ ์ ์ถ๋ ฅ ์๋๊ฐ ๋งค์ฐ ๋น ๋ฆ, ์ ๋ขฐ์ฑ์ด ๋์
- ๋จ์ : DB ๊ตฌ์กฐ ๋ณ๊ฒฝ์ด ๋งค์ฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ ๋น ๋ฐ์ดํฐ ๋ฑ ์๋ก์ด ํค๊ฐ ์ถ๊ฐ๋ ์ ์๋ ๊ฒ์๋ ์ฌ์ฉ์ด ์ด๋ ค์
- ๊ตฌ๋ฌธ ์ฐ์ต: https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
๐ฉ DB์์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
SELECT * FROM ์ด๋ฆ;
SELECT * FROM Customers;
๐ฉ ์ํ๋ ์ปฌ๋ผ์ ๊ฐ๋ง ๊ฐ์ ธ์ค๊ธฐ
SELECT ์ปฌ๋ผ๋ช FROM table;
SELECT City, Country FROM Customers;
๐ฉ DB์๋ ์ํฅ์์ด ์ํ๋ ๋ฐ์ดํฐ ์ถ๊ฐํด์ ๊ฐ์ ธ์ค๊ธฐ
SELECT ์ปฌ๋ผ๋ช , ์ํ๋ ๋ฐ์ดํฐ FROM table;
SELECT City, 1, 'test', NULL FROM Customers
๐ฉ ์ํ๋ ์กฐ๊ฑด์ ์ถฉ์กฑํ๋ Row๋ง ๊ฐ์ ธ์ค๊ธฐ
SELECT * FROM table WHERE ์กฐ๊ฑด
SELECT * FROM OrderDetails WHERE Quantity>5;
๐ฉ Row๋ฅผ ์ ๋ ฌํด์ ๊ฐ์ ธ์ค๊ธฐ
ORDER BY
SELECT * FROM Customers ORDER BY ContactName;
SELECT * FROM Customers ORDER BY ContactName DESC;
SELECT * FROM Customers ORDER BY CustomerName ASC, ContactName DESC;
๐ฉ Row์ ๊ฐ์ ์ง์ ํ๊ธฐ
LIMIT ์ํ๋ Row ์๋ฒ, ์๋ฅด๋ ๊ฐ์
SELECT * FROM Customers LIMIT 10;
SELECT * FROM Customers LIMIT 30 10;
๐ฉ Column ๋ช ์ ๋ณ๊ฒฝํด์ ๊ฐ์ ธ์ค๊ธฐ
AS
SELECT CustomerId AS id, CustomerName AS name FROM Customers;
๐ฉ ํ ์ด๋ธ ํฉ์น๊ธฐ
JOIN ON
SELECT * FROM Products P JOIN Categories C ON P.CategoryID = P.CategoryID;
๐ Local์ mysql DB ๊ตฌ์ถํ๊ธฐ
- [ํฐ๋ฏธ๋] mysql -u root -p
- [ํฐ๋ฏธ๋] show databases;
- [ํฐ๋ฏธ๋] use sakila;
- [ํฐ๋ฏธ๋] show tables;
๐ฉ MySQL workbench
DB ์์ฑ: create schema `MyDB` default character set utf8mb4 collate utf8mb4_general_ci;
TABLE ์์ฑ: create table visitor ( id int primary key, counts int );
DATA ์ฝ์ ํ๊ธฐ: insert into visitor ( id, counts ) values ( 1, 0 );
DATA ์ญ์ : delete from visitor where id = 2;
DATA ์์ : update visitor set counts = counts + 1 where id = 1;
TABLE ์์ ๋ฐ ์ญ์
๐ DB ํต์ ์ฉ ์๋ฒ ๊ตฌ์ถํ๊ธฐ
- ๋ฆฌ์กํธ ํ๋ก์ ํธ ์ต์๋จ์ data-server ํด๋ ๋ง๋ค๊ธฐ
- [ํฐ๋ฏธ๋] npm i express cors mysql
๐ฉ express ์๋ฒ ์ฝ๋ ์์ฑ
// data-server/server.js
const express = require('express');
const cors = require('cors');
const PORT = 4000;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
const dataRouter = require('./routes/data');
app.use('/data', dataRouter);
app.listen(PORT, () => {
console.log(`๋ฐ์ดํฐ ํต์ ์๋ฒ๊ฐ ${PORT}์์ ์๋ ์ค`);
});
๐ฉ MySQL ์๋ฒ ํต์ ์ฉ ๋ชจ๋ ์์ฑ
// data-server/dbConnect.js
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ' ',
port: '3306',
database: 'MyDB',
});
connection.connect();
module.exports = connection;
๐ฉ DB ํต์ ์ฉ ์ปจํธ๋กค๋ฌ ์์ฑ
// data-server/controllers/dataController.js
const connection = require('../dbConnect');
const db = {
getCounts: (cb) => {
connection.query('SELECT counts FROM MyDB.visitor;', (err, data) => {
if (err) throw err;
cb(data);
});
},
incCounts: (cb) => {
connection.query('UPDATE visitor SET counts = counts + 1 WHERE id = 1;', (err) => {
if (err) throw err;
cb(JSON.stringify('์
๋ฐ์ดํธ ์ฑ๊ณต'));
});
},
};
module.exports = db;
๐ฉ ์ฃผ์๋ณ routing ์ฒ๋ฆฌ
// data-server/routes/data.js
const express = require('express');
const router = express.Router();
const db = require('../controllers/dataController');
router.get('/count', (req, res) => {
db.getCounts((data) => {
res.send(data);
});
});
router.post('/inccount', (req, res) => {
db.incCounts((msg) => {
res.send(msg);
});
});
module.exports = router;
๐ฉ React ์ฑ์์ ๋ฐ์ดํฐ ์์ฒญ
import { useDispatch } from 'react-redux';
import styled from 'styled-components';
import OrangeButton from './OrangeButton';
import { next } from '../store/modules/mbti';
import { useEffect, useState } from 'react';
export default function Start() {
const [counts, setCounts] = useState(0);
const dispatch = useDispatch();
useEffect(() => {
async function fetchData() {
const resCount = await fetch('http://localhost:4000/data/count');
if (resCount.status === 200) {
const num = await resCount.json();
if (num[0].counts !== 0) setCounts(num[0].counts);
} else throw new Error('ํต์ ์ด์');
}
fetchData();
}, [counts]);
return (
<>
<Header>๊ฐ๋ฐ์ MBTI ์กฐ์ฌ</Header>
<MainImg src="/images/main.jpg" alt="๋ฉ์ธ ์ด๋ฏธ์ง" />
<SubHeader>
๊ฐ๋ฐ์๊ฐ ํํ ์ ํ๋ ์ํฉ์ ๋ฐ๋ผ์ MBTI๋ฅผ ์์๋ด
์๋ค! ์ง๊ธ๊น์ง{'\n\n'}
{counts} ๋ช
์ด ์ฐธ์ฌํด์ฃผ์
จ์ต๋๋ค.
</SubHeader>
<OrangeButton text="ํ
์คํธ ์์" clickEvent={() => dispatch(next())} />
</>
);
}
const MainImg = styled.img`
width: inherit;
`;
const Header = styled.p`
font-size: 3em;
`;
const SubHeader = styled.p`
font-size: 1.5em;
color: #777;
`;
* ํต์ ์๋ฌ ์ ๋์ฒ
alter user 'root'@'localhost' identified with mysql_native_password BY ' ๋น๋ฐ๋ฒํธ ';
๐ฉ ๋ง์ง๋ง ํ์ด์ง์์ count + 1 ์์ฒญ
// src/components/Show.js
export default function Show() {
const result = useSelector((state) => state.mbti.mbtiResult);
const explanation = useSelector((state) => state.mbti.explanation[result]);
const dispatch = useDispatch();
const incCount = async () => {
const resInc = await fetch('http://localhost:4000/data/inccount', {
method: 'POST',
});
if (resInc.status === 200) console.log(await resInc.json());
else throw new Error('ํต์ ์ด์');
};
return (
<>
<Header>๋น์ ์ ๊ฐ๋ฐ์ MBTI ๊ฒฐ๊ณผ๋?</Header>
<Explanation>{explanation.text}</Explanation>
<Result>{result}</Result>
<Additional>์ด๊ฑด ์ฌ๋ฏธ๋ก ์ฝ์ด ๋ณด์ธ์!</Additional>
<AdditionalImg src={explanation.img} alt="ํฉํญ" />
<OrangeButton
text="๋ค์ ๊ฒ์ฌํ๊ธฐ"
clickEvent={() => {
incCount();
dispatch(reset());
}}
/>
</>
);
}
'KDT > TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
10/17 TIL : MySQL ์ ๊ทํ, Mbti-App์ MongoDB ์ ์ฉ (0) | 2022.10.17 |
---|---|
10/12 TIL : React SPA ์ ์(Mbti-app) (0) | 2022.10.17 |
10/7 TIL : Redux, ๊ฐ๋จํ Todo-list ๋ง๋ค๊ธฐ (0) | 2022.10.14 |
10/5 TIL : React Router (0) | 2022.10.14 |
9/30 TIL : React Hooks(useEffect, useMemo) (0) | 2022.10.14 |