SQL κΈ°λ³Έ λͺ λ Ήμ΄
1.
λ°μ΄ν° μ μμ΄(DDL) : ν
μ΄λΈ μμ±, λ³κ²½, μμ
2.
λ°μ΄ν° μ‘°μμ΄(DML) : λ°μ΄ν° μ½μ
, μ‘°ν, μμ , μμ β μ μΌ λ§μ΄ μΈ λͺ
λ Ήμ΄
3.
λ°μ΄ν° μ μ΄μ΄(DCL) : λ°μ΄ν° μ κ·Ό κΆν λΆμ¬, μ κ±°
4.
νΈλμ μ
μ μ΄μ΄(TCL) : λ°μ΄ν° μ‘°μμ΄(DML) λͺ
λ Ήμ΄ μ€ν, μ·¨μ, μμμ μ₯
λ°μ΄ν° μ μμ΄(DDL; Data Definition Language)
CREATE : μ€ν€λ§, λλ©μΈ, ν
μ΄λΈ, λ·°, μΈλ±μ€ μμ±(μ μ)
ALTER : ν
μ΄λΈμ λν μ μλ₯Ό λ³κ²½νλ λ° μ¬μ©
DROP : μ€ν€λ§, λλ©μΈ, ν
μ΄λΈ, λ·°, μΈλ±μ€ μμ
1.
ν
μ΄λΈ μμ±(CREATE)
2.
μ΄ μΆκ°(ALTER)
3.
μ΄ λ°μ΄ν° νμ
λ³κ²½(ALTER)
4.
ν
μ΄λΈ λͺ
λ³κ²½(ALTER)
5.
ν
μ΄λΈ μμ (DROP)
컬λΌμ λ°μ΄ν° νμ
컬λΌμ μ μ½ μ‘°κ±΄
/* Practice λ°μ΄ν°λ² μ΄μ€ μμ± */
CREATE DATABASE Practice;
/* Practice λ°μ΄ν°λ² μ΄μ€ μ¬μ© */
USE Practice;
/* ν
μ΄λΈ μμ± (Create) */
CREATE TABLE νμν
μ΄λΈ (
νμλ²νΈ INT PRIMARY KEY,
μ΄λ¦ VARCHAR(20),
κ°μ
μΌμ DATE NOT NULL,
μμ λμ BIT
);
/* ν
μ΄λΈ μ΄ μΆκ° */
ALTER TABLE νμν
μ΄λΈ ADD μ±λ³ VARCHAR(2);
/* μ΄ λ°μ΄ν° νμ
λ³κ²½ */
ALTER TABLE νμν
μ΄λΈ MODIFY μ±λ³ VARCHAR(20);
/* μ΄ μ΄λ¦ λ³κ²½ */
ALTER TABLE νμν
μ΄λΈ CHANGE μ±λ³ μ± VARCHAR(2);
/* ν
μ΄λΈλͺ
λ³κ²½ */
ALTER TABLE νμν
μ΄λΈ RENAME νμμ 보;
/* ν
μ΄λΈ μμ */
DROP TABLE νμμ 보;
SQL
볡μ¬
λ°μ΄ν° μ‘°μμ΄(DML; Data Manipulation Language)
SELECT : ν
μ΄λΈμμ 쑰건μ λ§λ νμ κ²μνλ€
INSERT: ν
μ΄λΈμ μλ‘μ΄ νμ μ½μ
DELETE : ν
μ΄λΈμμ 쑰건μ λ§λ νμ μμ
UPDATE : ν
μ΄λΈμμ 쑰건μ λ§λ νμ λ΄μ©μ λ³κ²½
DELETE, UPDATE μ, WHERE 쑰건μ μ μ¬μ©ν΄μ μνλ νλ§ μμ ,λ³κ²½ν μ μλ€.
/* Practice λ°μ΄ν°λ² μ΄μ€ μ¬μ©*/
USE Practice;
/***************ν
μ΄λΈ μμ±(Create)***************/
/* νμν
μ΄λΈ μμ± */
CREATE TABLE νμν
μ΄λΈ (
νμλ²νΈ INT PRIMARY KEY,
μ΄λ¦ VARCHAR(20),
κ°μ
μΌμ DATE NOT NULL,
μμ λμ BIT
);
/***************λ°μ΄ν° μ½μ
*******************/
INSERT INTO νμν
μ΄λΈ VALUES (1001, 'νκΈΈλ', '2020-01-02', 1);
INSERT INTO νμν
μ΄λΈ VALUES (1002, 'μ΄μμ ', '2020-01-03', 0);
INSERT INTO νμν
μ΄λΈ VALUES (1003, 'μ₯μμ€', '2020-01-04', 1);
INSERT INTO νμν
μ΄λΈ VALUES (1004, 'μ κ΄μ', '2020-01-05', 0);
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/***************쑰건 μλ°*******************/
/* PRIMARY KEY μ μ½ μ‘°κ±΄ μλ° */
INSERT INTO νμν
μ΄λΈ VALUES (1004, 'μ₯λ³΄κ³ ', '2020-01-06', 0);
/* NOT NULL μ μ½ μ‘°κ±΄ μλ° */
INSERT INTO νμν
μ΄λΈ VALUES (1005, 'μ₯λ³΄κ³ ', NULL, 0);
/* λ°μ΄ν° νμ
쑰건 μλ° */
INSERT INTO νμν
μ΄λΈ VALUES (1005, 'μ₯λ³΄κ³ ', 1, 0);
/***************λ°μ΄ν° μ‘°ν***************/
/* λͺ¨λ μ΄ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* νΉμ μ΄ μ‘°ν */
SELECT νμλ²νΈ, μ΄λ¦ FROM νμν
μ΄λΈ;
/* νΉμ μ΄ μ΄λ¦ λ³κ²½νμ¬ μ‘°ν (μ‘°νν λλ§ μ΄λ¦ μ΄μ μ±λͺ
μΌλ‘ λ³κ²½ν΄μ μ‘°ν) */
SELECT νμλ²νΈ, μ΄λ¦ AS μ±λͺ
FROM νμν
μ΄λΈ;
/***************λ°μ΄ν° μμ *******************/
/* λͺ¨λ λ°μ΄ν° μμ */
UPDATE νμν
μ΄λΈ SET μμ λμ = 0;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* νΉμ 쑰건 λ°μ΄ν° μμ */
UPDATE νμν
μ΄λΈ SET μμ λμ = 1 WHERE μ΄λ¦ = 'νκΈΈλ';
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/***************λ°μ΄ν° μμ *******************/
/* νΉμ λ°μ΄ν° μμ */
DELETE FROM νμν
μ΄λΈ WHERE μ΄λ¦ = 'νκΈΈλ';
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* λͺ¨λ λ°μ΄ν° μμ */
DELETE FROM νμν
μ΄λΈ;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
SQL
볡μ¬
λ°μ΄ν° μ μ΄μ΄(DCL; Data Control Language)
DBA(λ°μ΄ν°λ² μ΄μ€ κ΄λ¦¬μ)κ° DB κ΄λ¦¬μ κΆν κ΄λ¦¬λ₯Ό λͺ©μ μΌλ‘ μ¬μ©
GRANT : DBμ μ¬μ©μμκ² νΉμ μμ
μ λν κΆν λΆμ¬
REVOKE : μ¬μ©μμκ² νΉμ μμ
μ λν κΆν λ°ν
/***************μ¬μ©μ νμΈ***************/
/* MYSQL λ°μ΄ν°λ² μ΄μ€ μ¬μ© */
USE MYSQL;
/* μ¬μ©μ νμΈ */
SELECT *
FROM USER;
/***************μ¬μ©μ μΆκ°***************/
/* μ¬μ©μ μμ΄λ λ° λΉλ°λ²νΈ μμ± */
CREATE USER 'TEST'@LOCALHOST IDENTIFIED BY 'PASSWORD';
/* μ¬μ©μ νμΈ */
SELECT * FROM USER;
/* μ¬μ©μ λΉλ°λ²νΈ λ³κ²½ */
SET PASSWORD FOR 'TEST'@LOCALHOST = '1234';
/***************κΆν λΆμ¬ λ° μ κ±°***************/
/** κΆν: CREATE, ALTER, DROP, INSERT, DELETE, UPDATE, SELECT λ± **/
/* νΉμ κΆν λΆμ¬ */
GRANT SELECT, DELETE ON PRACTICE.νμν
μ΄λΈ TO 'TEST'@LOCALHOST;
/* νΉμ κΆν μ κ±° */
REVOKE DELETE ON PRACTICE.νμν
μ΄λΈ FROM 'TEST'@LOCALHOST;
/* λͺ¨λ κΆν λΆμ¬ */
GRANT ALL ON Practice.νμν
μ΄λΈ TO 'TEST'@LOCALHOST;
/* λͺ¨λ κΆν μ κ±° */
REVOKE ALL ON Practice.νμν
μ΄λΈ FROM 'TEST'@LOCALHOST;
/***************μ¬μ©μ μμ ***************/
/* μ¬μ©μ μμ */
DROP USER 'TEST'@LOCALHOST;
/* μ¬μ©μ νμΈ */
SELECT * FROM USER;
SQL
볡μ¬
νΈλμμ μ μ΄μ΄(TCL; Transaction Control Language)
β’
νΈλμμ
μ΄λ?
BEGIN λͺ
λ Ήμ΄λ₯Ό μ¬μ©νμ¬ λ§λ λΆν ν μ μλ SQL μμ
λ¨μλ₯Ό νΈλμμ
μ΄λΌκ³ νλ€.
COMMIT : λͺ¨λ νΈλμμ
μμ
μ μ΅μ’
μ€ν
ROLLBACK : λͺ¨λ νΈλμμ
μμ
μ λλλ¦Ό(μ·¨μ)
SAVEPOINT : νΈλμμ
νμ μμ
λ΄μμ ROLLBACK μ μ₯μ μ μ§μ νλ λͺ
λ Ήμ΄
νΈλμμ
μμ
κ³Ό μΌλ° μμ
μ°¨μ΄μ ?
νΈλμμ
COMMIT νλ μμ μ λͺ¨λ μμ
λ€μ΄ νκΊΌλ²μ μνλλ κ²μ΄ μλλΌ, μΌλ°μ μΈ λͺ
λ Ήμ΄μ²λΌ λͺ
λ Ήμ΄κ° μ€νλλ©΄ μμ
μ΄ μνλλ€.
νμ§λ§ ROLLBACK μ ν μ μλ€λ μ₯μ μΌλ‘ μ¬μ©νλ κ² κ°λ€.Β
/* Practice λ°μ΄ν°λ² μ΄μ€ μ¬μ©*/
USE Practice;
/***************ν
μ΄λΈ μμ±(Create)***************/
/* (νμν
μ΄λΈ μ‘΄μ¬ν μ, νμν
μ΄λΈ μμ ) */
DROP TABLE νμν
μ΄λΈ;
/* νμν
μ΄λΈ μμ± */
CREATE TABLE νμν
μ΄λΈ (
νμλ²νΈ INT PRIMARY KEY,
μ΄λ¦ VARCHAR(20),
κ°μ
μΌμ DATE NOT NULL,
μμ λμ BIT
);
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/***************BEGIN + μ·¨μ(ROLLBACK)*******************/
/* νΈλμ μ
μμ */
BEGIN;
/* λ°μ΄ν° μ½μ
*/
INSERT INTO νμν
μ΄λΈ VALUES (1001, 'νκΈΈλ', '2020-01-02', 1);
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* μ·¨μ */
ROLLBACK;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/***************BEGIN + μ€ν(COMMIT)*******************/
/* νΈλμ μ
μμ */
BEGIN;
/* λ°μ΄ν° μ½μ
*/
INSERT INTO νμν
μ΄λΈ VALUES (1005, 'μ₯λ³΄κ³ ', '2020-01-06', 1);
/* μ€ν */
COMMIT;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/***************μμ μ μ₯(SAVEPOINT)*******************/
/* (νμν
μ΄λΈμ λ°μ΄ν° μ‘΄μ¬ν μ, λ°μ΄ν° λͺ¨λ μμ ) */
DELETE FROM νμν
μ΄λΈ;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* νΈλμ μ
μμ */
BEGIN;
/* λ°μ΄ν° μ½μ
*/
INSERT INTO νμν
μ΄λΈ VALUES (1005, 'μ₯λ³΄κ³ ', '2020-01-06', 1);
/* SAVEPOINT μ§μ */
SAVEPOINT S1;
/* 1005 νμ μ΄λ¦ μμ */
UPDATE νμν
μ΄λΈ
SET μ΄λ¦ = 'μ΄μμ ';
/* SAVEPOINT μ§μ */
SAVEPOINT S2;
/* 1005 νμ λ°μ΄ν° μμ */
DELETE
FROM νμν
μ΄λΈ;
/* SAVEPOINT μ§μ */
SAVEPOINT S3;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* SAVEPOINT S2 μ μ₯μ μΌλ‘ ROLLBACK */
ROLLBACK TO S2;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
/* μ€ν */
COMMIT;
/* νμν
μ΄λΈ μ‘°ν */
SELECT * FROM νμν
μ΄λΈ;
SQL
볡μ¬