1

I want to create a table with two column, one "created" and one "modified", with a timestamp type. I don't want them to have a default value. But when I create them, it automatically puts CURRENT_TIMESTAMP as default for at least one of them, and also on update.

How can I just have type timestamp with no default or on update value for any of my column?

Edit: I use MySQL

1
  • 1
    What's wrong with CREATE TABLE (TIMESTAMP val1, TIMESTAMP val2 ...);? Commented Jul 15, 2012 at 1:27

1 Answer 1

1

If you are using SQL Server, the timestamp type is used for row versioning, not for storing an actual date or time value. See MSDN.

You could create the columns with a datetime datatype and then set your created and modified values with triggers.

CREATE TRIGGER trg_SetDateCreated ON MyTable
FOR INSERT AS 
    UPDATE MyTable 
    SET created = CURRENT_TIMESTAMP
    WHERE MyTable.id = (SELECT Id FROM Inserted);
GO

CREATE TRIGGER trg_SetDateModified ON MyTable
FOR UPDATE AS 
    UPDATE MyTable
    SET modified = CURRENT_TIMESTAMP
    WHERE MyTable.id = (SELECT Id FROM Inserted);
GO
Sign up to request clarification or add additional context in comments.

4 Comments

I use My SQL. I didn't know that I should use datetime for SQL, nor the trigger solution. Thanks!
I tried thatCREATE TRIGGER trg_SetDateCreated ON users FOR INSERT AS UPDATE users SET us_created = CURRENT_TIMESTAMP WHERE users.us_id = ( SELECT us_id FROM Inserted ) ; and I get a syntax error...
Ok, my solution was for SQL Server, not MySQL. Perhaps someone else can provide the syntax for creating the equivalent triggers in MySQL. It looks like this site would be a good place to get you started: dev.mysql.com/doc/refman/5.0/en/create-trigger.html
The only problem is.. if you have the exact same data going in as the original record, you'll still have the time stamp changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.