코드 1-12 Products와 ProductAttributes 테이블 간 관계 생성

    CREATE TABLE Products (

      ProductNumber int NOT NULL PRIMARY KEY,

      ProdDescription varchar(255) NOT NULL

    );

     

    CREATE TABLE ProductAttributes (

      ProductNumber int NOT NULL,

      AttributeName varchar(255) NOT NULL,

      AttributeValue varchar(255) NOT NULL,

      CONSTRAINT PK_ProductAttributes

        PRIMARY KEY (ProductNumber, AttributeName)

    );

     

    ALTER TABLE ProductAttributes

      ADD CONSTRAINT FK_ProductAttributes_ProductNumber

        FOREIGN KEY (ProductNumber)

          REFERENCES Products (ProductNumber);


     

    속성 값을 컬럼이 아닌 로우로 저장해 문제를 해결한 것처럼 보이지만, 특정 속성의 특정 제품을 추출하는 쿼리는 훨씬 복잡해졌다. 특히 다뤄야 할 속성이 여러 개라면 더욱 그렇다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.