Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

create user madang2 identified by madang2 default tablespace users temporary

tablespace temp profile default;


grant connect, resource to madang2;
grant create view, create synonym to madang2;
grant unlimited tablespace to madang2;
alter user madang2 account unlock;

create table book(


bookid number(2) primary key, bookname varchar(30), publisher varchar2(30), price
number(8) );

create table customer( custid number(2) primary key, name varchar(30), address
varchar2(30), phone varchar2(20) );

create table orders( orderid number(2) primary key, custid number(2) references
customer(custid), bookid number(2) references book(bookid), saleprice number(8),
orderdate date);

insert into book values(1, '축구의 역사', '굿스포츠', 7000);


insert into book values(2, '축구 아는 여자', '남무수', 13000);
insert into book values(3, '축구의 이해', '대한미디어', 22000);
insert into book values(4, '골프 바이블', '대한미디어', 35000);
insert into book values(5, '피겨 교본', '굿스포츠', 8000);
insert into book values(6, '역도 단계별기술', '굿스포츠', 6000);
insert into book values(7, '축구의 추억', '이상미디어', 20000);
insert into book values(8, '야구를 부탁해', '이상미디어', 13000);
insert into book values(9, '올림픽 이야기', '삼성당', 7500);
insert into book values(10, 'olympic champions', 'pearson', 13000);

insert into customer values(1, '박지성', '영국 맨체스터', '000-5000-0001');


insert into customer values(2, '김연아', '대한민국 서울', '000-6000-0001');
insert into customer values(3, '장미란', '대한민국 강원도', '000-7000-0001');
insert into customer values(4, '추신수', '미국 클리블랜드', '000-8000-0001');
insert into customer values(5, '박세리', '대한민국 대전', null);

insert into orders values(1, 1, 1, 6000, to_date('2020-07-01', 'yyyy-mm-dd'));


insert into orders values(2, 1, 3, 21000, to_date('2020-07-03', 'yyyy-mm-dd'));
insert into orders values(3, 2, 5, 8000, to_date('2020-07-03', 'yyyy-mm-dd'));
insert into orders values(4, 3, 6, 6000, to_date('2020-07-04', 'yyyy-mm-dd'));
insert into orders values(5, 4, 7, 20000, to_date('2020-07-05', 'yyyy-mm-dd'));
insert into orders values(6, 1, 2, 12000, to_date('2020-07-05', 'yyyy-mm-dd'));
insert into orders values(7, 4, 8, 13000, to_date('2020-07-07', 'yyyy-mm-dd'));
insert into orders values(8, 4, 10, 12000, to_date('2020-07-08', 'yyyy-mm-dd'));
insert into orders values(9, 2, 10, 7000, to_date('2020-07-09', 'yyyy-mm-dd'));
insert into orders values(10, 3, 8, 13000, to_date('2020-07-01', 'yyyy-mm-dd'));

1. 모든 도서의 이름과 가격을 검색하시오.


select bookname, price from book;
select bookname as 책이름, price as 책가격 from book;

2. 도서 테이블의 모든 출판사를 검색하시오.


select publisher from book;
3. 도서 테이블의 모든 출판사(중복제외)를 검색하시오.
select distinct publisher from book;

4. 도서 테이블에서 가격이 2 만원 미만인 도서명과 가격과 출판사명을 검색하시오.


select bookname, publisher, price from book where price < 20000;

5. 도서 테이블에서 가격이 1 만원이상 2 만원이하 도서명과 가격과 출판사명을 검색하시오.(and 사용)


select bookname, publisher, price from book where price >= 10000 and price <=20000;

6. 도서 테이블에서 가격이 1 만원이상 2 만원이하 도서명과 가격과 출판사명을 검색하시오.(between and


사용)
select bookname, publisher, price from book where price between 10000 and 20000;

7. 출판사가 '굿스포츠' 혹은 '대한미디어'인 도서를 검색하시오.(in 사용)


select * from book where publisher in ('굿스포츠', '대한미디어');

8. 출판사가 '굿스포츠' 혹은 '대한미디어'가 아닌 도서를 검색하시오.(in 사용)


