So the timestamp containing column in my database gets automatically updated to current timestamp when I update data there, even if I don't pass any timestamps (not passing null, but just ignoring this field, not including it in update array).
I have checked table structure and discovered that the timestamp column settings are set to "default - CURRENT_TIMESTAMP" and "on update - CURRENT_TIMESTAMP", but I've never written statements like these in my migrations and I don't need any automatic update to current timestamp (in my migration files, from which the database was created, there are just rows like $table->timestamp('date');).
And so here is the question: what should be written in migration files to avoid this situation and why does it even appear? Is this some kind of MySQL feature, or does MySQL interpret ignored field like passed null?
Well I found solution in MySQL docs, it is said that no specifications in migration is equal to specifying current_timestamp on default and update, so to avoid my issue I have to specify default value in migration, and do not specify update attribute.
Source: http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
Related
If i have ZKTime machine to register the attendance of the employees .
Sometimes this machine insert bulk of transactions in sql server db with wrong later date like
8-2103 instead of 11-2016
What are the possible causes of this problem and how to restore to the right date if i can't detect the problem ?
I've looked at the vendor link you supplied and it does not help in this case. I'm afraid we won't be able to answer this due to items outside of SQL Server. I believe you will need to contact Vendor Support for this.
The questions you will need to find out are:
How does the time machine calculate the CheckTime data?
How does the time machine store the CheckTime data?
How does the machine create the file to export to SQL Server?
This appears to be either an issue with how the system records the CheckTime data or in how it either exports / writes the data to SQL server.
As far as correcting the issue a basic update statement will fix it, but since there are different dates you will need to write a unique update for each case.
One possible solution is to make use of a Trigger to validate the date and update the date accordingly. Assuming the table has the Primary Key as id, if a newly inserted row has a date beyond today, it can be reset to the current datetime since employees' attendance record can't be in future.
CREATE TRIGGER CorrectTheDate on Config
FOR INSERT
AS
DECLARE #CT DateTime
DECLARE #id int
SELECT #CT = i.CheckTime FROM inserted i;
SELECT #id= i.id FROM inserted i;
if(#CT >= DATEADD(dd,1,getdate()))
UPDATE MyTable SET CheckTime=getdate() WHERE id=#id
GO
I need mysql to automatically update a field on every update. I created the field as:
`lastModified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
But the problem is as I update a record using R::store, the lastModified does not get updated automatically but is set to the last set value. Note that my code is not setting the field. Basically doing a findOne and store to set any other field should update the timestamp but looks like redbeans is reading the whole row (so lastUpdated is filled in as old time) and assuming when I store, it thinks I am setting it to old time value.
lastModified property is pure MYSQL. It does not depend on the implementation.
RedbeanPHP also updates mysql database, have you disabled this?
R::freeze( TRUE );
Once you put it on freeze, you can modify the database and tables yourself, and redbean will not update it. This should also take care of the lastModified property.
try this
$bean->lastModified = date('Y-m-d G:i:s');
R::store($bean);
Is there a way in MySQL to get insert date/time for rows if there was no insert_date field. I have a database which I configured to store insert_date but can I populate the field before that change ( month ago ). Is that even possible?
Pull that insert date/time from log or something else?
Nope. If that date wasn't stored before, it's impossible to find out when a row was inserted. The best you can do is just pick a date, or maybe make a rough estimate if you have information to base that on (for instance, the create date of a customer might be related to the date of their first invoice)..
By the way, you can add timestamp columns and specify the clause DEFAULT CURRENT_TIMESTAMP to set a timestamp as soon as you insert the row. That way, you don't need a trigger to update the row.
You can even add a clause ON UPDATE CURRENT_TIMESTAMP so the column (or a different column) is updated automatically too.
See Timestamp initialization for more information about this subject.
This doesn't change the fact, though, that you cannot get those values for rows that already exist.
I am using PHP MyAdmin Version 4.1.12.
I am trying to create a simple trigger that, after an update, sets 'dateModified' in table 'person' to CURRENT_TIMESTAMP. dateModified is of type TIMESTAMP. The way in which the update occurs to person is the setting of a single attribute in a single record through a X-Editable enabled grid view on a web page. After performing validation against the model with the updated attribute, a new database command is created with the relevant update SQL and executed. So each update only ever modifies a single row within 'person'.
Here is the SQL I wrote to create the trigger:
DELIMITER |
CREATE TRIGGER PERSON_AUPD AFTER UPDATE ON person
FOR EACH ROW BEGIN
SET #dateModified = CURRENT_TIMESTAMP ;
END;
|
DELIMITER ;
After performing updates, I see that the trigger hasn't fired, and the timestamp remains unchanged from the one they were created with (the default for dateModified, and dateCreated, are both CURRENT_TIMESTAMP, so they get set automatically on insert).
I have looked around for answers, and even looked into alternate methods to getting the update (the alternate method was calling a model's afterupdate method and performing separate SQL there on dateModified). I would prefer to exhaust every opportunity to use the triggers, before I go putting more code into my model.
Any help would be greatly appreciated.
Thanks to #juergend, the solution was the following:
Set trigger type to before update because after update cannot update attributes, and are best used to insert new records in related audit tables, etc.
Add NEW. to the front of the attribute you wish to modify.
I have a database where people register on a website and their form data then gets inserted into their database. Along with all that information, I want to insert the time of their registration. For example I have an auto-incrementing ID column which I do nothing in my php script for, the sql database automatically increments that with every new entry. In the same way, can I have a time column that I don't have to do anything in the script for, rather the database will just get the current time and put that along with the other inserted information? I'm using phpMyAdmin and I tried adding a column named Time with the type as DATETIME and the default value as the CURRENT_TIMESTAMP but it woulnd't let me add that and said invalid default value for Time.
Change the column type to TIMESTAMP.
Use a timestamp field, which does exactly what you want.
use timestamp column type e.g.
`inserted` timestamp NOT NULL default CURRENT_TIMESTAMP