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.
Related
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
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'
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> create view incremented_salary as
-> update employee set salary=salary*1.1;
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
'update employee set salary=salary*1.1' at line 2
can we put update query inside view? If yes then please tell me why I'm getting above error.I want to increment the salary and display it using view.
Why dont you
create view incremented_salary as
select salary*1.1 from wahtevertable
Views do not modify the values of tables.
They give you a new View on them - f.e. joining multiple tables, aggregating and maybe recalculate some values based on tablevalues.
If what you tried would work, everytime you`d viewed the data, it would increase in value.
I am creating a table in MySQL for which I only ever want it to contain one tuple at a time. To enforce this, I am trying to create a trigger which will truncate the table each time an INSERT occurs. However, I am running into problems.
SQL:
CREATE TRIGGER `tbl_hire_truncate`
BEFORE INSERT ON `tbl_hire`
FOR EACH ROW
BEGIN
TRUNCATE TABLE `tbl_hire`;
END
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 '' at line 5
Every example of a trigger that I've seen uses a FOR EACH loop, but it certainly doesn't make much sense with what I am trying to achieve.
How can I rewrite my SQL to achieve my goal?