코딩애플 강의를 통해 배운 SQL & Database를 정리한 글입니다.
2025년 2월 25일
Part 2 : Part 2
-- 생성
create database 테스트
-- 삭제
drop database 테스트
-- 생성
create table 테스트.new_table (
id int,
이름 varchar(100) default '홍길동',
나이 int
)
-- 삭제
drop table new_table
-- 생성
alter table new_table add 컬럼명 varchar(1000)
-- 수정
alter table new_table modify column 나이 varchar(100)
-- 삭제
alter table new_table drop column 컬럼명
create table 테스트.new_table (
id int not null,
이름 varchar(100),
나이 int
)
create table 테스트.new_table (
id int unique,
이름 varchar(100),
나이 int
)
-- 이거는 id 이름값이 전부 같으면 금지라는뜻
create table 테스트.new_table (
id int,
이름 varchar(100),
나이 int,
unique(id, 이름)
)
create table 테스트.new_table (
id int,
이름 varchar(100),
나이 int check(나이 > 0),
)
create table 테스트.new_table (
id int primary key,
이름 varchar(100),
나이 int,
)
create table 테스트.new_table (
id int auto_increment,
이름 varchar(100),
나이 int,
)
CREATE TABLE new_table (
id INT,
이름 VARCHAR(100),
나이 INT,
CONSTRAINT 제약조건작명 PRIMARY KEY (id),
CONSTRAINT 제약조건작명2 CHECK(나이 > 10)
)
partial dependency를 제거한 테이블primary key를 composite primary key라고하는데composite primary key 중에 하나의 컬럼에만 종속되어 있는 따까리 컬럼을transitive dependency를 제거한것transitive dependency라고함primary key, foreign key 항상 넣으면 좋음where못쓰고 on쓰기가능select 프로그램, 가격, 강사, 출신대학
from program
inner join teacher
on 강사id = teacher.id
select *
from program
left join teacher
on 강사id = teacher.id
select *
from program
right join teacher
on 강사id = teacher.id
insert into stock (id,상품명,가격)
values(1,'김치',500)
create temporary table 테이블
update stock
set 가격 = 1000
where id = 1
delete
from stock
where 가격 = 1000;
UNION ALLselect * from 테이블1
union
select * from 테이블2
CREATE VIEW 뷰이름 AS
SELECT 컬럼1, 컬럼2, ...
FROM 테이블명