MySQL Truncate Table Before Insert - mysql

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?

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.

Update else INSERT in mysql

I tested this code:
UPDATE books SET price='20000' WHERE user_id='2'
IF ROW_COUNT()=0
INSERT INTO store_books(name,user_id) VALUES ('test1','2')
I encountered the following error.
error : 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 'IF ROW_COUNT()=0 INSERT INTO store_books(name,user_id) VALUES
('test1','2')' at line 2
Is there any solution to solve this problem?
I don't want to use INSERT INTO ... ON DUPLICATE KEY UPDATE (because i have multi keys in my main table).
Above example is trial for finding new way.
Pattern that i want:
Update(if exists) ELSE Insert.
Two things:
INSERT ... ON DUPLICATE KEY UPDATE ... is by far the best way to do what you want to do. If you don't do this, you'll have to use a database transaction to make sure you maintain integrity for the operation you want.
MySQL (unlike, say, MS SQL server) doesn't allow conditional execution of queries except in stored procedures. Read this. If conditional in SQL Script for Mysql

update query inside view in 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.

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;.

MySQL Triggers, drop query inside trigger

I am trying to write a trigger to not allow an update of a relation if a statement is true, and I am running into some trouble
CREATE TRIGGER noPriceLowerSpeed
BEFORE UPDATE
ON pc
FOR EACH ROW
BEGIN
IF(new.speed IS IN (SELECT speed FROM pc AS pc1) AND pc1.price < new.price)
THEN DROP new
END IF;
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 'IN (SELECT speed FROM pc AS pc1) AND pc1.price <
new.price) THEN DROP new
I am trying to not allow a pc into my pc relation if it has a higher price than a pc with the same speed.
How might I write this trigger to just not allow the update?
IS is superfluous. You should just use the following syntax:
new.speed IN( ... )
Also, I think you missed a ; after THEN DROP new.
To make the statement fail, use the SIGNAL statement in the trigger, to raise an error.
http://dev.mysql.com/doc/refman/5.5/en/signal.html