Find User Creation Date in Mysql? - mysql

Is there any way to find the time when a user was created? It means to trace the user creation date in mysql.
Please help me out.
Thanks,
Nitesh Kumar

You can add timestamp along with user information when user creation form is submitted otherwise there is no way to track that.

You have to add new field in your database users table say it "created" with type DATETIME
Sample Query is here:
ALTER TABLE `users` ADD `created` DATETIME NOT NULL COMMENT 'user created date' AFTER `name`
Here AFTER name is the last field of table so that it will add new field created at last of users table.

You can add a new TIMESTAMP field to your table, but remember to set CURRENT_TIMESTAMP as default value, so when the record is created, this field gets the timestamp from when the record is created.
I'd say it's impossible to tell afterwards when a record has been created, maybe with some kind of using binary logs, but i don't know much about that.

Related

How can I record the username and timestamp of row insertions and updates in MySQL?

I'd like to record the last user and last time a particular row was either inserted or updated in a MySQL table. What is the best way to go about this? Is there some MySQL metadata I can investigate or do I need to create username and timestamp columns myself and then create triggers to populate them?
You need to create separate columns yourself for timestamp and user name. For timestamps there is no need to use triggers to update its value, just declare the timestamp field to use current timestamp as initial value and update value:
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
The user is bit more interesting. If you are talking about mysql level user, then yes, use triggers or stored procedure using the USER() function. If you are talking about application level users, then I would supply that username as part of the update statement.
UPDATE table SET username='xxx', ... WHERE ...
You can use a column and set it default system date for insert
For update you must use trigger or etcs.
At least in MySQL 5.5.46, you can track tables. This includes structure and data. There, the DB username and timestamp is recorded.

Is it possible to copy a timestamp between MSQL tables?

I am creating a table for users that have been banned from my website. I want to move everything over to the new table, including the TIMESTAMP of when the record was created, but the new table will also have a TIMESTAMP for when they got banned.
When I create a row in table foo, I have a TIMESTAMP that registers when that row was created. I would like to transfer that row to another table (bar) that has a different number of columns, and keep the information from the original TIMESTAMP. How can this be done?
I am fairly new to MySQL, so correct me if I have false assumptions, but it seems that the TIMESTAMP field actually creates a TIMESTAMP when the record is created and that the existing TIMESTAMP value would have to be stored as something else like a VARCHAR in the new table. Am I off base?
EDIT: Solved my own question
I originally had not tried anything because I did not know what to try. In the end, it turns out that simply moving the value from one TIMESTAMP column to another TIMESTAMP column does, in fact, work. The only difference between the two columns is that the original TIMESTAMP has a DEFAULT of CURRENT_TIMESTAMP, whereas the new TIMESTAMP has a DEFAULT of NULL.
I used the following code:
INSERT INTO `bar` (`created`)
SELECT `created` FROM `foo` WHERE `user` = '000'

How to get SQL to update my record Modified Timestamp correctly?

I am trying to set up an sql table which records when an account was created and when it was last modified. I would like sql to handle this so I don't have to do it in my php files.
I have two columns in my users table (both are of type timestamp):
created
modified
I want the "created" time to never change and always contain the date it was created, and the "modified" to be changed each time the users row is modified. I have the table set up so "created" works as I expect, but when I try to update modified:
ALTER TABLE `users`
CHANGE `modified` `modified` TIMESTAMP NOT NULL
DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
I get the following error:
1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Can someone assist me on what I need to do to accomplish this correctly?
It's stil not possible in mysql. You can have them set to the actual time only on INSERTs, only on UPDATEs or on both. However you couldn't have more than one of these auto-TIMESTAMP columns in one table. that's now possible using TRIGGERs if using Mysql 5.x
Refer this article It will help you lot :
Two auto-TIMESTAMP columns in one table with MySQL 5.0

I need to select a timestamp and insert a date

I have a timestamp in a database table. Now everytime a record is amended the timestamp changes - this isn't really what I want as the record represents a sale so everytime the data is amended it looks like the sale time has changed! Thus I have added a new field to the database table called 'sale_date' and I want to select the timestamp field of that record, called 'sale_time' and convert the timestamp to the format dd/mm/yyyy hh:mm:ss. and insert it into the new 'sale_date' field (which is text not date)
Any ideas? I'm rubbish at mysql.
If I haven't explained myself well please say.
The definition of your timestamp column (one that changes on updates) constains ON UPDATE CURRENT_TIMESTAMP clause. Remove it (ALTER TABLE) and it will stop updating.
And please, pleasee, please, do not ever store dates as text.

Automatically add user and date to records

I have a database which can be accessed by multiple users. Is there any way I can set the tables such that whenever somebody enters a new record or modifies a record, his/her username and the date entered is stored into 2 columns in the same table as the record itself. The table would look like this:
Id | Name | Username | Date Entered | Date Modified
1 | Cat | john | 1999-05-05 | 1996-06-06
I am using a GUI which is phpMyAdmin.
Thanks!
You can set a column to not allow null values, either when you create the table using NOT NULL after the data type declaration. Otherwise, you use an ALTER TABLE statements to change a column if they already exist, or you are adding the column to an existing table.
That will stop someone from adding a record, but not update. If you have a separate table of users to reference, you would use a foreign key relationship to make sure that if the user column is populated, it will be done with a valid user.
DEFAULT constraints can be used to set the value of the date fields if a value is not provided.
ALTER TABLE x
ADD USER VARCHAR(45) NOT NULL
ALTER TABLE x
ADD DATE_ENTERED DATE NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
ALTER TABLE x
ADD DATE_ENTERED DATE NOT NULL DEFAULT ON UPDATE CURRENT_TIMESTAMP
First of all, you may want to spend some time thinking about the best way to design an audit mechanism, because adding two columns to every table in your DB is probably not optimal.
Having said that, what you are looking for to do what you described is a trigger. This is a piece of SQL that will execute every time a specified operation is invoked. A rough example for your case:
create trigger audit
after insert on "table_name"
insert (user, time) into "table_name"
I don't remember the precise mysql syntax, but that's a rough example of what you need. Please ask a follow-up question if that isn't clear.