Adding new Column with current time in SQL - mysql

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.

Related

How to add AUTO_INCREMENT to an existing table [duplicate]

I would like to change the column type to AUTO_INCREMENT within an existing MyISAM1 database table. The databse currently has thousands of records where the column value is very important. It is crucial that the current value is not affected. I want to increment from the latest higheste value.
I tried this within phpmyadmin (sql generated by phpmyadmin) and got an error.
ALTER TABLE `myTable` CHANGE `myCol1` `myCol1` INT(11) NOT NULL AUTO_INCREMENT;
The error I got was:
ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '2197' for key 'PRIMARY'
I did some reaserch and found that by adding an offset it should resolve my issue. I tried this but was greeted with a syntax error.
ALTER TABLE `myTable` CHANGE `myCol1` INT(11) NOT NULL AUTO_INCREMENT = 2500
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 'INT(11) NOT NULL AUTO_INCREMENT = 2500' at line 1
I have also tried the above sql without declaring INT
Simply put I need to keep the existing records as they are but add auto increment ability to column myCol1 starting from number 2500 (as currently the last item is 2498). This column is also the Primary Key.
This is not a duplicate question as the solutions I have found for other answers do not resolve my issue.
You are using wrong syntax, try below command-
ALTER TABLE `myTable` CHANGE `myCol1` `myCol1` INT(11) NOT NULL AUTO_INCREMENT, auto_increment=2500;

Error with datetime insertion

I'm trying to insert a datetime in my table, but I can't figure why it isn't working. I'm running the following query:
DROP TABLE IF EXISTS dynamic_pricing ;
CREATE TABLE dynamic_pricing(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
time_start TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
time_stop TIMESTAMP NOT NULL,
change_ratio REAL NOT NULL
-- day_of_week INT(1)
);
INSERT INTO dynamic_pricing(id,time_start,time_stop,change_ratio)
VALUES(1,DATETIME('2000-01-01 00:00:00'),DATETIME('2000-01-01 00:00:02'),2);
I tried using this, only to get
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 '('2000-01-01 00:00:00'),DATETIME('2000-01-01
00:00:02'),2)' at line 2
What's wrong with this query?
The problem is that your value is defined as DATETIME() but you could use
INSERT INTO dynamic_pricing VALUES ('2017-06-03 21:30:03' )
instead.
Given that you are defaulting two values, you don't need to insert them. I would suggest:
INSERT INTO dynamic_pricing(time_stop, change_ratio)
VALUES('2000-01-01 00:00:02', 2);
Then, the DATETIME() is unnecessary (is it even a function?). Here is a SQL Fiddle example.
INSERT INTO dynamic_pricing(id,time_start,time_stop,change_ratio)
VALUES(1,'2000-01-01 00:00:00','2000-01-01 00:00:02',2);

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;

MySQL: How to solve syntax error on ALTER TABLE statement during change type of column

When running
ALTER TABLE my_table modify column my_column int(10) NOT NULL DEFAULT 0;
I've got the error message:
Error: 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 'int(10) NOT NULL DEFAULT 0' at line 1.
How can this issue being fixed?
ALTER TABLE ... MODIFY COLUMN ... does not allow renaming the column; this is why the name of the column must be provided only once (the current name).
In order to rename the column (besides other changes you may want to operate on it, like changing its type) you have to use ALTER TABLE ... CHANGE COLUMN ... and provide the current and the new name of the column.
See the documentation page of the ALTER TABLE statement for more explanation and examples.
Try this code
ALTER TABLE my_table CHANGE mycolumn my_column INT( 10 ) NOT NULL DEFAULT '1';

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