-
2023 - 07 - 13 쿼리문 Query 컬럼 (column, field) 생성, 추가, 수정, 삭제Today I Learned/TIL 07 2023. 7. 13. 15:10
컬럼 추가
ALTER TABLE dbo.emp ADD email VARCHAR(25)
컬럼 삭제
ALTER TABLE dbo.emp DROP COLUMN email
데이터 사이즈변경 (VarChar 25 => 100)
ALTER TABLE dbo.emp ALTER COLUMN email VARCHAR(100)
데이터 타입 변경
ALTER TABLE dbo.emp ALTER COLUMN email TEXT
ALTER TABLE [테이블명] ALTER COLUMN [칼럼명] [변경할 데이터 타입]
Not Null 설정 또는 제거
-- NOT NULL 설정 ALTER TABLE dbo.emp ALTER COLUMN email VARCHAR(25) NOT NULL -- NOT NULL 제거 ALTER TABLE dbo.emp ALTER COLUMN email VARCHAR(25)
기존 칼럼에 NOT NULL을 설정할 경우 테이블에 NULL로 입력된 데이터가 있으면 오류가 발생한다. 해당 칼럼에 NULL이 존재하지 않도록 UPDATE를 수행 후 변경해야 한다.
DEFAULT 값 설정 또는 제거
-- DEFAULT값 설정 ALTER TABLE dbo.emp ADD CONSTRAINT df_emp_email DEFAULT 'None' FOR email -- DEFAULT값 제거 ALTER TABLE dbo.emp DROP CONSTRAINT df_emp_email
ALTER TABLE [테이블명] ADD CONSTRAINT [제약조건명] DEFAULT [기본값] FOR [칼럼명]
제약조건 명은 임의로 부여하면 된다.
PK 생성 또는 삭제
-- PK 생성 ALTER TABLE dbo.emp ADD CONSTRAINT pk_emp PRIMARY KEY (empno) -- PK 삭제 ALTER TABLE dbo.emp DROP CONSTRAINT pk_emp
ALTER TABLE [테이블명] ADD CONSTRAINT [PK명] PRIMARY KEY ([칼럼1], [칼럼2] ...)
칼럼 추가 (Not Null, Default 설정)
-- 컬럼 추가 (NOT NULL 설정) ALTER TABLE dbo.emp ADD email VARCHAR(25) NOT NULL -- 컬럼 추가 (DEFAULT 값 설정) ALTER TABLE dbo.emp ADD email VARCHAR(25) DEFAULT 'None' -- 컬럼 추가 (NOT NULL 설정 + DEFAULT 값 설정) ALTER TABLE dbo.emp ADD email VARCHAR(25) NOT NULL DEFAULT 'None'
'Today I Learned > TIL 07' 카테고리의 다른 글
2023 - 07 - 15 쿼리문 Query - 테이블 생성, 수정, 삭제, 조회 (0) 2023.07.18 2023 - 07 - 14 쿼리문 Query - 데이터 생성, 수정, 삭제, 조회 (0) 2023.07.18 2023 - 07 - 12 레이어드 아키텍쳐 Layered Architecture Pattern (1) 2023.07.12 2023 - 07 - 11 테스트코드 (Test Code) (0) 2023.07.11 2023 - 07 - 10 객체지향 설계 5가지 원칙 SOLID (0) 2023.07.08