inblog logo
|
gyul
    데이터 베이스

    [데이터 베이스] 7. 데이터 정의 (DDL)

    귤's avatar
    귤
    Feb 26, 2025
    [데이터 베이스] 7. 데이터 정의 (DDL)
    Contents
    1. Create2. Alter3. Drop4. Truncate5. 제약 조건 추가6. 제약 조건 (foreign key) 사용5. 삭제 (성공)6. 삭제 (실패)
    💡
    DDL은 데이터 정의 언어 (Data Definition Language)의 약어이다.
    데이터를 생성하거나 수정, 삭제 등 데이터의 전체 골격을 결정하는 역할의 언어를 말한다.
    CREATE ALTER DROP TRUNCATE

    1. Create

    💡
    데이터 베이스, 테이블 등을 생성하는 역할을 한다.
    -- DDL (create, drop, alter) -- 1. create table create table team_tb ( tno int primary key, -- 유일하게 짤 수 있다 tname varchar(10) unique, -- 유일하게 짤 수 있다 tyear int, tloc varchar(10) ) charset=utf8mb4; select * from team_tb; desc team_tb; create table player_tb ( pno int primary key, -- 유일하게 짤 수 있다, primary key는 유니크하고 null일 수 없다, 또한 그 행에서 반드시 값을 찾을 수 있어야 한다! pname varchar(20), -- 유일하게 짤 수 있다 pnumber int, prole varchar(10), tno int -- 참조키 ) charset=utf8mb4; select * from player_tb; desc player_tb; insert into team_tb(tno, tname, tyear, tloc) values(1,'삼성', 1982, '대구'); insert into team_tb(tno, tname, tyear, tloc) values(2,'넥센', 2000, '서울'); insert into team_tb(tno, tname, tyear, tloc) values(3,'롯데', 1990, '부산'); insert into player_tb(pno, pname, pnumber, prole, tno) values(1, '이대호', 20, '1루수', 3); -- 다른 테이블을 참조 시킬 수 있다 insert into player_tb(pno, pname, pnumber, prole, tno) values(2, '가득염', 10, '투수', 3); -- 다른 테이블을 참조 시킬 수 있다 insert into player_tb(pno, pname, pnumber, prole, tno) values(3, '임수혁', 5, '포수', 3); -- 다른 테이블을 참조 시킬 수 있다 insert into player_tb(pno, pname, pnumber, prole, tno) values(4, '이승엽', 3, '1루수', 1); -- 다른 테이블을 참조 시킬 수 있다 insert into player_tb(pno, pname, pnumber, prole, tno) values(5, '박병호', 19, '1루수', 2); -- 다른 테이블을 참조 시킬 수 있다 select * from team_tb; select * from player_tb;
    1. team_tb 생성
    1. team_tb 생성
    2. player_tb 생성
    2. player_tb 생성
    3. team_tb  insert
    3. team_tb insert
    1. team_tb 생성
    1. team_tb 생성
    2. player_tb 생성
    2. player_tb 생성
    3. player_tb insert
    3. player_tb insert
     

    2. Alter

    💡
    테이블을 수정하는 역할을 한다.
    alter table player_tb change column prole ptyle varchar (20);
    수정 전
    수정 전
    수정 후
    수정 후

    수정 바로 하기

    notion image
    notion image

    3. Drop

    💡
    데이터 베이스, 테이블을 삭제하는 역할을 한다.
    drop table player_tb; select * from player_tb;
    notion image
    notion image

    4. Truncate

    💡
    테이블을 초기화 시키는 역할을 한다.
    truncate team_tb; select * from team_tb;
    notion image

    5. 제약 조건 추가

    -- 5. 제약 조건 추가 create table player_tb ( pno int primary key auto_increment, pname varchar(20) not null, pnumber int, prole varchar(10) default '타자', tno int ) charset=utf8mb4; insert into player_tb(pname, pnumber, tno) values('이대호', 20, 3); insert into player_tb(pname, pnumber, prole, tno) values('가득염', 10, '투수', 3); insert into player_tb(pname, pnumber, prole, tno) values('임수혁', null, '포수', 3); insert into player_tb(pname, prole, tno) values('이승엽', '1루수', 1); insert into player_tb(pname, pnumber, prole, tno) values('박병호', 19, '1루수', 2); select * from player_tb;
    notion image

    👍외워 놓으면 좋은 것!

    • PK = Primary key, 유니크하고 null일 수 없다, 또한 그 행에서 반드시 값을 찾을 수 있어야 한다!
    • FK = Foreign Key, 데이터가 일관될 수 있도록 돕는 역할을 한다. 일종의 테이블을 연결하는 가상의 다리 역할을 한다.
    • FF = 포린키 퍼스트
    • 포린키있는게 드라이빙 되어야 함
    • 앤포드
      • notion image

    6. 제약 조건 (foreign key) 사용

    -- DDL (create, drop, alter) -- 모든 제약 조건 잠시 해제 drop table if exists team_tb; drop table if exists player_tb; -- 1. create table -- team_tb는 부모 테이블 create table team_tb ( tno int primary key, tname varchar(10) unique, tyear int, tloc varchar(10) ) charset=utf8mb4; -- 포린키를 걸면 player_tb는 자식 테이블 create table player_tb ( pno int primary key auto_increment, pname varchar(20) not null, pnumber int, prole varchar(10) default '타자', tno int, constraint fk_player_tb foreign key (tno) references team_tb (tno) -- 포린키 거는 이유 : 제한 걸려고, 참조할려고 하는게 아님!! / 제약 조건 걸때는 이름이 필요함 ) charset=utf8mb4; insert into team_tb(tno, tname, tyear, tloc) values(1, '삼성', 1982, '대구'); insert into team_tb(tno, tname, tyear, tloc) values(2, '넥센', 2000, '서울'); insert into team_tb(tno, tname, tyear, tloc) values(3, '롯데', 1990, '부산'); insert into player_tb(pno, pname, pnumber, prole, tno) values(1, '이대호', 20, '1루수', 3); insert into player_tb(pno, pname, pnumber, prole, tno) values(2, '가득염', 10, '투수', 3); insert into player_tb(pno, pname, pnumber, prole, tno) values(3, '임수혁', 5, '포수', 3); insert into player_tb(pno, pname, pnumber, prole, tno) values(4, '이승엽', 3, '1루수', 1); insert into player_tb(pno, pname, pnumber, prole, tno) values(5, '박병호', 19, '1루수', 2); -- 3. fk 제약조건으로 인해 insert 불가능 insert into player_tb(pname, pnumber, prole, tno) values('홍길동', 19, '1루수', 4); select * from player_tb;
     

    ❗ player_tb 정보 보는 법

    notion image
    notion image

    5. 삭제 (성공)

    -- 4. 삭제 (성공) delete from player_tb where pno = 5;

    (1) cascade - on delete (위험도 ⚠️)

    -- (2) cascade - on delete drop table if exists team_tb; drop table if exists player_tb; create table team_tb ( tno int primary key, tname varchar(10) unique, tyear int, tloc varchar(10) ) charset=utf8mb4; create table player_tb ( pno int primary key auto_increment, pname varchar(20) not null, pnumber int, prole varchar(10) default '타자', tno int, constraint fk_player_tb foreign key (tno) references team_tb (tno) on delete cascade ) charset=utf8mb4; select * from player_tb; delete from team_tb where tno = 3;
    notion image
     
    notion image
    notion image

    (2) cascade - on delete set null

    -- (3) cascade - on delete set null drop table if exists team_tb; drop table if exists player_tb; create table team_tb ( tno int primary key, tname varchar(10) unique, tyear int, tloc varchar(10) ) charset=utf8mb4; create table player_tb ( pno int primary key auto_increment, pname varchar(20) not null, pnumber int, prole varchar(10) default '타자', tno int, constraint fk_player_tb foreign key (tno) references team_tb (tno) on delete set null ) charset=utf8mb4; select * from player_tb; delete from team_tb where tno = 3;
    notion image
     
    notion image
    notion image

    6. 삭제 (실패)

    -- 5. 삭제 (실패) - 포린키 제약 조건 때문에 삭제가 안됨 -- (1) 참조하고 있는 이승엽의 tno를 null 업데이트 후 삭제하면 잘됨 delete from team_tb where tno = 1;
    notion image
    notion image
    번역
    notion image
     
    Share article

    gyul

    RSS·Powered by Inblog