mysql #2,3 실습예제

ssol·2021년 7월 2일
0

mysql

목록 보기
4/4

unsigned : 음수의 공간을 양수로 옮기겠다. ex.tinyint 공간 -127~129 =unsigned=> 0~254.
-- : mysql에서의 주석.

1> 테이블 생성
create table studentscore(
unq int unsigned not null,
loc varchar(10) not null,
userid varchar(10) not null,
kor tinyint unsigned default '0',
eng tinyint unsigned default '0'
);

desc studentscore;

2> 데이터 입력(외부파일 import)

import -파일선택
csv using load data
필드 구분자 --> ,

select count() from studentscore;
select
from studentscore limit 40; -- 데이터 40개까지 보여줌.
delete from studentscore where uniq =0; --첫번째 줄에 잘못된 데이터가 들어와서 부분 삭제 적용함.

delete from studentscore; -- 테이블은 유지, 데이터 삭제.
truncate studentscore; -- 테이블은 유지, 데이터 삭제.

3> 내용수정. loc컬럼값 변경
--1 서울, 2 대구, 3 원주, 4 전주
update studentscore set loc = '서울' where loc = '1';
update studentscore set loc = '대구' where loc = '2';
update studentscore set loc = '원주' where loc = '3';
update studentscore set loc = '전주' where loc = '4';

4> 실습예제
1. 영어점수를 1등부터 출력
select eng from studentscore order by eng desc;

  1. 영어점수 중 최고점을 출력한다.
    select max(eng) as eng from studentscore ;
    --최저점
    select min(eng) as eng from studentscore;

  2. 아이디 st101의 평균을 출력
    select round((eng+kor)/2,2) as 'avg' from studentscore where userid = 'st101';
    -- select avg ( column의 평균...)

  3. 국어점수가 60점 미만인 학생의 아이디를 출력
    select userid from studentscore where kor <60;

  4. 두과목 모두 60점 이상인 학생들 출력
    select * from studentscore where kor >= 60 and eng >=60;

  5. 두과목중 한과목이라도 60미만인 학생들을 출력
    select * from studentscore where kor < 60 or eng <60;

  6. 영어점수 기준으로 1등부터 5등까지의 학생들을 출력
    select * from studentscore order by eng desc limit 0,5;
    --limit 시작번호,출력개수

  7. 영어점수 기준으로 6~10등 학생들 출력
    select *from studentscore order by eng desc limit 5,5;

  8. 전체 총합 1등의 아이디,총합점수를 출력
    select userid, (eng+kor) as 'total' from studentscore order by (eng+kor) desc limit 0,1;
    --limit 0,x 일 경우, 0은 생략가능

  9. 아이디가 101인 데이터의 영어점수에 +5를 적용한다.
    update studentscore set eng =eng+5 where userid = 'st101';

  10. 대구지역 학생 목록을 출력한다.
    select * from studentscore where loc = '대구';

  11. 대구지역 학생 수를 출력한다.
    select count(*) from studentscore where loc = '대구';

  12. 대구지역에서 영어점수가 가장 높은 학생의 아이디와 영어점수를 출력한다.
    select userid, eng, loc from studentscore where loc = '대구' order by eng desc limit 1;
    --where은 테이블 뒤에 바로, 앞선조건

  13. 대구지역에서 두 과목 합계1등의 아이디와 총점을 출력한다.
    select userid, (eng+kor) total, loc from studentscore where loc = '대구' order by (eng+kor) limist 1;
    -- as total : as 생략가능
    // alias 이름은 order by 절에 사용가능. mysql에서는! 오라클은 나중에 비교해보기~

5> 주소값변경
update studentscore set loc = '서울특별시 강남구 12' where loc ='서울';
update studentscore set loc = '대구광역시 수성구 101' where loc ='대구';
update studentscore set loc = '강원도 원주시' where loc ='원주';
update studentscore set loc = '전라북도 전주시' where loc ='전주';

6> like 연산자 : %문자열, _문자한개
1. 수성구에 사는 학생들을 출력
select from studentscore
where loc like '%수성구%';
2. 서울에 사는 학생들을 출력
select
from studentscore where loc like '서울%';

7> 풀어보기
1. 대구에 사는 학생들은 총 몇명인가?
select count(*) from studentscore where loc like '대구%';
2. 강원도 학생들의 영어평균?
select avg(eng) from studentscore where loc like '강원%';

profile
공부,복습

0개의 댓글