How do i link database relationship with the following information? - php

I have been relentlessly trying to edit my database relationships but to no avail. Here is the scenario:
i have an inventory table. an inventory can be classified into 2 categories(technical eg. guns, radios and general eg. uniforms). i was hoping there could be a way for me to add details(from different tables eg. technical_inventory_table and general_inventory_table) to the inventory table through the category since the two categories have different fields. is there any way for me to do that? please do note that a technical inventory will be listed individually since a single item will have a serial number attached to it.
or does the answer provided here - https://dba.stackexchange.com/questions/33099/storing-different-products-and-joining-on-table-names - be an already good solution for my problem? i would like to avoid creating a table where it will be made up of what usually are the fieldnames as described in the link to prevent confusion within my team.
thanks for any help in advance

Related

Many to Many Relationship between users and products - Laravel

I am creating a database for to manage companies and their products(category) and customers. I have designed my database but i want to be sure if my table does not foul the rules of database schema.
users table and product table are linked many to many (having a pivot product_user).
In that instance,
user 1 can have enter a product called hardware
so in the pivot table, product_id : 1 & user_id : 1
user 2 can also enter the same product called hardware
so in the pivot table, product_id : 2 & user_id : 2
Is it a good way to do this? I am new to laravel now. Please help. Thank you
Yes this is a good way, but I would suggest you use "company" instead of "user". This way the naming is clear for everyone.
You might want to read the official documentation of relationships. The examples are also very useful and easy to understand.
Beside the fact that this is not a PHP nor a Laravael nor a MySQL (as a product) question, the real question is what should this many to many relationship stands for?
This is all about general database schema design. We can't tell you if this is right or wrong because we don't know what its meant to be.
If you you can build sentences like "A User has/have zero, ore or more Products" and at the same time "A Product is used by/belong to zero, one or more Users" than this is a good indicator that a Many to Many Relationship is required.
But there could be exceptions where the many to many relation ship is more than just a linking table. Because if a user bought a product than this is most likely a litle bit more complex.
e.g. at an online shop a user can do one ore more purchases, but one purchase belongs only to one user. The purchase do include one or more Products, and the a Product can be part of one ore more purchases. And than there is billing and shipping and all the stuff involved. In this case you could do a direct relation between user and products, but what dose it tell you? What if a user buy a product more than once?
There are situations where you could think that a many to many would be a good idea but in fact it is not (see above) because you create redundant data and/or break data integrity. The Process to avoid this is called normalization: https://en.wikipedia.org/wiki/Database_normalization
A good example of an many to many relation ship is User and Privileges.
There are users and there are privileges that an user can have. So a user can have zero, one or more Privileges and a privilege can be applied to zero, one or more Users.
So if you ask if product_user is a good way to do it the real answer is: it depends!

Data model for subscriptions, single purchase products and variable services

I am designing a database for a system that will handle subscription based products, standard one off set price purchase products, and billing of variable services.
A customer can be related to many domains and a domain may have many subscriptions, set priced products or billed variable services related to it.
I am unsure whether to seperate each of these categories into their own 'orders' table or figure out a solution to compile them all into a single orders table.
Certain information about subscriptions is required such as start date or expiry date which is irrelevant for stand alone products. Variable services could be any price so having a single products table would mean I would have to add a new product which may be never used again or might be at a different cost.
What would be the best way to tackle this, and is splitting each into seperate order tables the best way?
Have you looked at sub-typing - it might work for you. By this I mean a central "order" table containing the common attributes and optional "is a"/one-to-one relationships to specific order tables. The specific order tables contain the attributes specific only to that type of order, and a foreign key back to the central order table.
This is one way of trying to get the best of "both" worlds, by which I mean (a) the appropriate level of centralisation for reporting on things common and (b) the right level of specialisation. The added nuance comes at the cost of an extra join some times but gives you flexibility and reporting.
Although I am a little biased in favour of subtypes, some people feel that unless your order subtypes are very different, it may not be worth the effort of separating them out. There is some seemingly good discussion here on dba.stackexchange as well as here. That being said, books (or chapters at least) have been written on the subject!
As with any data model, the "best" way depends on your needs. Since you don't specify a lot of details regarding your specific needs, it's difficult to say what the bets model is.
However, in general you need to consider what level of detail is necessary. For example if all subscriptions cost the same and are billed on the 1st of the month, it may be sufficient to have a field like is_subscription ENUM ('Y', 'N') in your orders table. If billing dates and prices for subscriptions can vary however, you need to store that information too. In that case it may be better to have a separate table for subscriptions.
Another consideration is exactly what an "order" represents in your model. One interpretation is that an order includes all the purchases included in one transaction, including both one-off purchases, variable services and subscriptions. A completed order would then result in a bill, and subscriptions would be automatically billed on the proper day of the month without a new order being made.
You should aim to have one database design that is not hardwired into specifics regarding its contents, and if it does (have to) it does in such a way that it seperates the specialization from the core DB design.
There are certain fields that are common for each order. Put these in one table, and have it refer to the other rows in the respective (specialized) tables. Thats DB normalization for you.
You could have main table contain ID, OrderID, ItemType, ItemID when ItemType determines the table ItemID refers to. I advise against this, but must admit that i use this sometimes.
Better would be to have these tables:
Clients: ID, Name, Address, Phone
Sellers: ID, Name, CompanyAlias
Orders: ID, ClientID, SellerID, Date, Price
OrderItems: ID, OrderID, DiscountAmount, DiscountPercentage,
ProductDomainID, ProductBottleID, ProductCheeseID, ..
Now OrderItems is where the magic happens. The first four fields explain themselves i guess. And the rest refers to a table which you do not alter or delete anything ever:
Products_Cheese ID, ProductCode, ProductName, Price
And if you do need a variant product for a specific order add a field VariantOfID thats otherwise NULL. Then refer to that ID.
The OrderItems table you can add fields to without disturbing the established DB structure. Just constrict yourself to use NULL values in all Product*ID fields except one. But taking this further, you might even have scenario's where you want to combine two or more fields. For example adding a ExtraSubscriptionTimespanID or a ExtraServicelevelagreementID field that is set alongside the ProductCheeseID.
This way if you use ProductCheeseID + ExtraSubscriptionTimespanID + ExtraServicelevelagreementID a customer can order a Cheese subscription with SLA, and your database structure does not repeat itself.
This basic design is open to alterative ideas and solutions. But keep your structure seperated. Dont make one huge table that includes all fields you may ever need, things will break horribly once you have to change something later on.
When designing database tables, you want to focus on what an entity represents and having two or more slightly different versions of what is essentially the same entity makes the schema harder to maintain. Orders are orders, they're just different order types for different products, for different customers. You'd need a number of link tables to make it all work, but you'd have to make those associations somehow and it beats having different entity types. How about this for a very rough starting point?
What would be the best way to tackle this, and is splitting each into seperate order tables the best way?
That depends. And it will change over time. So the crucial part here is that you create a model of the orders and you separate the storage of them from just writing code dealing with those models.
That done, you can develop and change the database structure over time to store and query all the information you need to store and you need to query.
This for the general advice.
More concrete you still have to map the model onto a data-structure. There are many ways on how to solve that, e.g. a single table of which not all columns are used all the time (flat table), subtype tables (for each type a new table is used) or main table with the common fields and subtype tables containing the additional columns or even attribute tables. All these approaches have pros and cons, an answer here on Stackoverflow is most likely the wrong place to discuss these in full. However, you can find an insightful entry-level discussion of your problem here:
Entity-Attribute-Value (Chapter 6); page 61 in SQL Antipatterns - Avoiding the Pitfalls of Database Programming by Bill Karwin.

