본문 바로가기

SQL

할인 테이블 생성: 할인 정보를 저장하기 위한 테이블을 생성합니다.

반응형

할인 정보 테이블은 다양한 유형의 할인과 관련된 데이터를 관리하며, 이를 통해 상품 가격, 프로모션, 쿠폰, 할인율 등을 효율적으로 운영할 수 있습니다. 다음은 할인 정보를 저장하기 위한 테이블 생성을 위한 SQL 쿼리문 예제와 각각의 설명입니다.


1. 기본 할인 테이블 생성
CREATE TABLE discounts (
    discount_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    description TEXT,
    discount_percent DECIMAL(5, 2),
    start_date DATE,
    end_date DATE
);
설명: 할인의 이름, 설명, 할인율, 시작일, 종료일을 저장하는 기본 테이블입니다.

 

2. 상품별 할인 테이블 생성
CREATE TABLE product_discounts (
    product_discount_id INT AUTO_INCREMENT PRIMARY KEY,
    product_id INT,
    discount_id INT,
    FOREIGN KEY (product_id) REFERENCES products(product_id),
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 각 상품에 적용된 할인의 ID를 연결합니다.

 

3. 카테고리별 할인 테이블 생성
CREATE TABLE category_discounts (
    category_discount_id INT AUTO_INCREMENT PRIMARY KEY,
    category_id INT,
    discount_id INT,
    FOREIGN KEY (category_id) REFERENCES categories(category_id),
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 각 카테고리에 적용된 할인의 ID를 연결합니다.

 

4. 할인 코드 테이블 생성
CREATE TABLE discount_codes (
    code_id INT AUTO_INCREMENT PRIMARY KEY,
    code VARCHAR(50),
    discount_id INT,
    usage_limit INT,
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 할인 코드, 할인 ID, 사용 제한 횟수를 저장합니다.

 

5. 회원 등급별 할인 테이블 생성
CREATE TABLE member_discounts (
    member_discount_id INT AUTO_INCREMENT PRIMARY KEY,
    member_level VARCHAR(50),
    discount_id INT,
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 회원 등급별 할인 정보를 저장합니다.

 

6. 시간대별 할인 테이블 생성
CREATE TABLE time_based_discounts (
    time_discount_id INT AUTO_INCREMENT PRIMARY KEY,
    discount_id INT,
    time_range_start TIME,
    time_range_end TIME,
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 특정 시간대에만 적용되는 할인 정보를 저장합니다.

 

7. 일시적 할인 이벤트 테이블 생성
CREATE TABLE flash_sales (
    flash_sale_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    discount_id INT,
    sale_start TIMESTAMP,
    sale_end TIMESTAMP,
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 짧은 기간 동안 진행되는 할인 이벤트 정보를 저장합니다.

 

8. 할인 사용 기록 테이블 생성
CREATE TABLE discount_usage (
    usage_id INT AUTO_INCREMENT PRIMARY KEY,
    discount_id INT,
    order_id INT,
    used_date TIMESTAMP,
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id),
    FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
설명: 할인이 사용된 기록(주문 ID, 사용 날짜)을 저장합니다.

 

9. 할인 조건 테이블 생성
CREATE TABLE discount_conditions (
    condition_id INT AUTO_INCREMENT PRIMARY KEY,
    discount_id INT,
    condition_description TEXT,
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 할인의 적용 조건을 설명하는 텍스트를 저장합니다.

 

10. 고객별 할인 적용 테이블 생성
CREATE TABLE customer_discounts (
    customer_discount_id INT AUTO_INCREMENT PRIMARY KEY,
    customer_id INT,
    discount_id INT,
    applied_date DATE,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
    FOREIGN KEY (discount_id) REFERENCES discounts(discount_id)
);
설명: 특정 고객에게 적용된 할인과 적용 날짜를 저장합니다.

 

관련 전문용어 설명
CREATE TABLE: 새로운 테이블을 생성하는 SQL 명령어입니다.
INT, VARCHAR, DATE, DECIMAL, TIMESTAMP, TEXT: 데이터 유형을 나타냅니다.
AUTO_INCREMENT: 자동으로 숫자를 증가시키는 속성입니다.
PRIMARY KEY: 각 행을 고유하게 식별하는 주 키입니다.
FOREIGN KEY: 다른 테이블의 주 키를 참조하는 외래 키입니다.

반응형