I have a table called "users" and in that table there is a row for each account. In each row there is a column called "created" and in this I need to add the timestamp for when the account was created? So like when the user registers, the timestamp is automatically added. Also, when the values are updated in the row, I want the timestamp to stay the SAME. How can I accomplish this?
Create the table, and apply the default value for your created column.
CREATE TABLE IF NOT EXISTS `Users` (
`User_ID` int(11) NOT NULL,
... all other columns ...
`Created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)
There is no need to update the Created (timestamp) when user update the info.
Just update the columns that need a change (e.g. email, address, etc)
using a trigger for the Created column is a bit overkill, I would say. It is better to use the default value instead. A Trigger in my opinion would be better for example a LastUpdated column. i.e. each time a user/account is modified it will update this column.
Related
so, I have a table named companies and I want to add 2 columns for insert and update times.
how do I do this? I dont want to add insert and update dates into my query.
this is my create statement
create table companies(
name varchar(20),
city char(10),
numberofemployees int(10),
averagesalary double
);
alter table companies add inserttime datetime, add updatedtime datetime, add id serial;
I need the insert query to look like this:
insert into companies values ("company","bglr",30,400.00)
and need output as
name,city,numberofemployees,averagesalary,inserttime,updatetime
company blr 30 400.00 23:00:11 23:00:11
First of all, you'll have to change your INSERT statement since the number of values doesn't match the number of columns. Luckily, you can specify the subset of columns that correspond to the given values:
INSERT INTO companies(name, city, numberofemployees, averagesalary)
VALUES ('company', 'blgr', 30, 400.00);
As you don't provide values for inserttime and updatetime, default values will be used for new records. Another way to insert records using default values is to put in the DEFAULT keyword instead of a concrete value (please refer to the documentation of INSERT for more details).
In your case the default values should be NULL because you didn't define anything else. To change the DEFAULT value for your columns, you can modify their definitions:
ALTER TABLE companies MODIFY COLUMN inserttime datetime DEFAULT CURRENT_TIMESTAMP;
Having this, inserttime is set to the current time for newly inserted records. Of course, you can also use the DEFAULT clause in CREATE TABLE statements, or while adding the columns.
Next, let's have a look at the updatetime. Usually, you want this to be updated automatically to the current time. This can be achieved in MySQL by specifying an ON UPDATE clause for the default value (details in Automatic Initialization and Updating for TIMESTAMP and DATETIME:
ALTER TABLE companies MODIFY COLUMN
updatetime datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
From now on, updatetime will be set to the current time automatically when a new record is inserted or an existing record is updated.
More information about default values in MySQL can be found in the documentation.
I want to add at row "jailtime" timestamp. That timestamp would be the timestamp of when "pjailed" row was updated.
I tried to do the fallowing:
https://dba.stackexchange.com/questions/45470/get-the-time-of-last-update-of-a-column
But instead of creating table, I wanted to alter existing table, so I went for
ALTER TABLE `users`
CHANGE `jailtime` `jailtime` TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP NULL
DEFAULT 'updated_at pJailed';
But it gives me error "Invalid value for pJailed"
If the value for pJailed for a row is updated that whole row receives an update. So it would suffice to use ON UPDATE CURRENT_TIMESTAMP (and DEFAULT CURRENT_TIMESTAMP) on jailtime.
You can alter the column jailtime to reflect this using:
ALTER TABLE your_table
MODIFY COLUMN jailtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Edit: OP mentioned in their comment that the attribute jailtime should only be updated when the attribute pJailed is updated. To accomplish this one should use a trigger as described here.
I have this table with newspost, i want to add timestamps on date added, and i want to update another col when the post is edited. I would like it to happen automaticly in MySql. without the use of any PHP code.
CREATE TABLE IF NOT EXISTS news (
id int(11) NOT NULL AUTO_INCREMENT,
data text,
date_published timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_edited timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
What's the best practice in a case like this?
Consider using triggers. From the MySQL docs:
A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.
Example of a trigger:
CREATE TRIGGER trigger_example AFTER UPDATE ON news
FOR EACH ROW UPDATE some_table SET another_column = NEW.data;
You can only use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP features on one timestamp column in a table.
Use the following table definition to turn off the features for date_published and use them for date_edited:
CREATE TABLE IF NOT EXISTS news (
id INT(11) NOT NULL AUTO_INCREMENT,
data TEXT,
date_published TIMESTAMP NOT NULL DEFAULT 0,
date_edited TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
When inserting a new row, pass a NULL value for date_published to automatically assign the current timestamp to that column.
MySQL Docs on Automatic Initialization and Updating for TIMESTAMP:
It need not be the first TIMESTAMP column in a table that is
automatically initialized or updated to the current timestamp.
However, to specify automatic initialization or updating for a
different TIMESTAMP column, you must suppress the automatic properties
for the first one. Then, for the other TIMESTAMP column, the rules for
the DEFAULT and ON UPDATE clauses are the same as for the first
TIMESTAMP column, except that if you omit both clauses, no automatic
initialization or updating occurs.
To suppress automatic properties for the first TIMESTAMP column, do
either of the following:
Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.
I am using MySQL 5.6.1 on a Win 7 64Bit.
I have a standard audit column I add to all my tables called CRT_TS (create timestamp), along with a UPD_TS (update timestamp) column. I had planned on populating these via a before insert trigger and a before update trigger using utc_timestamp().
The UPD_TS column behaves as I expect it to. However, the CRT_TS column seems to be getting automatically populated without my defining a default or trigger for that column.
I was able to reproduce this behavior by running the following script.
create schema `test` default character set utf8 collate utf8_general_ci;
drop table test.TEST_TABLE;
create table test.TEST_TABLE(
TEST_ID int not null auto_increment ,
CRT_TS timestamp not null ,
UPD_TS timestamp not null ,
TEST_ALIAS varchar(64) not null ,
primary key PK_PERM (TEST_ID) ,
unique index UI_PERM_01 (TEST_ALIAS) )
auto_increment = 1001;
insert into test.TEST_TABLE
(TEST_ID
,TEST_ALIAS)
values
(1
,'testing');
select *
from test.TEST_TABLE;
In the above example, the CRT_TS column isn't being supplied a value, and yet it is being populated with the same value what would have been provided by the now() function. The UPD_TS column is populated with all zeros, yet both columns have been defined identically.
My questions is, what is populating the CRT_TS column? I am attempting to set both the UPD_TS and CRT_TS columns to utc_timestamp() value. Even setting the value in a trigger for CRT_TS, the value is overridden.
Thanks for any clarity you can provide.
The code below doesn't seem to work even though the collumn and table does exist, any ideas?
ALTER TABLE `table` CHANGE 'collumn_1' 'collumn_1' TIMESTAMP DEFAULT 'CURRENT_TIMESTAMP' NOT NULL
I'm just trying to make the collumn available so it can store the current date and time when any data is added to this table.
ALTER TABLE `table` MODIFY collumn_1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;