Find differences from data - mysql

I am parsing a html code at difference times. Now I want to find differences, so if any value has changed, added or deleten.
I want to save everything in a database.
The table looks like this:
id | column1 | column2 | column3
Now I want to update every row where the data has changed, added or deleten.
What is the best way to compare the old value and the new parsed values?
Should I create a hash, and if the hash is different I delete this entry and add a new one?

Related

Adding JSON data in audit table

I am creating audit tables for my database using sqlalchemy-postgresql-audit but the issue is that it creates a separate audit_table for every table and I wan't to create a common audit_table for all which contains
------------------------------------------------------------
| transaction(insertion/updation/deletion) | data. |
------------------------------------------------------------
I have edited the source code to create a common table for all by extend_existing=True and I wan't to add the the data of transacted row as JSON in data.
How can I achieve that?
I got the answer for this
When trigger is applies a RECORD for the OLD ROW in case of UPDATION and DELETION and NEW ROW in case of UPDATION and INSERTION is created i.e we can access values using
OLD.(column_name) or NEW.(column_name)
And to enter the entire ROW as JSON in data field
to_json(NEW) or to_json(OLD)
will work

How to store a list of unknown length in an SQL database

I have a table called 'Shows' that looks something like this:
| ID | TITLE | IMG | DESC | STATUS |
I also want to store a list of URLs that reference each Show's episode list. Unfortunately, each show can have a vastly different number of episodes, and there's no way to easily predict what the number will be for any show.
How do I store this type of data?
I considered making a table that was named after the ID of each show, with one column that contains the URLs, but I'm sure that the magic of relational databases has some other way of doing this.
Your last comment is on the right track. Yes, you need a separate table to capture the URLs, structured perhaps like:
"URL" table:
| UrlID | EpisodeID | URL |
Where "EpisodeID" is the unique identifier that you called "ID" in your table above. In other words, you might want to rename that "ID" column to "EpisodeID" to make things less confusing down the road.
This way this second "URL" table allows you to store infinite numbers of URLs associated with any single EpisodeID.
Regarding your show vs. episodes question, the same concept applies. You probably want a "Show" table separate from your "Episode" table, and then you'd add a new column to your Episode table that includes the ShowID. That way each row in "Episode" is a child whose "parent" is identified by ShowID.
Or maybe (I can't tell from your question) you expect there will never be more than one URL per episode, so you could get away with just two tables then, Show and Episode, and URL would just be a column in your Episode table.

SQL autoincrement in 'string'

I know about autoincrement, and that I might be better off doing a table of its own for this, but for some specific performance reasons I would prefer not to.
I have a row looking like this:
id,log
1 | 1:345;2:2345;3:234
Is there ANY way where I with a single SQL update can add X to the log column, and automatically a '4' as the log entry's id?
From your example I can't be entirely sure, but it looks like you don't even need the index numbers, i.e.
id | log
1 | 345;234;234
So you can simply add the entry, optionally prefixed with a ;. Later, any code can interpret the log contents by splitting on ;.
how about build a new table like this:
my_log
-------
id
sub_id
val
and populate it with all the proper parsed values -
then query from here to rebuild the string if needed, otherwise throw out the original table completely - terrible design.

Storing array with values in database

I have the following data which I want to save in my DB (this is used for sending text messages via a 3rd party API)
text_id, text_message, text_time, (array)text_contacts
text_contacts contains a normal array with all the contact_id's
How should I properly store the data in a MySQL database?
I was thinking myself either on 2 ways:
Make the array with contact_id's in a json_encoded (no need for serializing since it's not multi-dimensional) string, and store it in a text field in the DB
Make a second table with the text_id and all contact_id's on a new row..
note: The data stored in the text_contacts array does not need to be changed at any time.
note2: The data is used as individual contact_id to get the phone number from the contact, and check whether the text message has actually been sent.. (with a combination of text_id, and phonenumber)
What is more efficiƫnt, and why?
This is completely dependent upon your expected usage characteristics. If you will have a near-term need to query based upon the contact_ids, then store them independently as in your second solution. If you're storing them for archival purposes, and don't expect them to be used dynamically, you're as well off saving the time and storing them in a JSON string. It's all about the usage.
IMO, go with the second table, mapping text-ids to contact-ids. Will be easier to manipulate than storing all the contacts in one field
This topic will bring in quite a few opinions, but my belief: second table, by all means.
If you ever have a case where you actually need to search by that data, it will not require you to parse it before using it.
It is a heck of a lot easier to debug (for the same reason)
json_encode and json_decode (or equivalent) take far more time than a join does.
Lazy loading is easier, even if not necessary in most cases.
Others will find it more readable and, with a good schema definition, easier to conceptualize and maintain.
Almost all implementations would use one table for storing each text_contacts, and then a second table would use a foreign key to reference the text_contacts table. So, if say you had a table text_contacts that looked like this:
contact_id | name
1 | someone
2 | someone_else
And a text message table that looked like this:
text_id | text_message | text_time | text_contact
1 | "Hey" | 12:48 | 1
2 | "Hey" | 12:48 | 2
Each contact that has been sent a message would have a new entry in the text message table, with the last column referencing the contact_id field of the text_contacts table. This way makes it much easier to retrieve messages by contact, because you can say "select * from text_messages where text_contact = 1" instead of searching through each of the arrays on the single table to find the messages sent by a specific user.

how to convert multiple mysql column string to date

please i have a modified_by column in mysql table with string like this "40,1280825613|40,1280825617". the "40" is the id of the user that made changes to the record and "1280825613" is the date string. The | separates different periods of change.
How do i change the entire column to a readable date without affecting the user id. either mysql or php solution is welcome. Thank you so much.
I'd recommend a PHP script. You'll need to make two columns modified_by to retain the user id and modified for the timestamp. If there are multiple modified_by for each record you'll probably want to make a separate table, i.e. revisions. This would be the best way to store the data relationship. I'd also recommend not storing formatted data. You should already see why that's not a good idea. The database is the raw data, use PHP to format it.
Once you have that setup, just:
Select the data from the old table.
Loop over the records
explode() the column on |
Loop over the array
explode() the element on ,
Insert into new columns/table
Forgive me, but I'd rather teach you how to fish.