Multiple relations between two tables (User and Product) - database

Is it acceptable database design to have two different relations between two table.
For instance, the user can create a product and buy the product.
So there will be relation between the user and the product one to many where the product table has the UserId, while when the user buys a product it is a many to many relation which is created by joint table that has UserId and ProductId that represents the order.
It is a good design or there is another way of doing that ?

Related

Database schema for Customers, Products and Suppliers

I´m starting in database schema. I don´t have experience in it.
I am thinking of a platform for customers to have access to the types of products that suppliers offer.
So, I would like to know how to keep the database schema following the tables and their relationships.
The customer will be registered in the system and he will have access to suppliers and their products.
Costumer>>>>>>>>>Product>>>>>>>>>Supplier
I already thank anyone who can help.
greetings
Ed
Based on the data you've given.
You would have a Product table, a product can be sold by one or more supplier. So you would have a Supplier table. Since this is a many to many relationship you would have a cross reference table
Customer's wouldn't be related to Products or suppliers.
Customers generally don't have a direct relationship with Products. Generally, customers create orders, composed of multiple products.
So customers typically have a one to many relationship with an 'Orders' table. The Orders table has a one to many relationship with an OrderItems table. That table might store quantity, color, etc, and has a many to many relationship with the Products table.
Of course, that's just the typical entity relationship. Without more information, it's hard to know if it meets your needs or not.

Laravel DB design: how to model different types of similar entity

First of all I have to mention that I am modernising our ERP system that is build in-house. It handles everything from purchasing of parts, sale orders, quotes and inventory to invoicing and statistical data. The system is web based and heavily dependent on ORM. EloquentORM will be used in the redesign.
My main question is about the data model of certain entities that are very similar. Currently three of most widely interconnected entities in the app are: Orders, Products and Invoices.
1. Orders
In current DB design I have one big orders table in which there is a order_type attribute to distinct between different order types: Purchase orders, Sale orders, Quotes and Service orders. About 80% of fields are common to each order type and there are some specific fields for each order types. Currently at ~15k records.
2. Products
Similarly I have one big products table with an attribute product_type to distinct between different product types: Finished products, Services, Assemblies and Parts. Again there is a fair % of fields that are common throughout all product types and some that are specific to different product type Currently at ~7k records.
3. Invoices
Again one table invoices with invoice_type attribute to distinct between 4 invoice types: Issued invoices (for things we sell), Received invoices (for things we buy), Credit notes and Avans Invoices. More or less all invoice types use the same fields. Currently at ~15k records.
I am now trying to decide which is the optimal way for this kind of DB model. I see three options:
1. Single Table Inheritance
Leave as is, everything in the same table. It feels kind of awkward to always filter records like where order_type = 'Sale order' to display right orders in the right place in GUI... Also when doing sale and purchase analytics I need to include the same where condition to fetch right orders. Seems wrong!
2. Class Table Inheritance
Have a master tables orders, products and invoices with common set of fields between each of entity types and then one-to-one child relation tables for every different type of each entity: sales_orders, purchase_orders, quote_orders, finished_products, reseller_products, part_products, assembly_products, received_invoices and issued_invoices with FK in each of the child tables to master table... This seems like a good idea but handling that with ORM brings in a little more complexity...
In this method I have a questions which FK should be used around. For example each invoice can belong to one order. Received invoice will go with Purchase order and issued invoice will go with Sale order. Should the master orders table's PK be used as a FK in the master invoices table to relate these entities, or should the child sale_orders PK be used in the child issued_invoices?
3. Concrete Table Inheritance
Having completely separated tables for every type of each entity. This would avoid me having parent->child relationship between master table but would result in a lot of similar attributes in each table...
What would be the best approach? I am aiming at ease of use in EloquentORM and also speed and scalability for the future.

How do I create a table in SQL Server that stores multiple values for one cell?

Suppose I have a table for purchase orders. One customer might buy many products. I need to store all these products and their relevant prices in a single record, such as an invoice format.
If you can change the db design, Prefer to create another table called PO_products that has the PO_Id as the foreign key from the PurchaseOrder table. This would be more flexible and the right design for your requirement.
If for some reason, you are hard pressed to store in a single cell (which I re-iterate is not a good design), you can make use of XMLType and store all of the products information as XML.
Note: Besides being bad design, there is a significant performance cost of storing the data as XML.
This is a typical example of an n-n relationship between customer and products.
Lets say 1 customer can have from 0 to N products and 1 products can be bought by 0 to N customers. You want to use a junction table to store every purchase orders.
This junction table may contain the id of the purchase, the id of the customer and the id of the product.
https://en.wikipedia.org/wiki/Many-to-many_(data_model)

What kind of normalization is used here in database structure?

Hello I have a Database like three tables:
Customer: Customer ID, Customer Name, Customer Surname
Product: Product ID, Product Name, Product Price
Now I have another table
CustomerProduct: Customer ID, Product ID
Here are all bought stuff standing, while taking the customer id and product id.
I just ask what kind of normalization of database structure this is?
Can you give me an explanation ?
Such a table, which handles many-to-many relationships by storing the two related primary keys (as foreign keys) is called by various names, but junction table seems to be the standard (I personally use association table).
Most relational databases do not handle many-to-many relationships natively - you need an extra table like this.

Can a relation has a relation with another relation

can we have some thing like following ER?
Is it a technical fault or not?
To define a relationship there should be entity by the help of them you can show relation. so it is impossible to make relation without entity.
In your case if you want to use 2 relationship there should be 1 entity between them.
Example of real life
suppose you have two relation teacher and student. you can not say like i am teacher and student of XYZ.
But you can say like i am teacher of Xyz and student of abc.
Typr of relationship
One-to-many relationships
The most common relationship used when creating relational databases. A row in a table in a database can be associated with one or (likely) more rows in another table. An example of a one-to-many relationship is a single order has many items on that order. And since relationships work both ways it is not uncommon to hear reference to many-to-one-relationships as well.
One-to-one relationship
A row in a table is associated to one and only one row in another table. An example of a one-to-one relationship is a person can have one social security number and a social security number can only be assigned to one person.
In most cases there is no need for a one-to-one relationship as the contents of the two tables can be combined into one table.
Many-to-many relationships
When one or more rows in a table are associated with one or more rows in another table. An example of a many-to-many relationship is a table of customers who can purchase many different products and a table of products that can be purchased by many different customers.

Resources