query-13(sqlDB데이터 변경후 초기화코드).sql
drop database if exists sqldb2;
create database sqldb2;
use sqldb2;
drop table if exists user;
create table user(
userID char(8) not null primary key, -- 사용자 아이디(pk)
userName varchar(10) not null, -- 이름
birthday int not null, -- 출생년도
addr char(2) not null, -- 지역(경기, 서울, 경남식으로 2글자만 입력)
mobile1 char(3), -- 휴대폰의 국번
mobile2 char(8), -- 휴대폰의 나머지 전화번호(-제외)
height smallint, -- 키
mDate date -- 회원 가입일
);
drop table if exists buy;
create table buy(
num int auto_increment not null primary key, -- 순번(PK)
userID char(8) not null, -- 아이디(FK)
prodName char(6) not null, -- 물품명
groupName char(4), -- 분류
price int not null, -- 단가
amount smallint not null, -- 수량
foreign key (userID) references user(userID)
);
INSERT INTO user VALUES('YJS', '유재석', 1972, '서울', '010', '11111111', 178, '2008-8-8');
INSERT INTO user VALUES('KHD', '강호동', 1970, '경북', '011', '22222222', 182, '2007-7-7');
INSERT INTO user VALUES('KKJ', '김국진', 1965, '서울', '019', '33333333', 171, '2009-9-9');
INSERT INTO user VALUES('KYM', '김용만', 1967, '서울', '010', '44444444', 177, '2015-5-5');
INSERT INTO user VALUES('KJD', '김제동', 1974, '경남', NULL , NULL , 173, '2013-3-3');
INSERT INTO user VALUES('NHS', '남희석', 1971, '충남', '016', '66666666', 180, '2017-4-4');
INSERT INTO user VALUES('SDY', '신동엽', 1971, '경기', NULL , NULL , 176, '2008-10-10');
INSERT INTO user VALUES('LHJ', '이휘재', 1972, '경기', '011', '88888888', 180, '2006-4-4');
INSERT INTO user VALUES('LKK', '이경규', 1960, '경남', '018', '99999999', 170, '2004-12-12');
INSERT INTO user VALUES('PSH', '박수홍', 1970, '서울', '010', '00000000', 183, '2012-5-5');
INSERT INTO buy VALUES(NULL, 'KHD', '운동화', NULL , 30, 2);
INSERT INTO buy VALUES(NULL, 'KHD', '노트북', '전자', 1000, 1);
INSERT INTO buy VALUES(NULL, 'KYM', '모니터', '전자', 200, 1);
INSERT INTO buy VALUES(NULL, 'PSH', '모니터', '전자', 200, 5);
INSERT INTO buy VALUES(NULL, 'KHD', '청바지', '의류', 50, 3);
INSERT INTO buy VALUES(NULL, 'PSH', '메모리', '전자', 80, 10);
INSERT INTO buy VALUES(NULL, 'KJD', '책' , '서적', 15, 5);
INSERT INTO buy VALUES(NULL, 'LHJ', '책' , '서적', 15, 2);
INSERT INTO buy VALUES(NULL, 'LHJ', '청바지', '의류', 50, 1);
INSERT INTO buy VALUES(NULL, 'PSH', '운동화', NULL , 30, 2);
INSERT INTO buy VALUES(NULL, 'LHJ', '책' , '서적', 15, 1);
INSERT INTO buy VALUES(NULL, 'PSH', '운동화', NULL , 30, 2);
select *
from buy;
조인(join)
2개 이상의 테이블을 묶어서 하나의 결과 테이블을 만드는 것
내부 조인(inner join)
JOIN 개념
-서로 다른 테이블을 공통 컬럼을 기준으로 합치는(결합하는) 테이블 단위연산.
-조인의 결과 테이블은 이전 테이블의 컬럼 수의 합과 같음.
-select * from 테이블1 join 테이블2 on 테이블1.컬럼명 = 테이블2.컬럼명...
query-14(내부조인).sql
-- JOIN 개념
-- 서로 다른 테이블을 공통 컬럼을 기준으로 합치는(결합하는) 테이블 단위연산.
-- 조인의 결과 테이블은 이전 테이블의 컬럼 수의 합과 같음.
-- select * from 테이블1 join 테이블2 on 테이블1.컬럼명 = 테이블2.컬럼명...
-- 현업에서도 많이 사용되기 때문에 반드시 알아둘 필요가 있다.
use sqldb2;
desc buy;
desc user;
-- user가 첫번째 테이블이 되고, buy가 두번째 테이블이 된다.
-- user테이블에 PK인 userID와 buy테이블의 FK인 userID가 서로 같은 것을 조인으로 해서
-- 내부조인 함. KYM의 조건을 줘서 KYM의 내용만 출력된 쿼리문을 작성한 것이다.
select *
from user
inner join buy
on user.userID = buy.userID
where buy.userID = 'KYM';
-- 에러가 발생
-- 바로 userid때문. userid는 buy, user 두개의 테이블에 존재하기 떄문임.
-- userid 컬럼이 어떤 테이블의 userid인지 모호하기 때문에 에러가 난다.
select userid, username, prodname, addr, concat(mobile1, mobile2) as '연락처'
from buy
inner join user
on buy.userid = user.userid;
-- 명시적으로 타이틀명, 컬럼명으로 표기함
-- => alias를 사용하여 쿼리를 줄일수 있음.
select buy.userid, user.username, buy.prodname, user.addr,
concat(mobile1, mobile2) as '연락처'
from buy
inner join user
on buy.userid = user.userid;
-- 테이블에 직접 alias를 설정한 쿼리문
select b.userid, u.username, b.prodname, u.addr,
concat(mobile1, mobile2) as '연락처'
from buy b
inner join user u
on b.userid = u.userid
order by u.userid;
select *
from user;
select *
from buy;
-- left outer 조인
-- a-b 차집합 : 왼쪽 테이블(user)을 다 출력한 구문이다.
select b.userid, u.username, b.prodname, u.addr,
concat(mobile1, mobile2) as '연락처'
from user u
left join buy b
on u.userid = b.userid
order by u.userid;
-- buy에 구매기록이 존재하는 사람들에게 신년메시지를 보내고 싶다.
-- 이럴때 해당 사람에 대한 쿼리문을 작성하시오.
select distinct b.userid, u.username, u.addr,
concat(mobile1, mobile2) as '연락처'
from buy b
inner join user u
on b.userid = u.userid
where u.mobile1 is not null
order by u.userid;