Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have a scenario of of three classes .I am planning to make the database for it .The relationship between them is :
1 customer is related with many items and many dv-vouchers.
1 item is related with many customer and many dv-vouchers.
1 dv-vouchers is related with 1 customer and many items .
XML
public Customer
 {
int cust_id;
list<items> items;
list<dvouchers> dvouchers;
 }
public items
 {
int itm_id;
list<Customer>  customers;
list<dvouchers>  dvouchers;
 }
public dvouchers
 {
int dv_id;
Customer customer;
list<items> items;
 }

First of all what can be the design of database tables for above classes along with fk_constraints and relationship tables ?
Second Do I need to do both an Insert and Update the relationship tables along db table ? Please Help .
Posted
Updated 2-Dec-13 22:29pm
v9
Comments
agent_kruger 3-Dec-13 2:13am    
what actually you want to do with maintaining the relations?
Maciej Los 3-Dec-13 2:14am    
Is it proper design?
Customers table should store items and dvouchers; Items table should store customers and dvouchers and Dvouchers should store customers and items.
I would say: all in one, one in all.
Please, be more specific and describe more details. Use "Improve question" widget to upgrade your question.
Member 8840306 3-Dec-13 5:04am    
Now see my improved question.Hope it is clear now.

1 solution

Simple enough.
The answer to your second question is Yes.
SQL
CREATE TABLE customers (
    CustomerID NUMBER(8)
    )
/

ALTER TABLE customers ADD CONSTRAINT customers_pk PRIMARY KEY (CustomerID)
/

CREATE TABLE Items (
    ItemID NUMBER(8)
    )
/
ALTER TABLE Items ADD CONSTRAINT Items_pk PRIMARY KEY (CustomerID)
/

CREATE TABLE dvouchers (
    DVoucherID NUMBER(8),
    CustomerID NUMBER(8)
    )
/
ALTER TABLE dvouchers ADD CONSTRAINT dvouchers_pk PRIMARY KEY (CustomerID)
/

ALTER TABLE dvouchers
  ADD CONSTRAINT dvouchers_Customers_FK FOREIGN KEY (
    CustomerID
  ) REFERENCES Customers (
    CustomerID
  )
/

CREATE TABLE CustomerItems (
    CustomerID NUMBER(8),
    ItemID NUMBER(8)
    )
/

ALTER TABLE CustomerItems 
    ADD CONSTRAINT CustomerItems_pk PRIMARY KEY (
        CustomerID,
        ItemID
        )
/

ALTER TABLE CustomerItems
  ADD CONSTRAINT CustomerItems_Customer_FK FOREIGN KEY (
    CustomerID
  ) REFERENCES Customers (
    CustomerID
  )
/

ALTER TABLE CustomerItems
  ADD CONSTRAINT CustomerItems_Items_FK FOREIGN KEY (
    ItemID
  ) REFERENCES Items (
    ItemID
  )
/

CREATE TABLE dvoucherItems (
    dvoucherID NUMBER(8),
    ItemID NUMBER(8)
    )
/

ALTER TABLE dvoucherItems 
    ADD CONSTRAINT dvoucherItems_pk PRIMARY KEY (
        dvoucherID,
        ItemID
        )
/

ALTER TABLE dvoucherItems
  ADD CONSTRAINT dvoucherItems_dvouchers_FK FOREIGN KEY (
    dvoucherID
  ) REFERENCES dvouchers (
    dvoucherID
  )
/

ALTER TABLE dvoucherItems
  ADD CONSTRAINT dvoucherItems_Items_FK FOREIGN KEY (
    ItemID
  ) REFERENCES Items (
    ItemID
  )
/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900