select * from book where publisher not in ('굿스포츠', '대한미디어');

9. 축구의 역사를 출간한 출판사를 검색하시오.


select bookname, publisher from book where bookname like '축구의 역사';
select bookname, publisher from book where bookname = '축구의 역사';

10. 책이름 중에서 '축구'라는 글자가 들어있는 책이름과 출판사명을 검색하시오.


select bookname, publisher from book where bookname like '%축구%';

11. bookid 가 7 인 책이름을 '야구의 추억' 으로 변경하시오.


update book set bookname = '야구의 추억' where bookid = 7;

12. customer 테이블에서 고객번호가 '5'인 고객의 주소를 '대한민국 부산'으로 변경하시오.
update customer set address = '대한민국 부산' where custid = 5;

13. customer 테이블에서 박세리 고객의 주소를 김연아 주소로 변경하시오.


update customer set address = (select address from customer where name = '김연아')
where name = '박세리';

14. 주소가 '대한미국 서울'로 되어 있는 것을 '대한민국 서울'로 변경하시오.


update customer set address = '대한민국 서울' where address = '대한미국 서울';

15. update customer set address = '대한민국 서울'; //모든 레코드의 주소가 한번에 '대한민국
서울'로 변경된다.

16. customer 테이블에서 전화번호가 null 인 레코드를 검색하시오.


select * from customer where phone is null;

17. customer 테이블이 전화번호(phone)가 null 인 경우 '02-1234-1234'로 변경하시오.


update customer set phone = '02-1234-1234' where phone is null;

18. customer 테이블에서 고객번호가 5 인 고객을 삭제한 후 결과를 확인하시오.


delete from customer where custid = 5;
select * from customer;

19. delete from customer; //where 이 없으면 customer 테이블의 모든 레코드가 삭제된다.

20. customer 테이블의 박세리 레코드가 삭제되었는데 복원해보시오.


rollback;
select * from customer;

1. 마당서점의 고객이 요구하는 다음 질문에 대해 SQL 문을 작성하시오.


(1)select bookname from book where bookid = 1;
(2)select bookname from book where price > 20000;
(3)select sum(saleprice) from orders where custid = 1;
(4)select count(*) from orders where custid = 1;
(5)select count(publisher) from book where bookid in (select bookid from orders
where custid = 1);
(6)select bookname, price, price-saleprice from book, orders where bookid in
(select bookid from orders where custid = 1);
(7)select bookname from book where bookid not in (select bookid from orders where
custid = 1);

select bookname from book, orders where book.bookid = orders.bookid;

21. 고객이 주문한 도서의 총판매액을 구하시오.


select sum(saleprice) as 총판매액 from orders;

22. 2 번 김연아 고객이 주문한 도서의 총판매액을 구하시오.


select sum(saleprice) as 총판매액 from orders where custid = 2;

23. 고객이 주문한 도서의 총판매액, 평균값, 최저가를 구하시오.


select sum(saleprice) as 총판매액, avg(saleprice) as 총평균값, max(saleprice) as 최고값,
min(saleprice) as 최저가 from orders;

24. 고객별로 주문한 도서의 총수량과 총판매액을 구하시오.


select custid as 고객번호, count(*) as 도서수량, sum(saleprice) as 총판매액 from orders
group by custid;

25. 가격이 8000 원 이상인 도서를 구매한 고객에 대하여 고객별 주문 도서의 총수량을 구하시오. 단 2 권 이상
구매한 고객만 구하시오.

26. 고객과 고객의 주문에 관한 데이터를 고객별로 정렬하여 보이시오.


select * from customer, orders where customer.custid = orders.custid order by
customer.custid;

27. 고객의 이름과 고객이 주문한 도서의 판매가격을 검색하시오.


select name, saleprice from customer, orders where customer.custid = orders.custid;

28. 고객별로 주문한 모든 도서의 총판매액을 구하고, 고객별로 정렬하시오.


