1> 테이블작성
Create table board(
Unq int unsigned not null auto_increment,
Title varchar(200) not null,
Pass varchar(100) not null.
Name varchar(20),
Contnt text,
Hits int unsigned default ‘0’,
rdate datetime,
Primary key(unq));
2>
Primary key 설정 —>
(1) 중복데이터 방지
(2) 빠른검색
3> 데이터입력
8.0이상버전부터는 password() 함수 대신 md5()함수 이용.
insert into board(title, pass, name, contnt, rdate)
values('학습과목',md5('123456'),'행정','java,jsp,html',now());
alter table board change contnt content text;
—졸면서 쳐서…오타났음 ㅎㅎ
delete from board where unq ='1' and pass = md5('212');
—삭제안됨. 비밀번호 틀렸으므로,
delete from board where unq ='2' and pass = md5('123456');
select * from board;
삭제된거 확인.
4> 풀어보기
1-1. Unq 3번의 제목, 이름, 내용을 출력한다.
select title, name, content, left(rdate,10) as rdate from board where unq='3';
—날짜가 시분초제외하고 나오게!
1-2. 제목의 길이를 목록 출력 ex. 학습과목(12)
select concat(title, '(',length(title),')') title from board;
3-1. 제목 필드에서 축구라는 단어가 들어 있는 데이터를 검색한다.
select * from board where title like '%축구%';
3-2. 내용 필드에서 java라는 단어가 들어있는 데이터를 검색한다.
select * from board where content like ‘%java%’;
3-3. 땅콩이라는 단어를 제목과 내용에서 모두 검색한다.
select * from board where title like '%땅콩%'
or content like '%땅콩%';
4-1. 가장 조회수가 많은 데이터 순으로 출력
select from board order by hits desc;
4-2. 최신 글 순으로 데이터를 출력
select from board order by unq desc;
(rdate도 가능하나, unq로 뽑는게 시스템적으로도 편함.)
5-1. 가장 최신 글 하나를 출력
select * from board order by unq desc limit 1;
select from board where unq = max(unq); // error.
select from board where unq = (select max(unq) from board); //가능. max(unq)에대한 조건을 다 붙여줘야지만 사용가능.
5-2. 가장 조회수 많은 데이터 하나를 출력.
select * from board order by hits desc limit 1;
세번째 데이터를 수정
(수정내용 - 제목: 땅콩회황사건, 이름: 김기자, hits 1000)
update board set
title = '땅콩회황사건', name = '김기자', hits = 1000
where unq=3;
축구사랑이 쓴 데이터의 조회수를 증가(+1) 시킨다.
update board set
hits = hits +1
where name ='축구사랑';
상세보기 -> 조회수 누적증가.