update query inside view in mysql - mysql

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.

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'

Enter Image URL (http://...) in mysql table

I tried using this in mysql:
INSERT INTO users (url) VALUES (http://31.media.tumblr.com/tumblr_ljtc6i9GPA1qbq4v6o1_400.gif) WHERE id='15';
But it gives this 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 '://31.media.tumblr.com/tumblr_ljtc6i9GPA1qbq4v6o1_400.gif) WHERE id='15'' at line 1"
Can anyone tell me what is the reason and how to store the url in the table?
INSERT INTO statements don't have a WHERE clause. If you are wanting to use a WHERE clause you are just UPDATEing a row.
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

MySQL Truncate Table Before Insert

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?

MySQL Drop table doesn't work for prefix

I am trying to drop tables with wp_ prefix but its giving error below
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 'WHERE TABLE LIKE 'wp_%'' at line 1
Here is my query
"DROP TABLE WHERE TABLE LIKE '{$wp}%'"
What is wrong in this query? Please help
As far as I know, you can't selectively delete tables. You have to specifically delete each table, since deletion can't use a filter. You could probably use the metadata to get the names of all of your tables, and then find out in your code which ones start with wp_. Then, you would just loop through your list of tables to delete and then delete them with drop table [table-name];.
To get the list of table names from metadata, use select table_name from information_schema.tables;.