Database 정의
Database란?
DBMS란?(Database Management System)
관계형 데이터베이스 란?(RDB:Relational Database)
SQL 이란? (Structured Query Language)
% mysql -u root -p
Enter password:
SHOW DATABASES;
CREATE DATABASE dbname;
CREATE DATABASE testdb;
USE dbname;
USE testdb;
DROP DATABASE dbname;
DROP DATABASE testdb;
use mysql;
SELECT host, user FROM user;
CREATE USER 'usernama'@'localhost' identified by 'password';
# 생성한다. 유저를 usernama의 유저를 localhost(현재 피씨)에서만 접속 가능하다 / 비밀번호는? by '이걸로..'
CREATE USER 'noma'@'localhost' identified by '1234';
CREATE USER 'usernama'@'%' identified by 'password';
CREATE USER 'noma'@'%' identified by '5678';
DROP USER 'usernama'@'localhost'
DROP USER 'usernama'@'%'
DROP USER 'noma'@'%'
DROP USER 'noma'@'localhost'
CREATE DATABASE testdb;
CREATE USER 'noma'@'localhost' identified by '1234';
SHOW GRANTS FOR 'username'@'localhost';
SHOW GRANTS FOR 'noma'@'localhost';
GRANT ALL ON dbname.* to 'username'@'localhost';
GRANT ALL ON testdb.* to 'noma'@'localhost';
# 권한 확인 : SHOW GRANTS FOR 'noma'@'localhost';
FLUSH PRIVILEGES;
REVOKE ALL ON dbname.* from 'username'@'localhost';
REVOKE ALL ON testdb.* from 'noma'@'localhost';
# 권한 확인 : SHOW GRANTS FOR 'noma'@'localhost';
create database zerobase default character set utf8mb4;
데이터베이스 안에서 실제 데이터가 저장되는 형태이고, 행(Row)과 열(Column)로 구성된 데이터 모음
Table 생성 문법
create table tablename
(
columnname datatype,
columnname datatype,
...
)
create table mytable(id int, name varchar(16));
show tables;
desc mytable;
alter table tablename
rename new_tablename
alter table mytable rename person;
alter table tablename
add column columnname datatype;
alter table person add column agee double;
alter table tablename
modify column columnname datatype;
alter table person
modify column agee int;
alter table tablename
change column old_columnname new_columnname new_datatype;
alter table person
change column agee age int;
alter table tablename
drop column columnname;
alter table person
drop column age;
drop table tablename;
drop table person;
USE zerobase;
CREATE TABLE person
(
id int,
name varchar(16),
age int,
sex CHAR
);
DESC person;
INSERT INTO tablename (column1, column2, ...)
VALUES (value1, value2, ...);
INSERT INTO person (id, name, age, sex)
VALUES (1, '이효리', 43, 'F');
INSERT INTO tablename
VALUES (value1, value2, ...);
INSERT INTO person
VALUES (2, '이상순', 48, 'M');
SELECT column1, column2, ...
FROM tablename;
SELECT name, age, sex FROM person;
SELECT *
FROM tablename;
SELECT *
FROM person;
WHERE - 조건 : SQL 문에 조건을 추가하며 SELECT 뿐만 아니라 UPDATE 와 DELETE 에도 사용
테이블 내에서 조건을 만족하는 데이터 조회
SELECT column1, column2, ...
FROM tablename
WHERE condition;
SELECT *
FROM person
WHERE sex='F';
UPDATE tablename # 수정할꺼야!
SET column1 = value1, column2 = value2, ... # 이렇게 변경할꺼야!
WHERE condition; # 이조건에 만족하는 데이터에 한해서!
UPDATE person
SET sge = 23
WHERE name='이효리';
DELETE FROM tablename # 삭제할꺼야!
WHERE condition; # 이조건에 만족하는 데이터에 한해서!
DELETE FROM person
WHERE name = '이상순';
use zerobase;
create table celeb
(
id int not null auto_increment primary key,
name varchar(32) not null default '',
brithday date,
age int,
sex char(1),
job_title varchar(32),
agency varchar(32)
);
desc celeb;
insert into celeb values (1, '아이유', '1993-05-16', 29, 'F', '가수, 텔런트', 'EDAM엔터테이먼트');
insert into celeb values (2, '이미주', '1994-09-23', 28, 'F', '가수', '울림엔터테이먼트');
insert into celeb values (3, '송강', '1994-04-23', 28, 'M', '텔런트', '나무엑터스');
insert into celeb values (4, '강동원', '1981-01-18', 41, 'M', '영화배우, 텔런트', 'YG엔터테이먼트');
insert into celeb values (5, '유재석', '1972-08-14', 50, 'M', 'MC, 개그맨', '안테나');
insert into celeb values (6, '차승원', '1970-06-07', 48, 'M', '영화배우, 모델', 'YG엔터테이먼트');
insert into celeb values (7, '이수현', '1999-05-04', 23, 'F', '가수', 'YG엔터테이먼트');
select column1, column2, ...
from tablename
order by column1, column2, ... asc | desc;
# asc | desc 둘중하나를 선택하여 정렬하기
select age, name
from celeb
order by age asc;
# 기본값이 오름차순이라 asc는 생략가능
select age, name
from celeb
order by age desc;
select age, name
from celeb
order by age, name;
select age, name
from celeb
order by age desc, name asc;
use zerobase;
desc celeb;
select name, age from celeb where age=29 order by age;
select name, age from celeb where age!=29 order by age;
select name, age from celeb where age>29 order by age;
select name, age from celeb where age<29 order by age;
select name, age from celeb where age>=29 order by age;
select name, age from celeb where age<=29 order by age;
select name, age from celeb where age<>29 order by age;
use zerobase;
select * from celeb;
select column1, column2, ...
from tablename
where condition1 AND condition2 AND condition3 ...;
select *
from celeb
where age=29 AND sex='F';
select *
from celeb
where age>40 AND sex='M'
order by name;
# 역순이면 name desc;
select column1, column2, ...
from tablename
where condition1 OR condition2 OR condition3 ...;
select *
from celeb
where age<25 OR age>30
order by age;
select *
from celeb
where (age<29 and sex='F') OR (age>30 and sex='M')
order by age, sex;
select *
from celeb
where (agency = 'YG엔터테이먼트' or agency = '나무엑터스') and age<30;
select column1, column2, ...
from tablename
where not condition;
select *
from celeb
where not sex='F';
select *
from celeb
where (agency='YG엔터테이먼트' and not sex='M') or (job_title='가수' and not agency='YG엔터테이먼트');
select *
from celeb
where (brithday>19891231 and not sex='F') or (brithday<19800101 and not agency='안테나');
select column1, column2, ...
from tablename
where column1 BETWEEN value1 AND value2;
select *
from celeb
where age BETWEEN 20 and 40;
select *
from celeb
where not brithday between 19800101 AND 19951231 and sex='F' or agency='YG엔터테이먼트' and not age between 20 and 45;
select column1, column2, ...
from tablename
where column IN (value1, value2, ...);
# column의 값이 (값)안에 값이 존재하면 True로 반환한다.
select *
from celeb
where age IN (28,48);
select *
from celeb
where not agency IN ('나무엑터스', '안테나', '울림엔터테이먼트')
and (sex='F' or age>=45);
select column1, column2, ...
from tablename
where column LIKE pettern;
select *
from celeb
where agency LIKE 'YG엔터테이먼트';
select *
from celeb
where agency LIKE 'YG%';
select *
from celeb
where agency LIKE '%엔터테이먼트';
select *
from celeb
where job_title LIKE '%가수%';
select *
from celeb
where agency LIKE '_G%';
# 세번째 글자인 경우 __G 처럼 _로 표현
select *
from celeb
where job_title LIKE '가_%';
# 시작을 2글자로 시작하게 만들기위해 가_ 처럼 _(언더바)로 글자수를 표현
select *
from celeb
where job_title LIKE '가____%';
select *
from celeb
where job_title LIKE '영%모델';
select *
from celeb
where job_title LIKE '%영화배우%' and job_title LIKE '%텔런트%';
select *
from celeb
where job_title LIKE '%,%' and
not(job_title LIKE ('%영화배우%') or job_title LIKE ('%텔런트%'));
use zerobase;
select *
from celeb;
create table test1
(
no int
);
create table test2
(
no int
);
insert into test1 values (1);
insert into test1 values (2);
insert into test1 values (3);
insert into test2 values (5);
insert into test2 values (6);
insert into test2 values (3);
select * from test1;
select * from test2;
select column1, column2, ...
from tableA
union | union all
select column1, column2, ...
from tableB;
select * from test1
union all
select * from test2;
select * from test1
union
select * from test2;
select name, sex, agency from celeb where sex='F'
union all
select name, sex, agency from celeb where agency='YG엔터테이먼트';
select name, job_title from celeb where job_title like '%가수%'
union
select name, birthday, age from celeb where age between '1980-01-01' and '1989-12-31';
use zerobase;
select * from celeb;
create table snl_show
(
id int not null auto_increment primary key,
season int not null,
episode int not null,
broadcast_date date,
host varchar(32) not null
);
desc snl_show;
insert into snl_show values (1, 8, 7, '2020-09-05', '강동원');
insert into snl_show values (2, 8, 8, '2020-09-12', '유재석');
insert into snl_show values (3, 8, 9, '2020-09-19', '차승원');
insert into snl_show values (4, 8, 10, '2020-09-26', '이수현');
insert into snl_show values (5, 9, 1, '2021-09-04', '이병헌');
insert into snl_show values (6, 9, 2, '2021-09-11', '하지원');
insert into snl_show values (7, 9, 3, '2021-09-18', '제시');
insert into snl_show values (8, 9, 4, '2021-09-25', '조정석');
insert into snl_show values (9, 9, 5, '2021-10-02', '조여정');
insert into snl_show values (10, 9, 6, '2021-10-09', '옥주현');
select * from snl_show;
JOIN : 두개 이상의 테이블을 결합하는 것
두개의 실습 테이블 확인
두개의 테이블에서 공통된 요소들을 통해 결합하는 조인방식
INNER JOIN 문법
select column1, column2, ...
from tableA
inner join tableB
on tableA.column = tableB.column
where codition;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
inner join snl_show
on celeb.name = snl_show.host;
두개의 테이블에서 공통영역을 포함해 왼쪽 테이블의 다른 데이터를 포함하는 조인방식
LEFT JOIN 문법
select column1, column2, ...
from tableA
left join tableB
on tableA.column = tableB.column
where codition;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
left join snl_show
on celeb.name = snl_show.host;
두개의 테이블에서 공통영역을 포함해 오른쪽 테이블의 다른 데이터를 포함하는 조인방식
RIGHT JOIN 문법
select column1, column2, ...
from tableA
right join tableB
on tableA.column = tableB.column
where codition;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
right join snl_show
on celeb.name = snl_show.host;
두개의 테이블에서 공통영역을 포함하여 양쪽 테이블의 다른영역을 모두 포함하는 조인방식
FULL OUTER JOIN 문법
select column1, column2, ...
from tableA
full outer join tableB
on tableA.column = tableB.column
where codition;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
full outer join snl_show
on celeb.name = snl_show.host;
select column1, column2, ...
from tableA
left join tableB on tableA.column = tableB.column
union
select column1, column2, ...
from tableA
right join tableB on tableA.column = tableB.column
where condition;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
left join snl_show
on celeb.name = snl_show.host
union
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
right join snl_show
on celeb.name = snl_show.host;
select column1, column2, ...
from tableA, tableB, ...
where codition;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb, snl_show
where celeb.name = snl_show.host;
select celeb.name, celeb.job_title
from celeb, snl_show
where celeb.name = snl_show.host and celeb.agency='안테나';
select celeb.name, celeb.age, celeb.job_title, celeb.agency,
snl_show.season, snl_show.episode
from celeb, snl_show
where celeb.name = snl_show.host and
((not job_title like '%영화배우%' and
agency='YG엔터테이먼트') or (age>=40 and
agency != 'YG엔터테이먼트'));
select snl_show.id, season, episode, name, job_title
from celeb, snl_show
where name = host;
select name, season, episode, broadcast_date, agency
from celeb, snl_show
where name = host and
(episode in (7, 9, 10) or agency like 'YG______') and
broadcast_date > '2020-09-15';
use zerobase;
select * from celeb;
select * from snl_show;
select concat('string1', 'string2', ...);
select concat('concat', ' ','test');
select concat('이름 : ', name,) from celeb;
select column as alias
from tablename;
select column1, column2, ...
from tablename as alias;
select name as '이름'
from celeb;
select name as '이름', agency as '소속사'
from celeb;
select concat(name, ' : ', job_title) as profile
from celeb;
select s.season, s.episode, c.name, c.job_title
from celeb as c, snl_show as s
where c.name = s.host;
select concat(s.season, '-', s.episode, '(', s.broadcast_date, ')') as '방송정보',
concat(c.name, '(', c.job_title, ')') as '출연자정보'
from celeb as c, snl_show as s
where c.name = s.host;
select concat(s.season, '-', s.episode, '(', s.broadcast_date, ')') '방송정보',
concat(c.name, '(', c.job_title, ')') '출연자정보'
from celeb c, snl_show s
where c.name = s.host;
select distinct column1, column2, ...
from tablename;
select agency
from celeb;
select distinct agency
from celeb;
select sex, job_title
from celeb;
where job_title like '%가수%';
select distinct sex, job_title
from celeb;
where job_title like '%가수%';
select column1, column2, ...
from tablename
where condition
limit number;
select *
from celeb
limit 3;
select *
from celeb
order by age
limit 4;