제로베이스 데이터 분석 스쿨 2기] SQL (feat. 정의, User 관리, User 권한 관리, Table 생성, Table 변경&삭제, SELECT, WHERE, UPDATE, DELETE, ORDER BY, Logical operators)

박세우·2024년 1월 19일

Database

주요 용어들

database란, 여러 사람이 공유하여 사용할 목적으로 체계화해 통합, 관리하는 데이터의 집합체

DBMS 란 (Database Management System), 사용자와 데이터베이스 사이에서 사용자의 요구에 따라 정보를 생성해주고 데이터베이스를 관리해주는 소프트웨어

관계형 데이터 베이스란 (RDB: Relational Database), 서로간에 관계가 있는 데이터 테이블들을 모아둔 데이터 저장공간

SQL

SQL이란, 데이터베이스에서 데이터를 정의, 조작, 제어하기 위해 사용하는 언어

SQL 구성

데이터 정의 언어(DDL: Data Definition Language)
-CREAT, ALTER, DROP 등의 명령어

데이터 제어 언어 (DLC: Data Control Languages)
-GRANT, REVOKE, COMMIT, ROLLBACK 등의 명령어

Database 관리

CREAT - 데이터 베이스 생성
ex)CREATE DATABASE testdb;

USE - 해당 데이터베이스로 이동
ex) USE testdb;

DROP - 데이터베이스 삭제
ex) DROP DATABASE dbname;

User 관리

User 생성 - localhost (현재 PC에서 접근 가능한 계정)
현재 PC에서만 접속 가능한 사용자를 비밀번호화 함께 생성
CREAT USER 'username'@'localhost' identified by 'password';

현재 PC에서 접근 가능한 계정 삭제
DROP USER 'noma'@'localhost'

User 생성 - % (외부에서 접속 가능)
CREAT USER 'username'@%'localhost' identified by 'password';

외부 유저 계정 삭제
DROP USER 'noma'@'%'

User권한 관리

user 권한 확인
사용자에게 부여된 모든 권한 목록을 확인
SHOW GRANTS FOR 'username'@'localhost';

사용자에게 특정 데이터베이스의 모든 권한을 부여
GRANT ALL ON dbname.* to 'username'@'localhost';

사용자에게 특정 데이터베이스의 모든 권한을 삭제
REVOKE ALL ON dbname.* from 'username'@'localhost';

Table

Table이란, 데이터베이스 안에서 실제 데이터가 저장되는 형태이고, 행(row)과 열(column)로 구성된 데이터 모음

Table 생성
create table tablename
(column datatype, column datatype);

Table 목록 확인 문법
show tables;

Table 정보 확인 문법 (description)
desc tablename;

Table 이름 변경
alter table tablename
rename new_table;

Table Column 추가
alter table tablename
add column columnname datatype;

Table Column 데이터타입 변경
alter table tablename
modify column columnname datatype;

Table Column 이름 변경
alter table tablename
chage column old_columnname, new_columnname, new_datatype;

Table Column 삭제
alter table tablename
drop column columnname;

insert - 데이터를 추가하는 명령어
입력한 컬럼 이름의 순서와 값의 순서가 일치하도록 주의!
insert into tablename (col1, col2,...)
values (value1, value2,....);

모든 컬럼값을 추가하는 경우
모든 컬럼값을 추가하는 경우에는 다음과 같이 컬럼 이름을 지정하지 않아도 되지만, 입력하는 값의 순서가 테이블의 컬럼 순서와 일치하도록 주의!
insert into tablename
values (val1, val2, ...);

select - 데이터를 조회하는 명령어
태이블 내의 특정 칼럼에 대한 데이터를 조회
select column1, column2, ...
from tablename;

테이블 내의 모든 컬럼에 대한 데이터를 조회하는 경우
select *
from tablename;

where - 테이블 내에서 조건을 만족하는 데이터 조회
select column1, column2, ...
from tablename
where condition;

update - 데이터를 수정하는 명령어
update tablename
set column1 = value1, column2 = value 2, ...
where condition;

delete - 데이터를 삭제하는 명령어
delete from tablename
where condition;

order by - select 문에서 데이터를 특정 컬럼을 기준으로 오름차순 혹은 내림차순 정렬
asc (Ascending): 오름차순 정렬
desc(descending): 내림차순 정렬

select col1, col2
from tablename
order by col1, col2, acs | desc;

Logical operators

And 문법
조건을 모두 만족하는 경우 True
select col1, col2
from tablename
where condition1 AND condition2 ..

OR 문법
하나의 조건이라도 만족하는 경우 TRUE
select col1, col2
from tablename
where condition1 or condition2 ..

NOT 문법
조건을 만족하지 않는 경우 True
select col1, col2
from tablename
where not condition

Between 문법
조건값이 범위 사위에 있으면 True
select col1, col2
from tablename
where col1 BETWEEN value1 AND value2;

In 문법
목록 안에 조건이 존재하는 경우 True
select col1, col2
from tablename
where column IN (value1, value2);

Like 문법
조건값이 패턴에 맞으면 True
select col1, col2
from tablename
where column like pettern;

#소속사 이름이 'YG엔터테이먼트'인 데이터  검색
select * from celeb where agency like 'YG엔터테이먼트';

#'YG'로 시작하는 소속사 이름을 가진 데이터 검색
select * from celeb where agency like 'YG%';

#'엔터테이먼트'로 끝나는 소속사 이름을 가진 데이터를 검색
select * from celeb where agency like '%엔터테이먼트';

#직업명에 '가수'가 포함된 데이터를 검색
select * from celeb where job_title like '%가수%';

#소속사 이름의 두 번째 글자가 G인 데이터를 검색
select * from celeb where agency like '_G%';

#직업명이 '가'로 시작하고 최소 2글자 이상인 데이터 검색
select * from celeb job_title like '가_%';


#직업명이 '가'로 시작하고 최소 5글자 이상인 데이터 검색
select * from celeb job_title like '가____%';

#직업명이 '영'으로 시작하고 '모델'로 끝나는 데이터 검색
select * from celeb where job_title like '영%모델';



profile
최고의 데이터 분석가를 목표로 하는 박세우입니다.

0개의 댓글