How to store Log in database? - sql

My application let users to send files to each other. Regular users can edit their contacts, change password, etc. In addition, admin users can add/remove users and view the log of what happened. My question is how to store this log in MySQL database ?
I thought to store the log like this:
log_id time user_id action_type description
------ ---- ------- ---------------- ----------------------------------------
1 .... 4 User added Added new user: alex
2 .... 1 Contact added Added contact Paul to group Family
3 .... 1 User removed Removed user: gabrielle
4 .... 3 Files sent Sent files 3,5,7,14 to contacts 2,4,8
5 .... 8 Group added Added new group: Family
6 .... 8 Password changed
7 .... 8 First Name changed Changed First Name from Michael to Misha
What type would be the best for action_type ? Since new action_types may be added in future, I thought that ENUM won't be a good choice. So I thought to make it VARCHAR(..), like description.
Is this seems reasonable ?
I will be happy to hear any comments / suggestions.

If you're concerned about adding additional action types, make a separate table to store your action types and and join it to your logs table with a foreign key:
logs table:
log_id time user_id action_type_id description
------ ---- ------- ---------------- -----------------------------------
1 .... 4 1 Added new user: alex
2 .... 1 2 Added contact Paul to group Family
...
action_types table:
id name
--- ---------------
1 User added
2 Contact added
.....

Related

SQL - Tracking student exam records as they move between schools

I'd like to pick some of your glorious minds for an optimal solution to my dilemma.
Scenario:
Schools have children and children take tests.
The tests point to the child, not the school.
If the child moves school, the test records are taken to the new school and the previous school has no record of the test being done as they are linked to the child.
Obviously, this isn't ideal and is the result of the database not being designed with this in mind. What would the correct course of action be; I’ve currently identified the 3 possibilities listed below which would solve the current problem. However, i cannot be sure which is best for the issue at hand - and if any better solutions exist.
Have each test store the school & student within the test records (requiring current records to be updated & increasing the size of the database)
Create a new child record, duplicating the existing data for the new school with a new ID so the test remains linked to the previous school (complicating the ability to identify previous test scores)
Separately keep track of moves to other schools, then use this additional table to identify current and previous using the timestamps (increased complexity and computational requirements)
EDIT:
So i tried to use a basic example, but requests for the task at hand have been requested.
Here's the DB Schema for the tables (simplified for problem, note: Postnatal is not important):
Patients: ID, MidwifeID, TeamID
Midwives: ID
Groups: ID
GroupsMidwives: MidwifeID, GroupsID
PatientObservations: ID, MidwifeID, PatientID
Using a query as follows:
SELECT Some Information
from Postnatals
JOIN Midwives on Postnatals.MidwifeID = Midwives.ID
JOIN Patients on Patients.PatientID = Postnatals.PatientID
JOIN GroupsMidwives on GroupsMidwives.MidwifeID = Midwives.ID
JOIN Groups on Groups.ID = GroupsMidwives.GroupID
JOIN PatientObservations on PatientObservations.PatientID =
Postnatals.PatientID
WHERE groups.Name = ?
*some extra checks*
GROUP BY Midwives.Firstname, Midwives.Surname, Midwives.ID
However, in the event that a midwife is moved to a different team, the data associated with the previous team is now owned by the newly assigned team. As described in the example detailed previously.
Thus a modification (which modification is yet to be realised) is required to make the data submitted - prior to a team change - assigned to the previous team, as of current, because of the way the records are owned by the midwife, this is not possible.
You should below suggestion as per you concern.
Step 1 ) You need to create School Master Table
ID | School | IsActive
1 | ABC | 1
2 | XYZ | 1
Step 2 ) You need to create Children Master having school id as foreign key
ID | School | Children Name| IsActive
1 | 2 | Mak | 1
2 | 2 | Jak | 1
Step 3 ) You need to create test table having children id as foreign key
ID | Children_id | Test Name | IsActive
1 | 2 | Math | 1
2 | 2 | Eng | 1
Now whenever child moves school then make child record inactive and create another active record with new school. This will help you to bifurcate the old test and new test.
do let me know in case morehelp required