select name, sum(saleprice) from customer, orders where customer.custid =
orders.custid group by customer.name
order by customer.name;
29. 가격이 20000 원인 도서를 주문한 고객의 이름과 도서의 이름을 구하시오.
select customer.name, book.bookname from customer, orders, book where
customer.custid = orders.custid and orders.bookid
= book.bookid and book.price = 20000;

과제
30. 가장 비싼 도서의 이름을 보이시오.
select bookname from book where price=(select max(price) from book);
31. 가장 값싼 도서의 이름을 보이시오.
select bookname from book where price=(select min(price) from book);
32. '대한미디어'에서 출판한 도서를 구매한 고객의 이름을 보이시오.
select name from customer where custid in(select custid from orders where bookid
in(select bookid from book where publisher
= '대한미디어'));

select customer.name,book.publisher from book ,customer,orders where


customer.custid = orders.custid and book.bookid = orders.bookid
and book.publisher = '대한미디어';

33. '이상미디어'에서 출판한 도서를 구매한 고객의 이름을 보이시오.


select name from customer where custid in(select custid from orders where bookid
in(select bookid from book where publisher
= '이상미디어'));

34. book 테이블에 새로운 도서 '스포츠 의학'을 삽입하시오. 단 출판사명은 '한솔의학서적'이고, 가격은
90000 원
insert into book values(11, '스포츠 의학', '한솔의학서적', 90000);

35. book 테이블에 새로운 도서 'C 언어 프로그래밍'을 삽입하시오. 단 출판사명은 '글로벌'이고, 가격은
24000 원
insert into book values(12, 'C 언어 프로그래밍', '글로벌', 24000);

36. customer 테이블에서 고객번호가 5 인 고객의 주소를 '대한민국 부산'으로 변경하시오.


update customer set address = '대한민국 부산' where custid = 5;

37. customer 테이블에서 박세리 고객의 주소를 김연아 고객의 주소로 변경하시오.
update customer set address = (select address from customer where name = '김연아')
where name = '박세리';

38. customer 테이블에서 고객번호가 5 인 고객을 삭제하시오.


delete from customer where custid = 5;
select * from customer;
rollback;

39. 23.8567 값을 소수점 첫째 자리에서 반올림한 값을 구하시오.


select round (23.8567) from dual;

40. 'I love you'라는 문자열의 값을 모두 대문자로 변환하시오.


select upper('I love you') from dual;
41.'korea'라는 문자열의 값에서 ea 를 잘라내고 결과값을 구하시오.

42. 'I love you'라는 문자열의 값에서 앞 뒤 공백을 제거하시오.

43. 'I love you'라는 문자열의 값에서 모든 공백을 제거하시오.


select replace(' I love you ',' ','') from dual;

44. 'I love you'라는 문자열의 값에서 글자수를 반환하시오.

45. 굿스포츠에서 출판한 도서의 제목과 제목의 글자수, 바이트수를 보이시오.


select bookname as 제목, length(bookname) as 글자수, length(bookname) as 바이트수 from
book where publisher = '굿스포츠';

46. 고객중에서 같은 성을 가진 사람이 몇명이나 되는지 성별 인원수를 구하시오.


select substr(name, 1, 1) as 성, count(*) as 인원 from customer group by
substr(name, 1, 1);

47. 이름, 전화번호가 포함된 고객목록을 보이시오. 단 전화번호가 없는 고객은 '연락처없음'으로 표시하시오.
select name as 이름, NVL(phone, '연락처없음') as 전화번호 from customer;

48. 이름, 전화번호가 포함된 고객목록을 보이시오. 단 전화번호가 없는 고객인 '연락처미기재'으로 표시하시오.

49. 대한민국에 거주하는 고객에게 판매한 도서의 총판매액을 구하시오.


select sum(saleprice) as 총판매액 from orders where custid in (select custid from
customer where address like '%대한민국%');

50. 주소에 '대한민국'을 포함하는 고객들로 구성된 뷰를 만들고 조회하시오. 뷰의 이름은 vw_customer 로
설정
create view vw_customer as select * from customer where address like '%대한민국%';
select * from vw_customer;

You might also like