create database sampledb;

use sampledb;

drop table student;

create table student
(
     name varchar(20) not null, -- 이름
    dept varchar(20) not null, -- 학과
    id char(10) primary key not null, -- 학번, 기본키
    address varchar(50) not null -- 주소
);

insert into student values('최규식', '스마트컨텐츠개발', '2014071023', '강원도 홍천군');
insert into student values('박태수', '스마트컨텐츠기획', '2014072008', '경기도 용인시');
insert into student values('정상혁', '스마트컨텐츠마케팅', '2014071020', '서울시 관악구');

insert into student values('이기자', '컴퓨터공학', '0494013000', '서울시 성북구');

select * from student order by name asc;

-- 도서대여 테이블 생성
create table bookRent
(
     no int not null primary key, -- 대여번호, 기본키
    id char(10) not null, -- 학번
    title varchar(20) not null, -- 책 제목
    rDate varchar(20) not null, -- 대여일자
    foreign key (id) references student(id) -- 참조키, 이 테이블의 id가 student 테이블의 id 참조
);

-- 부분출력 (현재날짜 및 시간) -> 인덱스번호는 1번부터...!!!
select substring(now(), 1, 20);

insert into bookRent values(201401, '2014072008', '입고살래?', substring(now(), 1, 20));
insert into bookRent values(201402, '2014071020', '난 정말 JAVA를 배운적이 없다구요', '20141202');
insert into bookRent values(201403, '2014071023', '짜투리 프로그래밍', '20141202');
insert into bookRent values(201404, '0494013000', '안드로이드', '20141223');

update bookRent set rDate='2014-12-02' where no=201402;
update bookRent set rDate='2014-12-02' where no=201403;
update bookRent set rDate='2014-12-23' where no=201404;

select s.id, s.name 이름, b.title 도서명, b.rDate 대여일자
from student s, bookRent b
where s.id=b.id
order by id;