: mysql -u root -p비밀번호
현재 PC에서 접근 가능한 계정 사용자
: localhost
: CREATE USER '사용자이름'@'localhost' identified by'비밀번호'
외부에서 접속 가능한 사용자
: localhost 대신 %
alter table mytable rename person;
alter table person add column agee double;
alter table person modify column agee int;
alter table person change column agee age int;
: change column 문법은 컬럼 네임, 데이터 타입 모두 변경할 수 있음
alter table person drop column age;
drop table animal;
insert into person (id, name, age, sex)
-> values (1, '이효리', 43, 'F');
insert into person
-> values (2, '이상순', 48, 'M');
select name, age from person;
select * from person;
person은 테이블네임
' * ' : 전부
: person 테이블에 모든 데이터를 보겠다
desc person; 과의 차이점
SQL 문에 조건을 추가
SELECT 뿐만 아니라 UPDATE 와 DELETE 에도 사용
테이블 내의 조건을 만족하는 데이터 조회
SELECT 컬럼1, 컬러2, ...
FROM 테이블 네임
WHERE 조건;
Ex. person 테이블에서 성별이 여자인 데이터 조회/ 필터링
select * from person where sex='F';
update person set age=23 where name='이효리';
delete from person where name='이상순';
use zerobase;
NOT NULL AUTO_INCREMENT PRIMARY KEY
NOT NULL DEFAULT ' ',
테이블 생성 후 꼭 확인 하기 !
desc celeb;
ACE(ascending) : 오름차순
DESC(descending) : 내림차순
SELECT 컬럼1, 컬럼2, ...
FROM 테이블네임
ORDER BY 컬럼1, 컬럼2, ... ASC | DESC ;
SELECT FROM WHERE 다음에 ORDER BY
SELECT age, name
FROM celeb
ORDER BY age ASC;
= select age, name from celeb order by age;
SELECT age, name
FROM celeb
ORDER BY age DESC;
-------------------------
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, name;
SELECT age, name
FROM celeb
ORDER BY age DESC, name ASC;
select name, age from celeb where age=29 order by age;
select name, age from celeb where age!=29 order by age;
select age, name from celeb where age>29 order by age desc;
SELECT * FROM celeb WHERE age=29 AND sex='F';
select * from celeb where sex='M' and age>40 order by name desc;
select * from celeb where age<25 or age>30;
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
order by age, sex;
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 (birthday > 19891231 and not sex='F') or (birthday<19800101 and not agency = '안테나');
select * from celeb
where age between 20 and 40;
select * from celeb where age>=20 and age<=40;
select * from celeb
where (not birthday between 19800101 and 19951231 and sex='F')
or (agency='YG엔터테이먼트' and not age between 20 and 45);
SELECT * FROM celeb
WHERE age IN (28, 48);
select * from celeb
where not agency in ('나무엑터스', '안테나', '울림엔터테이먼트')
and (sex='F' or age>=45);
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%'
select * from celeb
where job_title like '가_%'
select * from celeb
where job_title like '%,%' ;
여러 개의 SQL문을 합쳐서 하나의 SQL문으로 만들어주는 방법
주의 ! 컬럼의 개수가 같아야함 !
SELECT문 1
UNION | UNION ALL
SELECT문 2
SELECT * FROM test1
UNION
SELECT * FROM test2;
select * from test1
union all
select * from test2;
select name, sex, agency from celeb where sex='F'
union all
select name, sex, agency from celeb where agency='YG엔터테이먼트' ;
select * from celeb
where job_title like '%가수%'
union
select * from celeb
where job_title like '%배우%';
두 개 이상의 테이블을 결합하는 것
실습 환경
SELECT 컬럼1, 컬럼2, ...
FROM 테이블A
INNER JOIN 테이블B
ON 테이블A.column = 테이블B.column
WHERE 조건;
ON : 조인 기준
snl_show에 호스트로 출연한 celeb을 기준으로 celeb 테이블과 snl_show 테이블을 INNER JOIN
컬럼명이 겹치지 않을 경우, 테이블명 생략 가능
: select celeb.id, name, snl_show.id, host
SELECT celeb.id, celeb.name, snl_show.id, snl_show.host
FROM celeb
INNER JOIN snl_show
ON celeb.name = snl_show.host;
SELECT 컬럼1, 컬럼2, ...
FROM 테이블A
LEFT JOIN 테이블B
ON 테이블A.column = 테이블B.column
WHERE 조건;
테이블A = LEFT 테이블
테이블B = RIGHT 테이블
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
left join snl_show
on celeb.name = snl_show.host;
select celeb.id, celeb.name, snl_show.id, snl_show.host
from celeb
right join snl_show
on celeb.name = snl_show.host;
LEFT JOIN 과 RIGHT JOIN 사용 후, UNION 사용
UNION : 중복된 부분 제거
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 컬럼1, 컬럼2, ...
FROM 테이블A, 테이블B, ...
WHERE 조건;
FROM 합칠 여러 테이블
WHERE 기준 조건
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='안테나';
celeb 테이블의 연예인 중, snl_show에 host로 출연했고,
배우는 아니면서 YG엔터테이먼트 소속이거나
40세 이상이면서 YG엔터테이먼트 소속이 아닌
연예인의 이름과 나이, 직업, 소속사, 시즌, 에피소드 정보 검색
STEP 1 : celeb 테이블의 연예인 중, 영화배우는 아니면서 YG 엔터 소속이거나
select * from celeb
where not job_title like '%배우%' and agency = 'YG엔터테이먼트' ;
select * from celeb
where age >= 40 and agency != 'YG엔터테이먼트';
select celeb.id, celeb.name, snl_show.host
from celeb, snl_show
where celeb.name = snl_show.host ;
select celeb.id, celeb.name, snl_show.host
from celeb, snl_show
where celeb.id = snl_show.host
and not job_title like '%배우%' and agency = 'YG엔터테이먼트' ;
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 concat ('concat', ' ', 'test');
select concat ('이름:' , name)
from celeb ;
SELECT 컬럼 as 컬럼 별칭
FROM 테이블네임 ;
SELECT 컬럼1, 컬럼2, ...
FROM 테이블네임 as 테이블 별칭 ;
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 agency FROM celeb ;
SELECT DISTINCT agency FROM celeb ;
select distinct sex, job_title from celeb
where job_title like '%가수%' ;
SELECT 컬럼1, 컬럼2, ...
FROM 테이블네임
WHERE 조건
LIMIT 숫자 ;
셀렉트 문 마지막에 넣으면 됨
select * from celeb limit 3;
select * from celeb order by age limit 4;