Database desing Mysql - php

currently I found myself wondering if this is the right thing to do in this case.
You see I have a database called for example Warehouse
I this database I have two tables:
[the table items is for saving the items of the system add-modify-delete records]
Items (which its columns are )
TAG_ID
P_NUMBER
QUANTITY
TYPE
LOCATION
DESCRIPTION
REASON
USERID
DATE
[the table lastchanges is for saving the items of the system that has been changed]
lastchanges (which its columns are )
ID
TAGID
PNUMBER
USERID
ACTION
DATING
Now I have been asked to add "exactly what has been changed" to the current form, for example if the quantity changed I have to show that before and after in a bootstrap form.
My brain told me to just add all the columns of the table items into lastchanges and save on those columns the data before changing and into items the new modified data, but performance-wise I see this as a bad action and I want your opinion.

If I understand you correctly you need a history of your DB changes.
If thats the point I would recommend you to create a new row for each entry and soft delete the old one. Then nothing gets lost and you can always get differences or older values.
Adding a the field deleted_at, and created_at as dates would do that trick for you. If deleted_at is null its the current entry, if there is a date set you know exactly when it got "overwritten"

Related

How to save the modifications of a column in sql

I'm coding a website where users can add concerts/events and other users can modify the informations provided for every concert/event added on the website.
I want to save every modification that has been made and the user that made the modification. For instance there is this functionnality on Wikipedia where every article has saves of every modification made and the user who made it.
For now, I just save the name of the user who made the last modification(s), and the modifications aren't saved: when a modification is made it will just overwrite the previous value of the corresponding column.
You can see under the current state of the tables I'm talking about. One for concerts with different informations: artist, date of the event, hour, links related to the event etc...
and another one for users.
Current sql tables
I think a first step would to create separate tables for every element of a concert that can be modified, with 2 foreign keys (one linked to user, one linked to concert) plus the value of the element. But still, if a modification is made I still don't know how to save the previous value and previous user.
I've thought of puting values inside an array but I don't know if this is possible in SQL and if this is the best solution.
Thanks for reading/helping
You should add a column that mark the create/update time to the concert table.
Then create a new history table (same schema of concert) for storing concert changes.
When a user edit a concert:
Save the current state of concert content to the history table.
Update the new change to the concert table with the user_id of the user who perform this action
That's it! When you want to list the changes history, just query the history table!
If you want to log the delete operations as well, add another column and mark the end period of each version (a little bit more complex)

How can I show results in a select input, based on values shown in a seperate table?

