0

I am working with MySQL I have made a table which have a TIMESTAMP column, which add current time and date to each row added. But I don't know how to initialize it when I insert data to that table. My SQL Queries are :

TABLE

"CREATE TABLE IF NOT EXISTS messages ( mess_id int PRIMARY KEY AUTO_INCREMENT, mess_from int, mess_to int, mess_txt VARCHAR(20000), mess_time TIMESTAMP, FOREIGN KEY (mess_from) REFERENCES users(id),FOREIGN KEY (mess_to) REFERENCES users(id) )"

INSERTION

"INSERT INTO user_messages (NULL,'"+data.from+"','"+data.to+"','"+data.text+"',DEFAULT)"

The problem is, I don't know how to initialize the timestamp column, I tried NULL and DEFAULT but both show me this error of invalid syntax. I want the timestamp field to be initialized automatically! That's it.

3
  • Is this a MySQL question? It appears to be unrelated to SQL Server, which is a different product. You also urgently need to escape your parameters. Commented Oct 26, 2017 at 17:28
  • yes it is mySql Commented Oct 26, 2017 at 17:28
  • If the column has a defined DEFAULT you just need to exclude the column out of the insert to get the default value. If you want the actual timestamp as default you can also just insert NOW() Commented Sep 7, 2020 at 6:42

2 Answers 2

2

On the table creation, set the default on the timestamp column to CURRENT_TIMESTAMP.

CREATE TABLE IF NOT EXISTS messages ( 
    mess_id int PRIMARY KEY AUTO_INCREMENT, 
    mess_from int, 
    mess_to int, 
    mess_txt VARCHAR(20000), 
    mess_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 
    FOREIGN KEY (mess_from) REFERENCES users(id),FOREIGN KEY (mess_to) REFERENCES users(id) )

EDIT: For completeness, this will set the mess_time field to the current timestamp when a new record is inserted and does not contain an explicit value for that field. It will have no effect when an UPDATE on that row occurs. You can set it so that on every update the timestamp will be set to the current timestamp using "ON UPDATE CURRENT_TIMESTAMP"

CREATE TABLE IF NOT EXISTS messages ( 
    mess_id int PRIMARY KEY AUTO_INCREMENT, 
    mess_from int, 
    mess_to int, 
    mess_txt VARCHAR(20000), 
    mess_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    FOREIGN KEY (mess_from) REFERENCES users(id),FOREIGN KEY (mess_to) REFERENCES users(id) )

When doing the INSERT, do not specify the timestamp column:

INSERT INTO user_messages (mess_from,mess_to, mess_text) VALUES (@FromValue,@ToValue,@TextValue)

(The above will work in all cases. There is a system variable that controls whether you can use NULL to insert the current_timestamp. This is not standard and less compatible with other SQL engines, so it is often set to standards mode.

The variable is explicit_defaults_for_timestamp, but it has been deprecated, and these non-standard behaviors are set to be removed in future mysql versions, so I would not recommend using it.)

Sign up to request clarification or add additional context in comments.

4 Comments

sorry, I didn't address the INSERT statement.
edited. The INSERT statement should exclude the timestamp field.
CURRENT_TIMESTAMP doesn't work. It's not recognized by MYSQL
@JasonGenX CURRENT_TIMESTAMP is supported. dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
1

Try the following:

INSERT INTO user_messages (NULL, '"+data.from+"', '"+data.to+"', '"+data.text+"', now());

Comments

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.