제약조건을 변경할때는 기존제약을 위반하는 데이터가있는지 검사한다
#-열제약추가
alter table sample12 modify c varchar(20) not null;
#alter사용 c열의 varchar 최대길이를 20으로 줄이고 not null을 추가
#제약조건 삭제
alter table sample12 modify c varchar(30);
#삭제라고하는데 그냥 alter를 사용한 수정인거같다
alter table sample12 drop primary key;
#기본키는 테이블당 하나만 존재할수 있으므로 열을 지정하지않아도 된다
#전체조회
SELECT *
FROM information_schema.table_constraints;
#테이블별 조회
SELECT *
FROM information_schema.table_constraints
WHERE 'sample12';
#해당 스키마에서 조회
select *
from information_schema.table_constraints
where CONSTRAINT_SCHEMA = '스키마명';
insert into sample634 values(1, '첫번째');
- sample634에 차례대로 1과 '첫번째'라는 값을 넣어라
update sample634 set p=2 where p=3;
- update 갱신해라 set -> p열을 2로, where -> p가 3인곳을/ where필터링이 없으면 p열이 전부 2로 바뀜
#슈퍼키 만들기
create table sample635(
a int not null,
b int not null,
constraint skey_sample635 primary key(a,b)
#primary key를 2개의 열로 언급 -> 슈퍼키
);
insert into sample635 values(1,1);
insert into sample635 values(1,2);
insert into sample635 values(1,3);
insert into sample635 values(2,1);
insert into sample635 values(2,2);
#a와 b 두열을 하나로 묶어서 봐야한다 -> 11,12,13,21,22 이런식으로
ab = 11, 12, 13, 21, 22 서로묶어서 봤을때 중복되지 않는다
아래 사진을 보자. 7조라는 팀에 팀원은 4명이 있다. 이 4명을 구분할 수 있는 것은 절대 겹치지 않는 학번 일수도 있고, 주민번호일 수도 있다.
이름과 나이를 묶어서 하나의 속성으로 만드는 것도 가능하다. 이름과 나이를 합쳐서 7조안에서 중복만 되지 않으면 가능하기 때문이다. 이름과 나이를 합쳐서 4명을 구분할 수 있으면 슈퍼키가 될 수 있다.
학번과 주민번호를 묶어서 슈퍼키로 만들수도 있고, 학번과 주민번호과 이름을 합쳐서 슈퍼키로도 만들수 있고, 학번과 주민번호과 이름과 나이를 합쳐서 슈퍼키를 만들수도 있다. 어떤 속성끼리 묶던 중복값이 안나오고 서로 구별만 할 수 있으면 된다.

*출처 https://jerryjerryjerry.tistory.com/49
효율적검색이가능, where조건지정된 select명령 처리속도 향상
알고리즘 종류
- 풀테이블 스캔
- 이진탐색
- 이진트리
인덱스는 DB객체이므로 DDL을 사용해서 작성,삭제한다
#인덱스 생성
create index index1 on sample13(a);
select * from sample13; #index를 사용하지않음
explain select * from sample13;
#possible_keys열, key열이 null이면 인덱스를 안쓰고있다는 것
select * from sample13 where a='a'; #index사용해서 검색
explain select * from sample13 where a='a';
#possible_keys,key열이 index1을 사용
#create view 뷰명 as select 명령
create view sample_view_635 as select * from sample635;
서브쿼리의 형태
#SELECT 절 - (스칼라 서브쿼리)
select (select from) from 테이블명
#FROM 절 - (인라인 뷰)
select * from (select * from) 테이블명
select * from 테이블명 where 테이블명 = (select * from) 테이블명
- (중첩서브쿼리)