Best way to Manage Junction Tables

I have a PHP application where I want to enter many-to-many relationships between products and categories. Everything I put into the database will be manual and my database structure is:
--TABLES--
Products - (Product_ID, Name, etc.)
Categories - (Category_ID, Name, etc.)
Product_Categories (Product_Category_ID, Product_ID, Category_ID)
*this is my junction table
*the Product/Category IDs in this table reference those in the other tables.
Currently when entering in relationships manually, I have to think: which product is product #7 again, and which category is category #8? now there is just a handful but this will be incredibly cumbersome when there are more.
My question is: If I'm manually entering relationships into the junction table, what is the best way to manage this process. Do I need to create my own php page that visualizes these relationships, is there an application better than phpmyadmin (or a feature in it) for this, can I somehow show other table info related to ID's in phpmyadmin?
I'm new to all of this, so sorry if I'm missing something obvious.
PHPMyAdmin wiki has a page describing how to link foreign key values from tables with fewer than 200 rows. Maybe this will help your category problem, but probably not your product id problem.
(Personally I think your instinct to write a data entry app in php or whatever was correct, but here is at least an attempt to find a feature within PMA.

Is it better to have separate tables for articles & comments, or one table for both?

I am working on a little project where a user submits an article to MySQL, and then PHP send the post to the screen. So far so good.
Now i want to extend it to a "two level" post system, where people can comment on the articles.
The case is, i dont know how to do that.
The table i use for storing articles:
TABLE: posts
id user date avatar signature post
Should i make a new row named comments in the posts table, or should i place the comments in a seperate table? one for articles, one for comments?
All help is much appreciated.
Depends on how you use it on your website. You have to ask: "are my articles and comments essentially the same concept?" If yes, then use one table, if no, use two. On most websites articles work differently, can be categorized, editted etc., and usually need a different fields which would clutter the comments table... so in that case two tables are more reasonable. But if you conclude that on your website articles and comments are generally the same (but try to think future proof: wouldn't you need to add some article functionality in 2 months?) then you can think of articles also as of comments and have one table for them.
If you decide to merge things to one table, and you realize that you need another column to distinguish type of the post, and that some columns remain unused for some types, it is a clear warning signal you should have two tables!
This is a little subjective, but I would just set up a parent/child relationship on your posts table, make a parent_id column that is a foreign key for the id column in the same table. for extra credit, you can make it a nested set relationship which will allow you to pull the parent and all the children in one query

Best way to store product colors in a database

I am working on an online jewelry store website. One of the requests is that from the back end a color/colors can be chosen for a single piece of jewelry so that users would be able to sort/view only items that are for instance green.
What is the best way to store this in a database? Should this be done with 3 tables:
product table (already exists)
color table : each field has a color
table that matches the product is with the color id
Is this the correct way to do it? I am using php and mysql but I think this is just a pretty standard database question.
Yes, your solution with a many-to-many relationship between tables sounds good for your case. You can then easily JOIN the tables to get information out of the IDs.
Yes, that's an appropriate way to do it.

Categories