one to one and one to many relation with same entity mysql - mysql

I have two entities Invoice and User.
Invoice have following relations with user
invoice have a owner(one to one relation with user)
one invoice can share with multiple users, it means invoice can have multiple shared users(one to many relation with user)
How to map these relations in mysql database? how many tables? and table structure?

invoice could have two fields, billed_to and payed_by. your billed_to field would be your one to one relationship, and the payed_by field would be your one to many.

For One-to-one relationship, it is advised not to keep separate table. Keep it with the invoice table itself.
For one-to-many relationship, keep another mapping table.
Your table structure should be
Users
----------
id | xxx | .....
-
Invoices
------------
id | user_id | .....
in the above case user_id is the owner.
shared_invoices
-------------------
id | user_id | invoice_id

Related

Mysql table relation for a user with multiple business

How to properly implement mysql table relation such that a user can create multiple business and each of those business can have multiple users. thanks
Basically going to be two (possibly 3 tables):
Business Table:
business_id int (this will be the primary key)
business_name
whatever extra fields that relate to the business
User Table:
user_id whatever type (this will be the primary key)
user_name
whatever extra fields that relate to the business
The relational part you can implement in one of two ways:
add the business id to the user table. So user table will have an additional field business_id and will relate back to one of the rows in the business table. This implies a user can only relate to 1 business.
If you want a user to be able to relate to one or more businesses then you need a separate table:
Business_User Table:
business_id int
user_id (whatever type you picked)

SQL - Insert multiple categories to column

I have a table where I store products, and in the same table I store the ID of the category I want the product to be in.
My product table looks like this:
id | product_name | category_id | price
And then I have my category table:
id | category
My problem is to know how I can insert multiple categories into my category_id, and if it is possible.
If not, whats the best way that I could do it?
It is possible but you really don't want to go there. Storing multiple values in a single datarow column is a terrible idea in 99.99999% of the cases.
For more information, read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
What you want to do is to add another table to store the relationship between the products and the categories. This is referred to as a many to many relationship.
This new table should hold the product id in one column and the category id in the other one, and have a composite primary key that is the combination of both these columns.
This way, you can have many products in the same category, and many categories for the same product.
You need a many to many relationship, which mean you need a third table let's call it ProductCategories, in this case you will have
Products
id | product_name | category_id | price
Category
id | category
ProductCategories
PruductID | CategoryID (combined PK)

Multiple values in one entry

I am having a problem while setting my database.
The context: I have a company table that can have 0, 1 or multiples jobs. Jobs have its own table. I will need to join both table.
How can I "say" that a company has this, this and this job?
The only way I found is by creating a "job" value in the meta_key entry (of the company table) and declared the jobs in the meta_value entry but it seams not good (and will create difficulty to retrieve the jobs while joining).
You are describing a many to many relation. It works like this
company table
-------------
id
name
other columns
jobs table
----------
id
name
other columns
company_jobs table
------------------
company_id
job_id

Table item many relationship

I know this is not code realated but wasnt sure where to post it. If this is the wrong spot please let me know where to post as opposed to just a down vote. Basically I am setting up my first DB on my own. I am a little conffused on how to set some tables up. I understand the basic relational model by using a unique ID to pair up tables but what about an item in a table that has many relations.
Examble:
User Table
ID | Name | Email
Product Table
ID | Name | Price | User_ID
Now I know I can pair the 2 using the User_ID in the product table but what if the product can belong to multiple users? How do I associate the the product by using the User_ID to multiple users?

How to map two tables in database PHPMyAdmin

I'm working on a project in which a user can save their own coupon codes on the website, so I want to know what is the best way to do that.
Lets say, I have 1 table with the users, like this:
userId | firstName | lastName | codeId
And then I have a table of the coupon codes, like this:
codeId | codeNumber
So what I can do is to connect the codeId to userId so when someone saves the coupons the codeId from the coupon table goes into the codeId of the users table.
But now what do I do if when user has multiple coupons? How should they be connected to the user?
I have 2 options what to do:
Saving the codeId from coupons table into the codeId of users table like 1,2,3,4,5,
Make a new row into the coupons table and connect the user to the code by adding another field in the coupon table userId and putting into it the userId of the user which has added the coupon.
So which of the two options is better?
A many-to-many relationship between two tables should be solved by adding a third table connecting the two:
user_coupons
- userId
- couponId
Your first option violates the 1-NF rule, where it says that no column will have a group of values.
This is a typical one-to-many relationship. Your second option of creating a separate table User_Coupons (not the the underscore) and create a row with UserID, CouponCode, for each association. The two columns together should be unique so that they both become the primary key of the associative table.
If i was you my database will be:
userId | firstName | lastName
codeNumber | userId
You can insert many codeNumber for any user if you want.