π 2023λ 11μ 23μΌ
[DB 6μΌμ°¨]
DB ꡬ쑰λ₯Ό μ½μλ νκΈ°λ²μ μν΄ νννκ³ μ€κ³ν΄ λκ°λ κ³Όμ μ λ§νλ©° μ€λμ νλ£¨μ’ μΌ λͺ¨λΈλ§μ κ΄νμ¬ λ°°μ°κ³ μ€μ΅νλ€.
ERD(Entity Relationship Diagram) λ€μ΄μ΄κ·Έλ¨μ ν΅ν΄ λ°μ΄ν°λ² μ΄μ€μ ꡬ쑰λ₯Ό μκ°ν νλ©°, μμ§ μΆμμ μΈ λ¨κ³
κ°μ²΄(Entity)λ μ¬κ°νμΌλ‘ νννκ³ κ΄κ³(Relationship)λ λ§λ¦λͺ¨λ‘ νννλ€.
ꡬ체μ μΌλ‘ λ°μ΄ν°λ² μ΄μ€ μ€κ³. κ° λ°μ΄ν°μ νμ , κ΄κ³, keyλ±μ μ§μ νλ©° μμ λλ ERDλ₯Ό μ¬μ©νλ€.
pk = primary key
fk = foreign key
μ€μ λ°μ΄ν°λ² μ΄μ€λ₯Ό λ§λλ κ³Όμ μ΄λ©° SQLμ μμ±ν΄μ λ§λ λ€.
CREATE Database ORDER_SYSTEM; create table MEMEBERS( MEMEBERS_id BIGINT PRIMARY KEY AUTO_INCREMENT, -- ID MEMEBERS_role enum('USER', 'ADMIN', 'SELLER') NOT NULL DEFAULT 'USER', -- USER μ’ λ₯ MEMEBERS_NAME varchar(255) NOT NULL, -- USER μ΄λ¦ MEMEBERS_PHONE varchar(255) NOT NULL, -- USER μ’ λ₯ MEMEBERS_EMAIL varchar(255) NOT NULL -- USER λ©μΌ ); create table ITEMS( ITEMS_id BIGINT PRIMARY KEY AUTO_INCREMENT, -- ID ITEMS_NAME VARCHAR(255) NOT NULL, -- μν μ΄λ¦ ITEMS_PRICE BIGINT NOT NULL, -- μν κ°κ²© ITEMS_COUNT INT, -- μν μ¬κ³ μ SELLER_id BIGINT NOT NULL, -- ν맀μ ID FOREIGN KEY(SELLER_id) REFERENCES MEMEBERS(MEMEBERS_id) ON DELETE CASCADE ); create table ORDERS( ORDER_id BIGINT PRIMARY KEY AUTO_INCREMENT, -- κΈ°λ³Έν€ USER_id BIGINT NOT NULL, -- μ£Όλ¬Έν μ¬λμ id MEMEBERS_ADDRESS_id BIGINT NOT NULL, -- λ°°μ‘μ§ ORDERS_Progress enum('ꡬ맀μμ²', 'μν μ€λΉμ€', 'λ°°μ‘μ€', 'λ°°μ‘μλ£') NOT NULL DEFAULT 'ꡬ맀μμ²', -- μ£Όλ¬Έ μν FOREIGN KEY(USER_id) REFERENCES MEMEBERS(MEMEBERS_id), FOREIGN KEY(MEMEBERS_ADDRESS_id) REFERENCES MEMEBERS_ADDRESS(MEMEBERS_ADDRESS_id) ); create table ORDER_DETAILS( ORDER_DETAILS_id BIGINT PRIMARY KEY AUTO_INCREMENT, -- κΈ°λ³Έν€ ORDER_ITEM_COUNT INT NOT NULL, -- ꡬ맀 κ°―μ ORDER_id BIGINT NOT NULL, -- μ£Όλ¬Έ id ITEMS_id BIGINT NOT NULL, -- μν id FOREIGN KEY(ORDER_id) REFERENCES ORDERS(ORDER_id), FOREIGN KEY(ITEMS_id) REFERENCES MEMEBERS(MEMEBERS_id) ); create table MEMEBERS_ADDRESS( -- λ°°μ‘μ§κ° μ¬λ¬ κ³³μΌ κ°λ₯μ± MEMEBERS_ADDRESS_id BIGINT PRIMARY KEY AUTO_INCREMENT, MEMEBERS_SI VARCHAR(255) NOT NULL, MEMEBERS_DO VARCHAR(255) NOT NULL, MEMEBERS_DETAIL VARCHAR(255) NOT NULL, MEMEBERS_id BIGINT NOT NULL, FOREIGN KEY(MEMEBERS_id) REFERENCES MEMEBERS(MEMEBERS_id) ON DELETE CASCADE ); --- ν μ€νΈ μμ -- νμκ°μ (ν맀μ) : MEMEBERS INSERT -- νμκ°μ (ꡬ맀μ) : MEMEBERS INSERT -- μνλ±λ‘ (ν맀μ) : ITEMS INSERT -- μνꡬ맀 (ꡬ맀μ) -- λ°°μ‘μ§ μ λ ₯ : MEMEBERS_ADDRESS INSERT -- ꡬ맀λ΄μ© λ±λ‘ : ORDERS INSERT -- ꡬ맀μμ λ΄μ© λ±λ‘ : ORDER_DETAILS INSERT -- μν μ¬κ³ μμ : ITEMS UPDATE
λ§λλ κ²λ³΄λ€ μκ°νλλ° μκ°μ λ λ§μ΄ μλΉνλ€.
νΉν, κ΄κ³μ μμ΄ 1:1, 1:n, n:mμΈμ§ λ§μ μκ°μ λ§μ΄νκ³ λ λ°λμ΄ μμ΄μΌ νλ MandatoryμΈμ§ μλλ©΄ μκ±°λ μμ΄λ μκ΄μλ OptionalμΈμ§ λ무λ ν·κ°λ Έλ€. μ΄λ²μλ λ°λμ μλ²½νκ² κ°λ
μ μ‘κ³ κ°κ³ μ νλ€.