MySQL date column in phpmyadmin - mysql

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;

Related

Adding new Column with current time in SQL

Im trying to add a new column to my SQL table i want the data type to be TIME and the default value to be CURRENT_TIME. This is my query.
ALTER TABLE tuesday_records ADD cur_time TIME DEFAULT CURRENT_TIME
And this is the error message i get.
Error
SQL query:
ALTER TABLE tuesday_records ADD cur_time TIME DEFAULT CURRENT_TIME
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CURRENT_TIME' at line 1
You can do what you want with generated columns:
create table t (
id int auto_increment primary key,
x int,
t timestamp default now(),
tt time generated always as (time(t))
);
That is, add a timestamp column and then extract the time.
Here is a db<>fiddle.
Although this answers your question, I'm not sure if it is the best approach to your overall problem.

How to properly alter table default value

I can't figure out why my alter table query throws an error. Currently, this is a (DATETIME) column with default value NULL.
My wish is to alter it so datetime value gets automatically populated when I update a row. I'm trying to write an alter statement, but I can't figure out why mine is throwing the error.
My alter statement
ALTER TABLE `mydb`.`orders` CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}';
And this is the error that I'm getting
16:28:34 ALTER TABLE `mydb`.`orders` CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}' 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 'NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}'' at line 1 0.00041 sec
I'm using MySQL version 5.7
The CHANGE COLUMN alteration is used when you might want to change the name of the column, and requires you to provide the new name after the old name. If you're not renaming the column, you have to provide the name twice. Your command tries to rename the date_u column to DATETIME, and it's missing the datatype before the NULL keyword.
Use MODIFY COLUMN instead. It's the same, but doesn't allow renaming, so doesn't require you to give the column name twice.
ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
I'm also not sure what you intended with '{}' at the end, but I don't think it's valid syntax, either, so I've removed it.
You're missing to name the new column.
change this:
ALTER TABLE `mydb`.`orders`
CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}';
into this:
ALTER TABLE `mydb`.`orders`
CHANGE COLUMN `date_u` `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Notice the column name is typed twic, because you want the column name to stay the same, formerly here's the change column syntax:
ALTER TABLE `table_name`
CHANGE COLUMN `column_name` `column_new_name` (...);
Or, you can just you modify column syntax:
ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Ps: I don't get what you mean by '{}' so I removed it because I think it's not a valid syntax.
Hope I pushed you further.

mySQL alter table on update, current timestamp

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:").

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