How to design database that handle Order, OrderItem, Return, Refund, Exchange? -
most questions on internet focus on order, orderitem. there're few question designing database comprehensively handle aspects of online retail (order, orderitem, return, refund, exchange).
i know data model.
product (productid, name, etc) order (orderid, date, totalcost, etc) orderitem (orderid, productid, quantity, unitprice, etc)
based on above structure, how can manage return, refund, exchange?
i noticed when return/exchange item on super market, staff there regenerates new invoice. way handle return, refund, exchange?
- (f table.column) means foreign key pointing table.column
- (p) means primary key
- (u) means unique key
here tables , example data...
addresses id unsigned int(p) line1 varchar(50) line2 varchar(50) // allow null city_id unsigned int(f cities.id) zip varchar(6) // 5 digits , mx, 6 characters (x9x9x9) ca zip4 char(4) // allow null +----+-----------------+-------+---------+--------+------+ | id | line1 | line2 | city_id | zip | zip4 | +----+-----------------+-------+---------+--------+------+ | 1 | 123 main street | apt | 17 | 92101 | 1234 | | 2 | 345 east street | null | 25 | t1x0l3 | null | | .. | ............... | ..... | ....... | ...... | .... | +----+-----------------+-------+---------+--------+------+ cities id unsigned int(p) state_id unsigned int(f states.id) name varchar(50) ... +----+----------+-----------+-----+ | id | state_id | name | ... | +----+----------+-----------+-----+ | .. | ........ | ......... | ... | | 17 | 130 | san diego | ... | | .. | ........ | ......... | ... | | 25 | 14 | calgary | ... | | .. | ........ | ......... | ... | +----+----------+-----------+-----+
see iso 3166-1
countries id char(2)(p) iso3 char(3)(u) iso_num char(3)(u) name varchar(45)(u) +----+------+---------+---------------+ | id | iso3 | iso_num | name | +----+------+---------+---------------+ | .. | .... | ....... | ............. | | ca | can | 124 | canada | | .. | .... | ....... | ............. | | mx | mex | 484 | mexico | | .. | .... | ....... | ............. | | | usa | 840 | united states | | .. | .... | ....... | ............. | +----+------+---------+---------------+
see php's crypt() function hashing password.
customers id unsigned int(p) first_name varchar(50) middle_name varchar(50) // allow null last_name varchar(50) email varchar(255) username varchar(32) password varbinary(255) // hashed ... +----+------------+-------------+-----------+----------------------------+-----------+----------+-----+ | id | first_name | middle_name | last_name | email | username | password | ... | +----+------------+-------------+-----------+----------------------------+-----------+----------+-----+ | 1 | john | quincy | public | jqp@privacy.com | johnqball | xxxxxxxx | ... | | 2 | jane | null | doe | ladyinred@chrisdeburgh.com | janeykins | xxxxxxxx | ... | | .. | .......... | ........... | ......... | .......................... | ......... | ....... | ... | +----+------------+-------------+-----------+----------------------------+-----------+----------+-----+ customers_addresses id unsigned int(p) customer_id unsigned int(f customers.id) address_id unsigned int(f addresses.id) orders id unsigned int(p) customer_id unsigned int(f customers.id) bill_address_id unsigned int(f addresses.id) ship_address_id unsigned int(f addresses.id) created datetime shipped datetime ... +----+-------------+-----------------+-----------------+---------------------+---------------------+-----+ | id | customer_id | bill_address_id | ship_address_id | created | shipped | ... | +----+-------------+-----------------+-----------------+---------------------+---------------------+-----+ | 1 | 1 | 1 | 1 | 2012-12-31 23:59:59 | 2013-01-01 00:00:00 | ... | +----+-------------+-----------------+-----------------+---------------------+---------------------+-----+ orders_products id unsigned int(p) order_id unsigned int(f orders.id) product_id unsigned int(f products.id) quantity unsigned int unit_price double ... +----+----------+------------+----------+------------+-----+ | id | order_id | product_id | quantity | unit_price | ... | +----+----------+------------+----------+------------+-----+ | 1 | 1 | 1 | 1 | 12.34 | ... | | 2 | 1 | 2 | 13 | 1.78 | ... | | .. | ........ | .......... | ........ | .......... | ... | +----+----------+------------+----------+------------+-----+ products id unsigned int(p) name varchar(50) price double ... +----+----------+-------+-----+ | id | name | price | ... | +----+----------+-------+-----+ | 1 | widget 1 | 12.34 | ... | | 2 | widget 2 | 1.78 | ... | | .. | ........ | ..... | ... | +----+----------+-------+-----+ returns id unsigned int(p) order_product_id unsigned int(f orders_products.id) quantity unsigned int ... +----+------------------+----------+-----+ | id | order_product_id | quantity | ... | +----+------------------+----------+-----+ | 1 | 1 | 1 | ... | | .. | ................ | ........ | ... | +----+------------------+----------+-----+
see iso 3166-2
states id unsigned int(p) country_id char(2)(f countries.id) code char(2) // ab, al, nl, etc. name varchar(50) // alberta, alabama, nuevo leon, etc. ... +-----+------------+------+------------+-----+ | id | country_id | code | name | ... | +-----+------------+------+------------+-----+ | ... | .......... | .... | .......... | ... | | 14 | ca | ab | alberta | ... | | ... | .......... | .... | .......... | ... | | 72 | mx | ch | chiapas | ... | | ... | .......... | .... | .......... | ... | | 130 | | ca | california | ... | | ... | .......... | .... | .......... | ... | +-----+------------+------+------------+-----+
returns, refunds , exchanges return - customer returning product. how handle depends on business rules...
Comments
Post a Comment