MySQL vs MariaDB - ddl - setting default for time field - mysql

I've got table products, and want to add column with type time.
I've got statement as follows:
ALTER TABLE products ADD openTime1 TIME DEFAULT TIME(now());
it works on MariaDB, but it doesnt work on Mysql.
On mysql it produces Error -
[42000][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 '(now())' at line 1
Can someone tell me why? whats wrong here? I thought that this should be the same.

In MySQL the expression used in DEFAULT field attribute must be wrapped into the parenthesis:
ALTER TABLE products ADD openTime1 TIME DEFAULT (TIME(now()));
db<>fiddle here

Related

Adding a column to SQL with a formula

Apologies I'm brand new to SQL. I'm trying to add a column to SQL that calculates the number of days difference between as shipping date and todays date.
The following works perfectly when I want to view the days
SELECT DATEDIFF(now(),shipping_date) from tracking as days_transit
But when I try to make a new column with the following code I get errors
alter table tracking add days_transit as DATEDIFF(now(),shipping_date)
#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 'as cast(DATEDIFF(now(),shipping_date))' at line 1
What am I doing wrong?! I am using phpmyadmin
Just alter table to add the new column first with no values. Then next update the column values accordingly with update command.

Issue with MySQL Generated Columns

Hello I am trying to make a alter an exsisting table to add a Generated columns which is the count of all the rows in another table(It will be a like system so I am going to do all the matching and "WHERE" once I get this to work)
I am currently using this.
ALTER TABLE board ADD like_cnt INT GENERATED ALWAYS AS (COUNT(*) FROM likes) NOT NULL;
But it gives me this 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 'FROM likes) NOT NULL' at line 1
I am using WAMP and it has SQL version 5.7.24
Is this not possible or am I doing something wrong ?
There are limits in GENERATED COLUMNS notably 'Subqueries are not permitted'

MySQL Generated Columns of date type

i use mysql version 5.7
i have a field devicetime which is a datetime field.
for some reason i want to add a generated column which stores only the date part of the devicetime field.
i have tried the following statement
alter table mytable
add COLUMN recorddate date generated always as date(devicetime) stored;
i get an error
[Err] 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 'date(devicetime) stored' at line 2
i have taken the inputs from MySQL documentation from this link.
my current table structure is like this
For whatever reason MySQL needs some ()'s around the expression:
ALTER TABLE `mytable `
ADD COLUMN `recorddate` DATE GENERATED ALWAYS AS (date(devicetime)) STORED;
One solution came into my mind is,you can store TIMESTAMP into table.
And when try to get anywhere in your SYSTEM you can use DATE_FORMAT() and STR_TO_DATE() function to whatever part you required from that actual data.

MySQL syntax issue on database and table

What is wrong with my syntax?
I'm trying to update several databases in one shot:
update `db_name1`.`db_table` SET `cc_number_enc` = NULL
update `db_name2`.`db_table` SET `cc_number_enc` = NULL
update `db_name3`.`db_table` SET `cc_number_enc` = NULL
and I'm getting query syntax error in phpmyadmin
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 'cc_number_enc = NULL' at line 1
UPDATE
I've rewritten the same query simply by copying & pasting, and now getting the following:
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 'cc_number_enc = NULL' at line 1
Question, is it matter from which database I'm running the query from in phpmyadmin?
try puting semicolon after query. It can happen because of multiple reasons.
references http://www.inmotionhosting.com/support/website/database-troubleshooting/error-1064
MySQl Error #1064
MySQL Nested Queries with Joins
Looks like when copying, your backticks are changed to some other symbol, which looks like backtick, but actually, it is not.
Try again without quotes, or manually put backticks in place.

Why does this ALTER TABLE query throw syntax error?

This is the query I'm using:
ALTER TABLE apartment ADD technical TEXT NOT NULL AFTER is_sale
Why does it produce a syntax error?
EDIT:
ALTER TABLE apartment ADD COLUMN technical TEXT NOT NULL AFTER is_sale still produces an error:
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 'ALTER TABLE andreevka_apartment ADD COLUMN technincal TEXT NOT NULL AFTER' at line 1
Also doesn't validate: http://developer.mimer.com/validator/parser200x/index.tml#parser
It actually turned out that my query was copy-pasted from PhpMyAdmin and had some encoding issues. I pasted it into Notepad++, converted it to ANSI and it worked. Weird.
The syntax was perfectly fine.
You have a syntax error, to add column you should use COLUMN in sentence
ALTER TABLE apartment ADD COLUMN technical TEXT NOT NULL
EDITED
I think you cannot use AFTER with ALTER because it won't care of column order, it does not matter for storage or querying.
ALTER TABLE apartment ADD technical TEXT NOT NULL AFTER is_sale is working fine and I tested it.I use HeidiSQL as client.Here no need of ADD COLUMN.check whether is_sale name is correct or apartment name is correct
use ADD COLUMN for adding new column
ALTER TABLE apartment ADD COLUMN technical TEXT NOT NULL AFTER is_sale
Not sure it's allowed in MySQL, but in Oracle there's another syntax as well:
ALTER TABLE apartment ADD (technical TEXT NOT NULL AFTER is_sale);