테이블 생성 (CREATE TABLE)
고객 테이블 생성
CREATE TABLE 고객 (
고객아이디 VARCHAR(20) NOT NULL,
고객이름 VARCHAR(10) NOT NULL,
나이 INT,
등급 VARCHAR(50) NOT NULL,
직업 VARCHAR(20),
적립금 INT DEFAULT 0,
PRIMARY KEY(고객아이디) // 기본키
)
제품 테이블 생성
CREATE TABLE 제품 (
제품번호 CHAR(3) NOT NULL,
제품명 VARCHAR(20),
재고량 INT,
단가 INT,
제조업체 VARCHAR(20),
PRIMARY KEY(제품번호),
CHECK (재고량 >= 0 AND 재고량 <= 10000)
)
주문 테이블 생성
CREATE TABLE 주문 (
주문번호 CHAR(3) NOT NULL,
주문고객 VARCHAR(20),
주문제품 CHAR(3),
수량 INT,
배송지 VARCHAR(30),
주문일자 DATE,
PRIMARY KEY(주문번호),
FOREIGN KEY(주문고객) REFERENCES 고객(고객아이디),
FOREIGN KEY(주문제품) REFERENCES 제품(제품번호)
)
배송업체 테이블 생성
CREATE TABLE 배송업체 (
업체번호 CHAR(3) NOT NULL,
업체명 VARCHAR(20),
주소 VARCHAR(100),
전화번호 VARCHAR(20),
PRIMARY KEY(업체번호)
)
테이블 변경(ALTER TABLE)
고객테이블에 가입날짜 속성 추가
ALTER TABLE 고객 ADD 가입날짜 DATE
고객테이블에 가입날짜 속성 삭제
ALTER TABLE 고객 DROP COLUMN 가입날짜
고객테이블에 20세 이상의 고객만 가입할 수 있다는 데이터 무결성 제약조건 추가
ALTER TABLE 고객 ADD CONSTRAINT CHK_AGE CHECK(나이 >= 20)
고객테이블의 제약조건 삭제
ALTER TABLE 고객 DROP CONSTRAINT CHK_AGE
테이블 삭제(DROP TABLE)
배송업체 테이블 삭제
DROP TABLE 배송업체