-1

I have the following tables - client Invoice. - Workshop invoice. - Owner invoice. - Owner withdraws money. - payment

Relationship with payment

  • client Invoice: one invoice can have many payments --> one to many relation.
  • Workshop invoice: one invoice can have many payments --> one to many relation.
  • Owner invoice: one invoice can have many payments --> one to many relation.
  • Owner withdraws money: one invoice can have one payment--> one to one relation.

The payment table will include all data of payments (huge amounts of data). the problem with the current design is that when a payment is created for any type of invoice their will be 2 unused columns referencing the other invoice tables. an obvious enhancement is removing all the FK keys from the payment table then add to the tables paymentID as an FK. this will lead to huge repetition due to placing the FK in a "one to many relation" in the parent table. My current design is included below.

owner withdraw table

  • owner_Withdraw_ID Primary int(11)
  • user_ID Primary Index int(11)
  • payment_ID
  • Primary Index int(11)
  • Date_Added date
  • amount int(11)
  • Description varchar(255)

client Invocie table

  • IDIndex int(11)
  • row_IDPrimary int(11) AUTO_INCREMENT
  • product_ID Primary Index int(11)
  • client_ID Primary Index int(11)
  • quantity_of_Product int(11)
  • Description varchar(600)
  • Date_Added date
  • VAT int(11)
  • discount int(11)
  • total_Price int(11)

invoice owner table

  • invoice_O_ID Primary int(11) AUTO_INCREMENT
  • user_ID Primary Index int(11)
  • substance_ID Primary Index int(11)
  • direction varchar(30)
  • Invoice_Date date
  • Quantity int(11)
  • price int(11)
  • VAT int(11)
  • Description varchar(255)

invoice workshop

  • IDPrimary int(11)
  • workshop_ID PrimaryIndex int(11)
  • Description varchar(1000) utf8_general_ci
  • total_Price int(11)
  • Date date
  • VAT int(11)
  • discount int(11)

payment table

  • payment_ID int(11)
  • row_ID Primary int(11) AUTO_INCREMENT
  • workshop_Invoice_ID Index int(11) FK from workshop Invoice
  • client_Invoice_ID Index int(11) FK from client Invoice
  • owner_Invoice_ID Index int(11) FK from owner Invoice
  • owner_Withdraw_ID Index int(11) FK from owner withdraw money
  • payment_IN tinyint(1) if true money in the system else money out of system
  • amount_Paid int(11)
  • statues tinyint(1) true when paid total money
  • Date_Added date
  • Description varchar(600) utf8_general_ci
  • I am still new to stack overall I am not familiar with the styling syntax and I couldn't post images in the question I could link an image. yet I preferred to keep all information visible – user4626745 Feb 01 '20 at 12:17
  • Please [use text, not images/links, for text--including tables & ERDs](https://meta.stackoverflow.com/q/285551/3404097). Use images only for what cannot be expressed as text or to augment text. Images cannot be searched for or cut & pasted. Include a legend/key & explanation with an image. Read the edit help re inline & block formats for code & quotations. Also re line breaks. – philipxy Feb 03 '20 at 13:20
  • Hi. What is the 1 (clear non-duplicate) specific question with a specific answer that you want to ask re the 1st place you are stuck? Give just enough specification & design necessary to ask. "Any help" is not an on-topic question. [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/3404097) "how to desing this" is too broad. ["best" mean nothing](https://meta.stackexchange.com/q/204461/266284) until you give exact criteria that others could use to all come up with the same answer. See [ask], other [help] links & the voting arrow mouseover texts. – philipxy Feb 03 '20 at 13:24
  • @philipxy The question is obvious as it is. yet I will break it down for you. what I need is a better database design for the tables I provided. The design in the question causes a huge amount of unused data if any payment is created 2 table columns include info that is not needed and this table will expand rapidly for it's the only way money is in or out. an alternative approach is to add paymentID as FK in the three tables mentioned. this reduces the data but it causes complexity in queries I am using PDO for security reasons. some queries didn't work well although it works in MySQL – user4626745 Feb 03 '20 at 14:21
  • @philipxy I need an enhancement that doesn't need complex queries to show the content of expenses, income, liability pages. earlier I had some problems with getting a select with a special order the query worked fine in MySQL yet in PDO the data was not ordered... the point here is that sometimes complex queries is not best friends with PDO – user4626745 Feb 03 '20 at 14:28
  • Please clarify via edits, not comments. Please edit your post per my comments to be more specific etc. Please in code questions give a [mre]. – philipxy Feb 03 '20 at 15:12
  • @philipxy thanks for your help the question is better now. – user4626745 Feb 04 '20 at 10:21
  • What is your question? PS What "huge repetition"? There will be some nulls in some rows. PS This seems to be about SQL/DB subtyping/inheritance. You are using an anti-pattern I call FK radio buttons--2/multiple FKs to 2/multiple tables. These are faqs. Before considering posting please always google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Feb 05 '20 at 11:36
  • @philipxy thanks for pointing out for object-oriented DB method to solve my problem. this is a useful way to solve my problem I came out with an easier way though. I didn't answer the question waiting for a better method. – user4626745 Feb 05 '20 at 21:58
  • Please do not be rude. The question uses the term "inheritance' which is refers to subtyping objects so the question is about subtyping and the answers are about subtyping & not particular to tables representing objects. – philipxy Feb 05 '20 at 23:36

1 Answers1

0

Your scenario seems to describe an operational system - essentially a sales application.
Your central entity/table is a sale. Each sale has line items - with description, quantity, price and VAT.

An invoice is also an entity, but it is not the same as sale, an invoice is issued and presents the sale to different parties - workshop, client, owner. You need to keep track of the sale in a separate table - imagine controlling a refund across your invoices.

So, I would start with a high level sale table

date, 
sale_id, 
user_id (user who made the sale), 
discount,
total_vat,    
total_price.

Then, create a second table sale_item

date,
sale_id,
item_id,
quantity,
substance,
descritption,
vat,
cost,
price,
discount.

If a sale changes, say the client wants to refund something, then you would only need to change two tables to capture this event. I would then create a table for the invoice

 - date,
   *invoice_id, 
   sale_id,* --PRIMARY KEY on this table 
   invoice_type, --client,owner,workshop 
   user_id.

For the same sale_id in table invoice, you could have up to three invoice_ids.

This is how I would approach this situation - obviously add and remove columns you need to describe the entities and events. Essentially, an invoice describes a sale in different ways.

beatbox
  • 21
  • 1
  • 5
  • hello their, I don't think that concatenating the tables is a good idea and yes this is a big system I've been working on this project for 3 months. my original plan was to find something close to the project on GitHub and edit it. Then the client asked for very specific tasks and the system now is a part of employee management, production and stock management, and a sales system. I had to build everything from scratch the total number of tables in my DB is 20. there is a total of 55 php and HTML . and nearly everything is connected. – user4626745 Feb 02 '20 at 21:32