mySQL alter table on update, current timestamp - mysql

alter table `quote`
modify column `timestamp`
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
NOT NULL
What's wrong with the above mysql query?
I am trying to change my timestamp column to default and update with the current timestamp.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL' at line 1

After MODIFY COLUMN col_name the syntax requires a column_definition which in turn requires a type. Add the current type of this column (e.g. DATETIME) before DEFAULT to resolve the syntax error.

This is not so much an answer as it is a tip. In MySQL workbench 6.3 community build, if you want to modify the column using the table edit screen, make certain the "Data Type:" for your timestamp column is for sure set to TIMESTAMP and then make ON UPDATE CURRENT_TIMESTAMP the default value (on my screen, it is just below the "Data Type:").

Related

Add timestamp column in mysql

SQL Statement:
ALTER TABLE `Trade`.`details`
ADD COLUMN `datetime` TIMESTAMP(3) NOT NULL DEFAULT TIMESTAMP(3) AFTER `Result`
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(3) AFTER Result' at line 2
i am new to mysql any help.Please
timestamp doesn't need length, also you are defining a default value but provided a datetype instead !!
here is what I mean
ALTER TABLE `Trade`.`details`
ADD COLUMN `datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `Result`

MySQL date column in phpmyadmin

I'm building registration form for my teacher's website.
I have created database called "students" with various rows like "name, surname etc."
Now I would like to add a column named "date_submitted" which will automatically insert current date and time to the row when data is submitted.
For example a student submits his data, the data is inserted into the table and mysql automatically fills the date and time when this insertion occured.
I tried #MLBDG answer from this question MySQL date column auto fill with current date but it doesn't work.
This is the way I do it:
Query:
ALTER TABLE `students` ADD `date_submitted` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER ;
The error I'm receiving:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Server version: 5.5.34 - MySQL Community Server (GPL)
Remove AFTER keyword at the end of your query:
ALTER TABLE `students` ADD `date_submitted` TIMESTAMP on update CURRENT_TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP;
Syntax: ALTER TABLE
If using the AFTER keyword in your ALTER TABLE query, it must be followed by the name of a field in the table you are modifying.
Remove After Keyword at the end of your query
ALTER TABLEstudentsADDdate_submittedTIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

Altering Mysql column default value to CURRENT_DATE gives error?

ALTER TABLE `mysystem`.`projects`
MODIFY COLUMN `project_capture_date` DATE NOT NULL DEFAULT CURRENT_DATE();
gives:
Error Code: 1064. You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server
version for the right syntax to use near 'CURRENT_DATE()'
at line 2
Current row definition:
project_capture_date, date, NO, , 0000-00-00
Just recently changed the engine to InnoDB from MyISAM.
As per the mysql document
http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression. This means,
for example, that you cannot set the default for a date column to be
the value of a function such as NOW() or CURRENT_DATE. The exception
is that you can specify CURRENT_TIMESTAMP as the default for a
TIMESTAMP
To solve this you may need to define the datatype as TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP
DEMO
If you do not want TIMESTAMP then you have do while inserting by setting the column value as Now()

How to alter an existing column to default to the current time on insert, in MySQL?

I'm trying to change the existing column "time_sent" to default to the time during insertion. My SQL is:
ALTER TABLE `email_history` alter `time_sent` set DEFAULT CURRENT_TIMESTAMP
I'm getting this error though:
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_TIMESTAMP' at line 1
I've read the documentation and other (similar, but not identical) examples, and no luck.
My version of MySQL is 5.0.67 I believe.
It should be ..
ALTER TABLE 'table' MODIFY collumn_1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

Define default date value in MySQL, similar to timestamp

I'm using MySQL (nobody's perfect), version 4.1 and I'm used to define some timestamp columns like that:
ALTER TABLE foo ADD creation TIMESTAMP DEFAULT NOW() ;
I'd like to do exactly the same thing, but for a DATE field. The reason being I don't need a TIMESTAMP precision and since no functional index exists in MySQL, I cannot access quickly to the rows with a given date (regardless of time of day). So I tried the following but it just does not work:
ALTER TABLE foo ADD creation_date DATE DEFAULT CURDATE() ;
ERROR 1067 (42000): Invalid default value for 'creation_date'
Or even
ALTER TABLE foo ADD creation_date DATE DEFAULT DATE(NOW()) ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(now())' at line 1
Which does not work either.
Any Ideas?
In MySQL default values have to be constant.
Functions or expressions are not allowed.
The exception ist the TIMESTAMP type, for which CURRENT_TIMESTAMP is a valid non constant default value.
See 4.1 manual: Data Type Default Values