DB 63

[MySQL] 뷰 view 생성 예제

1학기 수업을 듣는 학생들을 검색 출력: 학번, 이름 조건: 1학기 / 중복제거 추가 출력: 과목이름 중복제거를 하지 않는 경우 ! 조건에 맞는 학생이름 전체 출력, 조건에 맞는 번호 전체 출력 = 2번출력 -- 학번, 이름, 중복제거 mysql> create or replace view 1term_view as -> select distinct s.st_name, s.st_num -> from student as s, course as c -> where c.co_term = 1; Query OK, 0 rows affected (0.00 sec) mysql> select * from 1term_view; +---------+------------+ | st_name | st_num | +--------..

[MySQL] 뷰 view 생성, 수정, 삭제

뷰 view : 데이터베이스에 존재하는 일종의 가상 테이블 - 가상테이블 실체가 없고(실제 데이터를 저장하고 있지 않음), 필요한 내용만 꺼내서 보는 테이블 물리적으로 존재하지 않음 = 보여주기만 가능 view는 한번 생성하면 변경이 안됨 (update, delete 등 X) => 새로운 뷰로 대체(replace) 해야함! index를 가질 수 없음 뷰 view 생성 여러필드에서 가져오는 경우) 중복제거 distinct는 하는 것이 좋음 중복제거를 하지 않는 경우 조건에 맞는 값이 칼럼별로 나와 여러번 출력 1. 하나의 필드에서 가져와 생성 create view 뷰이름 as select 칼럼명 from 테이블명 where 조건 ; 2. 여러 필드에서 가져와 생성 create view 뷰이름 as sele..

[MySQL] 인덱스(색인) index 생성, 추가, 삭제

인덱스(색인) index : 테이블의 조회 속도를 높여주는 자료구조 추가/삭제/변경이 없어 거의 변동이 없는 칼럼에 이용 단점 : update, insert, delete 사용시 조회 속도 저하 인덱스 index 특징 primary키 = 자동 인덱스 설정 MYSQL INDEX(MYI) 파일에 저장된다 인덱스는 하나 또는 여러개의 칼럼에 대해 설정이 가능 (여러칼럼을 묶어 하나의 인덱스로도 설정이 가능) where 절 뒤에 사용될 경우, order by, group by에 사용되는 경우 -> index도 영향을 미침 인덱스 index를 사용할 수 없는 경우 복수의 키에 대해 order by하는 경우 연속하지 않는 칼럼에 대해 order by하는 경우 group by 칼럼, order by 칼럼이 다를 경우..

[MySQL] ERD 대학수강프로그램 - 4. 테이블 업데이트 (update, case, if, modify)

1. at_mid, at_final, at_attend, at_homework 값 업데이트 (40/40/10/10) update attend set at_homework = 8 where at_num=1; update attend set at_homework = 5 where at_num=2; update attend set at_homework = 2 where at_num=3; update attend set at_homework = 10 where at_num=4; update attend set at_homework = 7 where at_num=5; update attend set at_homework = 5 where at_num=6; update attend set at_homework = ..

[MySQL] ERD 대학수강프로그램 3. table이용 예시 (join)

( join으로 가져올 경우, from뒤에는 관계테이블 명 = 가운데 연결되어 있는 테이블 을 넣기! ) 1. 김영철이 수강하는 과목명을 출력 ( 중복과목이 있는 경우 (학기)도 함께 출력) mysql> select concat(su.su_title, '(' ,c.co_term, '학기)' ) as "김영철이 수강하는 과목명( 학기 )" -> from attend a -> join student s on at_st_num = s.st_num -> join course c on at_co_num = c.co_num -> join subject su on co_su_num = su.su_num -> where st_name = "김영철" -> order by co_term; +-----------------..

[MySQL] foreign key가 설정되어 있는 값의 설정변경

1. 외래키 테이블 락 걸기 lock table 참조테이블명 write, 내테이블명 write; //foreign key가 걸려있는 내테이블과 참조테이블에 lock lock table course write, attend write; 2. 외래키 지우기 alter table 내테이블명 drop foreign key 내테이블명_ibfk_2; //내테이블에서 참조데이블을 끊기! alter table attend drop foreign key attend_ibfk_2; 3. 속성 넣기 alter table course modify co_num int not null auto_increment; //참조테이블의 속성 설정 다시하기 alter table 참조테이블명 modify 칼럼명 int not null au..

[MySQL] auto_increment 초기화방법

alter table 테이블명 auto_increment = 1; set @count = 0; //변수만들기 update 테이블명 set 변경할칼럼명 =@count:=@count+1; mysql> alter table guide auto_increment = 1; mysql> set @count = 0; mysql> update guide set gu_num=@count:=@count+1; mysql> select * from guide; +--------+---------+------------+------------+ | gu_num | gu_year | gu_pr_num | gu_st_num | +--------+---------+------------+------------+ | 1 | 2020 ..

[MySQL] ERD 대학수강프로그램 2. 정보넣기

insert into student values (2020123001,'김영철',2,0), (2020123002,'나영희',2,0), (2020160001,'강철수',2,0), (2020160002,'박철수',2,0), (2020456001,'강군',2,0); insert into subject (su_code,su_title,su_point,su_time) values ('msc001','대학수학',3,3), ('com001','컴퓨터개론',2,2), ('com002','운영체제',3,3), ('abc001','글쓰기',2,2), ('abc002','영어',2,3); insert into professor values (2005789001,'홍길동',60,'B동 302호','재직','정교수'), (20..

[MySQL] ERD 대학수강프로그램 1. 생성

mysql> CREATE TABLE student ( -> st_num int NOT NULL, -> st_name varchar(20) NOT NULL, -> st_term int NOT NULL DEFAULT 0, -> st_point int NOT NULL DEFAULT 0 -> ); Query OK, 0 rows affected (0.01 sec) mysql> CREATE TABLE professor ( -> pr_num int NULL, -> pr_name varchar(20) NOT NULL, -> pr_age int NOT NULL, -> pr_room varchar(45) NOT NULL, -> pr_state varchar(45) NOT NULL DEFAULT '재직', -> pr_pos..