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

김주희's avatar
Feb 26, 2025
[DB] 7. 데이터 정의 (DDL)

1. CREATE Table

-- DDL (create, drop, alter) 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;
notion image
notion image
notion image
 
create table player_tb ( pno int primary key, pname varchar(10), pnumber int, prole varchar(10), tno int ) charset=utf8mb4; select * from player_tb; desc player_tb;
notion image
notion image
notion image
 
 

2. Alter Table (수정)

1. alter table 테이블명 change colum 기존 컬럼명 변경할 컬럼명 타입;

-- 2. alter table (수정) -- drop 하고 create table을 수정해서 새로 만드는게 낫다. alter table player_tb change column prole ptype varchar(20);
변경 전
notion image
변경 후 (alter 실행)
notion image

2. drop table 후 컬럼 수정 후 create table

drop table player_tb; create table player_tb ( pno int primary key auto_increment, pname varchar(20) not null, pnumber int, ptype varchar(20) default '타자', tno int ) charset=utf8mb4;
 

3. 설정으로 수정

notion image
notion image
 

3. Drop Table (삭제)

-- 3. drop table (삭제) drop table player_tb;
notion image
 

4. Truncate (테이블 비우기)

-- 4. truncate (테이블 비우기) truncate team_tb;
notion image
 

5. 제약조건들

  1. auto_increment: 1씩 값을 자동 증가
  1. not null: null 허용X
  1. default: 값을 넣지 않을 경우 default로 설정된 값이 들어간다.
-- 5. 제약조건들 drop table 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 ) 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
 
Share article

jay0628