I have three tables, and I'm just looking for a way to make this work.
tbl_campaigns has the columns "id" and "campaign". This one is fairly straight forward, it's just campaign names with an ID number that is auto-incremented so they have unique IDs.
tbl_users has an "id" column so each user has a unique ID number, standard stuff.
tbl_permissions creates a new row whenever a new user is created. This means its "id" column has unique ID values that match to the ID of a user in 'tbl_users'. The columns have been named to match the ID value of a campaign each time a new one is created, for example, the column "campaign_1" is relevant to the campaign in 'tbl_campaigns' with the ID of 1. The idea is this table data is filled with either 1's or 0's.
If a row with the ID of 1 has the number 1 for the column "campaign_1", then the user with the ID of 1 is approved for the campaign with the ID of 1 in the campaign table. If it were 0 then they're not approved for it. The same logic applies for columns "campaign_2", "campaign_3" etc..
Anyways, the issue I'm having is displaying this information on a front-end, as I only want the user to be able to see the campaigns they are approved to run in a drop-down list. When the user is logged in it stores their User ID in a session, I'm not sure if there's a way around it with this method.
Is there any way to get around this? Please note I've done this in procedural PHP as I'm still in my early days, so if anyone has a solution along these lines it would be much appreciated. Sorry if it's a little confusing. I am aware it's a bit ham-fisted, but I just want it to work first.
I believe that your schema needs to be improved, as the table structure should not have to change every time that you add a new campaign.
keep tables tbl_campaigns and tbl_users as they are
create table tbl_permissions with 4 fields (id, user_id, campaign_id and permission)
To check if a user has permission use a query like this:
SELECT permission FROM tbl_permissions WHERE user_id = ? AND campaign_id = ?
So, every time you create a campaign add a corresponding record to the tbl_permissions table. No need to add a new column.
I think the best practice to do this is as follows:
- Create HTML to show to the user(if you don't have it, let me know so i can work on one you can use)
- Create JS archive that will be in charge of calling PHP file and show the result in your HTML(if you don't know how to make it let me know so i can help you)
- Create PHP file, this is going to be in charge of consulting your data base and give the result disired for your select (if you don't know how to make it, let me know)
It is pretty easy to make this work, let me know if you need more help.

News feed using redis

I have the following table structure where I am storing activity of a user like 'product creation'
id
user_id
type_id
type
verb
data
created_at
updated_at
Whenever an event happens on a product like ( an order has come for the product), I need to update the updated time, so that the record appears in the first as the user will be seeing the 'sorted using updated_at' row data. The feed which I am referring here will be consumed by a single user and there is no concept of following here. So whoever created a feed will be seeing his own feeds. If there is an update (order placed by someone on that product) coming on the feed, it should go up.
So when an order is placed, I will update the updated time of the entry so that it appears in the first.
I am planning to use redis for the reads, but I am pretty confused on the update part. How will I handle this case.
What I have tried ?
Created the table structure as following
id
user_id - User who created the event
event - created_product
event_id - product_id of products table
data - json object of the product details
created_at
updated_at
When an order is received
The updated_at timestamp is updated. So that the record comes on the top of the user feed.
But this doesn't seem to be a proper solution as frequent updates can come for a row which can lead to row locking and more waits. How do I solve this?
Well, you could create sorted set in redis:
FEED:USER_ID containing values:
PRODUCT_ID - last update time as score
And then get products from database by ids which you got from redis.
But if your main concern is row locking, then maybe you should just queue updates? Create redis sorted set "PRODUCT_UPDATED" and add row whenever you are updating product:
USER_ID#PRODUCT_ID - update time as score
Then create some background CRON job which will update products found in that sorted set. You can do it every second, or every 30 seconds, you can throttle number of updates etc...

Need to auto increment but multiple entries have to have the same int assigned using MySQL

I was about to create my tables when I noticed, sh!t. I have no field to group similar entries (that is the main purpose of this table lol). The idea is, there is a family that has signed up and I want to group them. So when I pull out data, I can assign prices to the family as a whole.
gp_ID customer leader_Of_Group
1 Turk yes
1 JD no
1 Sarah no
1 Felina no
2 John no
2 Manny no
2 Jaden yes
*note - simplified table for readability
My problem is, I don't know how i'm going to achieve the gp_ID. I am confident primary keys don't allow duplicate values so, i'm stumped at the moment.
The gp_ID will be entered automatically, I just don't know how to increment after each family has signed up. Furthermore, there is functionality where my client can select which passengers to group.
I'm not sure how to go about setting up this table or how to query it in a way that each family will increment appropriately. My only thought so far is finding out the gp_ID based on the latest entry and increment, and even then I don't know how to go about doing that or if i'm on the right track. Also, would an auto increment field be necessary too? Any help/guidance would be greatly appreciated.
You are facing an issue because you really have two entities. One of the those entities is the group and the other is the group members.
Your process for adding members to a group should be:
Add a row to the Groups table. This would have an auto-incrementing id.
Add rows to the GroupMembers table, identifying the group using the previous id.
Voila! The groups will be well identified. The Groups table itself could have columns such as:
GroupId (auto-incremented id)
Name for the group
Creation date
Leader_MemberId
The latter would be a way of ensuring that each group has exactly one leader, without having to write a trigger to enforce this constraint.

Stock movement in PHP

I have a software in PHP and postgres that I use for invoicing. I am running a stock management system that I created. i am trying to create a stock movement page where I can see when a part came in, where it was issued and when and also when it was credited (if it was). I am running 5 tables for the stock. My main one is part2vendor, parts, expenses, wo_parts and int_part_issue. When I receive stock, it goes into the table part2vendor (onhand gets increased by number received). The expense table gets the details of the part number, the supplier invoice and the date received. wo_parts stores the part number issued to a workorder. int_part_issue is when I do a stock adjustment or use a part internally. I am looking to create a PHP table that would list (in date order) the 'paper trail' of a part. I can let you know table names and columns if required. Thanks.
Sounds like you need a simple history table?
Columns
part_history
id
part_id
date_modified (timestamp)
action ( or maybe action_id if you have an actions table)
vendor_id
And when you get a new part, and add it to the parts2vendor table ( i think u said) you would use the inserted part ID (or unique part id ) to add a record rto history
INSERT
(id, part_id, action, vendor_id)
46565, 5757575, "Purchased", 757575
The date will be inserted as a timestamp by postgres
Than for any part yuou can grab history relying on the uniquer id
sort by date_modified DESC.

Categories