Question

Hi i am designing a website which is going to list 13,000 electronic products, first of all i want to be able to navigate through a side navigation which i have already created, would i store the categories of these electronic parts in the database? if so what is the best way to store categories? because i have some products with 3 tier categories for example

  1. Semiconductors
  2. Diode
  3. Transorb.

As you can see i am a bit confused about how to do this in the database correctly.

Was it helpful?

Solution

There are many ways to handle this. I'm suggesting this. First create two tables

CREATE TABLE ITEM(
ITEM_KEY VARCHAR(20) NOT NULL,
NAME VARCHAR(255),
TYPE_CODE VARCHAR(50) NOT NULL,
CONSTRAINT ITEM_TYPECODE_CK CHECK(TYPE_CODE IN('ITEM','CATEGORY')),
CONSTRAINT ITEM_ITEMKEY PRIMARY KEY(ITEM_KEY)
);

Table Item is for Products and categories.

CREATE TABLE ITEMLINK(
ITEM_KEY VARCHAR(20) NOT NULL,
LINKED_ITEM_KEY VARCHAR(20) NOT NULL,
TYPE VARCHAR(50) NOT NULL,
CONSTRAINT ITEMLINK_ITEMKEY_LINKEDITEMKEY 
             PRIMARY KEY(ITEM_KEY, LINKED_ITEM_KEY,TYPE),
CONSTRAINT ITEMLINK_TYPE_CK CHECK(TYPE IN( 'CIL','CCL'))
);

Item Link table is for hierarchy. Item key is parent and linked item key is child. CIL means Category item Link and CCL means Category Category Link.

INSERT INTO ITEM (ITEM_KEY, "NAME", TYPE_CODE) VALUES ('IT01', 'Transorb', 'ITEM');
INSERT INTO ITEM (ITEM_KEY, "NAME", TYPE_CODE) VALUES ('CT01', 'Semiconductors', 'CATEGORY');
INSERT INTO ITEM (ITEM_KEY, "NAME", TYPE_CODE) VALUES ('CT02', 'Diode', 'CATEGORY');

INSERT INTO RWITEMLINK (ITEM_KEY, LINKED_ITEM_KEY, "TYPE") VALUES ('CT01', 'CT02', 'CCL');
INSERT INTO RWITEMLINK (ITEM_KEY, LINKED_ITEM_KEY, "TYPE") VALUES ('CT02', 'IT01', 'CIL');  

Now you can iterate with ItemLink table to populate list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top