简体   繁体   中英

Whats kind of relationship should I create ? One-To-One or One-To-Many?

I am using MS-Access database.

I am trying to make relationship with two tables, Old Customer table having data and Newly added coupon table.

As my client want to introduce new concept of coupon, where customer come with coupon instead of giving cash.

I have inserted Coupon code in coupon table in bulk.

Now, I am confused about what kind of relationship I should create with these two tables ?

I have to consider the below things...

  1. customer can give either cash or coupon.

  2. IF customer show the coupon, there will be an entry in CouponID column as well in cash column (to know the value of that coupon.)

  3. The CouponID should be unique in the customer table, Coupon Code should not be repeated.

I am confused whether it should be one-To-One or One-To-Many ?

This image will help you to understand the problem. 在此处输入图片说明

I would not include "CouponID" in the customer table at all (nor "Cash" for that matter). The customer table models a customer, the coupon table models a coupon. You need another table to model the transaction:

[CustomerTransaction] id date customer_id coupon_id

etc...

Every type of independent "thing" should be modeled by a discrete table. and "things" should be related to each other by other tables that create the 1:N relationship.

The relationship of customer to coupon is an optional (ie nullable) one-to-one; your data model looks good.

Some other comments:

  • The table would be better named sale rather than customer , since if the same customer comes back again, there will be a new row (but with the same name)
  • You could create a unique index on couponID that ignores nulls
  • You could rename Cash to Amount ; the amount is either "cash" or coupon - the couponID column tells you the type of the amount

To create a unique index that ignores nulls:

CREATE UNIQUE INDEX idx1 ON customer (couponID) WITH IGNORE NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM