C:\Program Files\MySQL\MySQL Server 8.0\bin
기본 설치 위치는 다음과 같음ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
# mysql에 root 유저를 비밀번호로 들어가겠다!
mysql -u root -p
SHOW DATABASES;
# 데이터 베이스가 있어야 함
USE sakila;
# CREATE DATABASE 테이블 이름;
CREATE DATABASE testdb;
# CREATE DATABASE 테이블 이름;
DROP DATABASE testdb;
SHOW TABLE;
# SELECT * FROM 테이블 이름 LIMIT 갯수;
# 전체를 가져와라, 필름에 있는, 10개만
SELECT
*
FROM
film
LIMIT
10;
# 전체 갯수를 가져와라 필름에 있는
SELECT
COUNT(1)
FROM
film;
# 유저 생성
# % == 외부 접속 가능
# CREATE user '유저 이름'@'접속 위치' IDENTIFIED BY '비밀번호';
CREATE user 'testUser'@'%' IDENTIFIED BY '0000';
# 유저 권한 부여
GRANT ALL PRIVILEGES ON testdb.* TO testUser@'%';
# 유저 권한 부여와 유저 생성을 동시에
GRANT ALL PRIVILEGES ON testdb.* TO testUser@'%' IDENTIFIED BY '0000';
# 유저 삭제
DELETE
FROM
user
WHERE
user='testUser';
# 테이블 이름 설정
CREATE TABLE member(
# 큰 숫자, 자동 증가, null 허용 안함, 기본키
id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
# 글자 10개
member_id VARCHAR(10),
name VARCHAR(10),
address VARCHAR(10),
phone_number VARCHAR(20),
# 날짜 정보
create_date DATETIME,
update_date DATETIME
);
# 잘만들어 졌늕 ㅣ확인
DESC member
# 생성
INSERT INTO member(
) VALUES (
'member1',
'member',
'seoul',
'010-xxxx-xxxx',
NOW(),
NOW()
);
# address 없음
INSERT INTO member(
member_id,
name,
phone_number,
create_date,
update_date
) VALUES (
'member2',
'member',
'010-xxxx-xxxx',
NOW(),
NOW()
);
# id만
INSERT INTO member(
member_id,
create_date,
update_date
) VALUES (
'member3',
NOW(),
NOW()
);
# 복수개 생성 가능
INSERT INTO testdb.member
( member_id, name, address, phone_number, create_date, update_date)
VALUES
('member1', 'A', 'Seoul', '010-1111-1111', NOW(), NOW()),
('member2', 'B', 'Seoul', '010-1111-2222', NOW(), NOW()),
('member3', 'C', 'Daegu', '010-1111-3333', NOW(), NOW()),
('member4', 'D', 'Daegu', '010-1111-4444', NOW(), NOW()),
('member5', 'D', 'Daegeon', '010-1111-5555', NOW(), NOW()),
('member6', 'E', 'Daegeon', '010-2222-1111', NOW(), NOW()),
('member7', 'F', 'Daegeon', '010-2222-1111', NOW(), NOW()),
('member8', 'F', 'Seoul', '010-2222-2222', NOW(), NOW());
# 읽기
# SELECT 컬럼 FROM 테이블 WHERE 조건;
SELECT
*
FROM
product
WHERE
qty > 10;
# 수정
# id가 '2'인 row의 address 속성을 부산으로 변경
# UPDATE 테이블 SET 컬럼1 = 값1, 컬럼2 = 값2, WHERE 조건;
UPDATE
member
SET
address = 'busan'
WHERE
id = '2';
# 다양한 연산이 가능함
# 동일 테이블 타 컬럼 사용 가능
# 타 테이블 타 컬럼 사용 가능(잘 묶여 있어야 함)
UPDATE
product
SET
qty - 5 / price
WHERE
name = 'apple';
# 삭제
# DELETE FROM 테이블 WHERE 조건;
# 위 기능을 극도로 꺼려짐(사용하지 말자!)
# is_use 또는 is_delete columns을 추가해서 삭제함
DELETE
FROM
member
WHERE
id = 3;
PK 이면서 FK인 경우?
가능?
# ORDER BY [컬럼] [ASC | DESC]
SELECT
*
FROM
product
ORDER BY
price;
SELECT
*
FROM
product
LIMIT
3;
SELECT
DISTINCT
price
FROM
product;
SELECT
SUM(qty), AVG(qty), MAX(qty), MIN(qty), COUNT(qty)
FROM
product;
# 집계함수를 사용했다면
SELECT
# 이름, 합, 평균, 최대, 최소, 갯수, 가격
name, SUM(qty) AS 'sumQty', AVG(qty), MAX(qty), MIN(qty), COUNT(qty), price
FROM
product
WHERE
price > 1000
GROUP BY
name, price # select 값을 가져옴
HAVING
sum(qty) > 10; # 그룹 값에 조건 걸기
# 테이블 이름 설정
CREATE TABLE product(
id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(10),
qty INT ,
price INT ,
create_date DATETIME,
update_date DATETIME
);
INSERT INTO testdb.product
(name, qty, price, create_date, update_date)
VALUES
('carrot', 10, 1000, NOW(), NOW()),
('apple', 100, 500, NOW(), NOW()),
('pear', 30, 800, NOW(), NOW()),
('orange', 50, 800, NOW(), NOW()),
('honey', 10, 3000, NOW(), NOW()),
('cabage', 15, 3000, NOW(), NOW()),
('pine', 20, 5000, NOW(), NOW()),
('mellon', 10, 10000, NOW(), NOW());
SELECT
id, member_id, name, address, phone_number, create_date, update_date
FROM
member
WHERE
address = 'Daegu';
SELECT
name
FROM
member
WHERE
phone_number= '010-1111-1111';
SELECT
member_id, phone_number
FROM
member
WHERE
address = 'Daegeon';
SELECT
price
FROM
product
WHERE
name = 'carrot';
SELECT
id, name, qty, price, create_date, update_date
FROM
product
WHERE
price > 3000;
D
로 시작하는 address를 가진 member 테이블의 이름 검색SELECT
name
FROM
member
WHERE
address LIKE 'D%';
SELECT
member_id, address
FROM
member
WHERE
phone_number LIKE '%1111';
SELECT
name
FROM
product
WHERE
qty > 20
AND price >= 2000;
SELECT
name
FROM
member
WHERE
address = 'Seoul' OR
phone_number LIKE '%2222%';
UPDATE
product
SET
qty = qty + 5;
UPDATE
product
SET
price = price + 200
WHERE
name = 'orange';
UPDATE
member
SET
address = 'Busan'
WHERE
member_id= 'member8';
DELETE
FROM
product
WHERE
name= 'cabage';
SELECT
*
FROM
member
ORDER BY
address
DESC;
SELECT
*
FROM
product
WHERE
qty >= 20
ORDER BY
price
DESC
LIMIT
3;
SELECT
DISTINCT
address
FROM
member;
address
가 Daegeon
인 member
를 name
순서대로 정렬SELECT
*
FROM
member
WHERE
address = 'Daegeon'
ORDER BY
name;
SELECT
SUM(price) AS sumPrice
FROM
product;
SELECT
AVG(qty) AS avgQty
FROM
product;
SELECT
qty, COUNT(price) AS countPrice
FROM
product
GROUP BY
qty;