How to recover deleted/updated records in Informix?

I want to know if there's any way to recover the deleted or updated records updated accidentally in an Informix DB?
The table structure is like this:
ID NAME CODE
1 john yy7
2 Rany uu6
3 Mike hh7
The data has been updated to:
ID NAME CODE
1 XX yy7
2 XX uu6
3 XX hh7

Selecting rows using multiple LIKE conditions from a table field

I created a table out of a CSV file which is produced by an external software.
Amongst the other fields, this table contains one field called "CustomID".
Each row on this table must be linked to a customer using the content of that field.
Every customer may have one or more set of customIDs at their own discretion, as long as each sequence starts with the same prefix.
So for example:
Customer 1 may use "cust1_n" and "cstm01_n" (where n is a number)
Customer 2 may use "customer2_n"
ImportedRows
PKID CustomID Description
---- --------------- --------------------------
1 cust1_001 Something
2 cust1_002 ...
3 cstm01_000001 ...
4 customer2_00001 ...
5 cstm01_000232 ...
..
Now I have created 2 support tables as follows:
Customers
PKID Name
---- --------------------
1 Customer 1
2 Customer 2
and
CustomIDs
PKID FKCustomerID SearchPattern
---- ------------ -------------
1 1 cust1_*
2 1 cstm01_*
3 2 customer2_*
What I need to achieve is the retrieval of all rows for a given customer using all the LIKE conditions found on the CustomIDs tables for that customer.
I have failed miserably so far.
Any clues, please?
Thanks in advance.
Silver.
To use LIKE you must replace the * with % in the pattern. Different dbms use different functions for string manipulation. Let's assume there is a REPLACE function available:
SELECT ir.*
FROM ImportedRows ir
JOIN CustomIDs c ON ir.CustomID LIKE REPLACE(c.SearchPattern, '*', '%')
WHERE c.FKCustomerID = 1;

Grouping attributes in SSAS

I have an account dimension where the same account can have multiple account numbers. So I setup a durable key to group these accounts. So for example
Durablekey AccountNum
1 1
1 2
1 3
2 4
3 5
The durablekey has no business meaning. I would like to have an attribute in cube that displays the accountnum but would essentially have the durablekey as the key. So if I selected accountnum 2 it would pull all the information for everything with a durablekey of 1. If I simply set the accountnum as the name and durablekey as the key I get a duplicate error during processing as expected.
Is this possible to do?
Hi there is sample data http://msftdbprodsamples.codeplex.com/releases/view/55330
You need to make Parent->Child Hierarchie, that Account key was normal integer key and other Accounts will have Parent Account key, which show to Parent Account. Parrent Account can have multiple other Accounts:
AccountKey | ParentAccount | Code
1 - 102
2 1 10255
3 1 10266
4 3 13444
5 3 13555
There is a lot of examples in internet just type - SSAS Parent - Child :
http://my.safaribooksonline.com/book/databases/microsoft-sql-server/9780735626201/creating-dimensions/creating_a_parent-child_dimension
Also you can use sample database and SSAS projects, there is everything done on working Cube...

Rails console - output queries results table

On rails console (just for the record, rails 3.2), how can I make it output to STDOUT my queries results table (ie: the table containing the columns names and values)?
User Load (6.5ms) SELECT `users`.* FROM `users`
---------------------------
Id | Name | Address | Phone
---------------------------
1 | Sam | ZZZ 10 | 55555
---------------------------
2 | xxxx | xxxxxxx | xxxxx
Tks!
ps: Note that query.explain is NOT what i need.
I wrote a gem to do exactly this! http://tableprintgem.com
The most powerful feature of table_print is the ability to see your data in the context of other objects it relates to. You can reference nested objects with the method chain required to reach them. This example is showing data from three different tables:
name from the Author table (reached through author.name)
title from the Book table (reached through author.books.title)
caption from the Photo table (reached through author.books.photos.caption)
There's a short intro screencast at http://tableprintgem.com