SQL Stored Procedure Deleting all rows in table - mysql

I am trying to delete a single row using a stored procedure in MySQL, however the stored procedure is deleting all rows. The stored procedure is:
CREATE DEFINER=`sherattd`#`%` PROCEDURE `deleteFilm`(in ID int)
BEGIN
delete from films where id=ID;
END
The table I am trying to delete from has headings:
create table films (
id int(8),
title varchar(100),
year int(8),
director varchar(100),
stars varchar(100),
review text);
Thanks

Because you use as parameter the same name as the column name. It is case insensitive.
where id=id
is always true. Choose another parameter name!

Parameter name needs to be different as per answer by #juergen d
e.g.
CREATE DEFINER=`sherattd`#`%` PROCEDURE `deleteFilm`(in FID int)
BEGIN
delete from films where id=FID;
END

Related

I am trying to create a PROCEDURE that INSERTS values via paramerters but it's not working

DELIMITER &&
DROP PROCEDURE IF EXISTS add_row_in_emp_table;
CREATE PROCEDURE add_row_in_emp_table
(
empcode VARCHAR(15),
empname VARCHAR(15),
deptcode VARCHAR(15),
birthdate DATE,
sex CHAR(1),
desigcode varchar(15),
supcode varchar(15),
gradecode varchar(15),
gradelevel varchar(15),
basicpay varchar(15)
)
BEGIN
INSERT INTO emp
(empcode)
values(
empcode,empname,deptcode,birthdate,sex,desigcode,
supcode,gradecode,gradelevel,basicpay
);
END &&
DELIMITER ;
it is showing commands out of sync. can you please explain what is wrong?
i also tried using session variables that also is not working
Use columns name if few columns value are inserted. But if all columns value are inserted then columns name using are not mandatory but may be a chance for data type mismatch. Also passing session value with parameter or handle with variable inside procedure.
CREATE PROCEDURE add_row_in_emp_table (IN ecode varchar(15),IN ename varchar(15))
BEGIN
insert into emp (empcode, empname) values (ecode,ename);
END
N.B.: use columns name for inserting data or selecting data for better readability
Please check this url https://dbfiddle.uk/ODxp-NFH

MYSQL - Trigger -update /deletion

I have to create a trigger a which captures old and new value while updation & deletion taken place
Audit table :
create table if not EXISTS tbl_audits
(
TA_Auditid int AUTO_INCREMENT PRIMARY key,
TA_Name varchar(100),
TA_Oldvalue varchar(1000),
TA_Newvalue varchar(1000),
TA_Actiontaken varchar(100),
TA_createddt datetime,
TA_createdby varchar(100)
)
DELIMITER //
create TRIGGER TRfiledetails_update
BEFORE update on tbl_file_details
for EACH ROW
BEGIN
insert into tbl_audits
set TA_createddt =CURRENT_TIMESTAMP,
TA_Actiontaken ='update',
TA_Name ='File',
TA_Oldvalue =old.FL_dtstartdt ,
TA_Newvalue =new.FL_dtstartdt
end //
DELIMITER ;
In my main table i have to create trigger an event on two columns startdt and enddate.
In this case I would like to know whether I need to create two triggers separately for each column.
Is it possible to create two columns action on same trigger or need to create separate columns in audit table.

MYSQL Trigger To Populate New Table

Hey all so I have created a table named products that looks like so:
`CREATE TABLE products
(
Prod_ID int(10),
Prod_name varchar(20),
Prod_qty varchar(20),
Prod_price int(20)
);`
Product_log table is nearly identical to another table called products:
`CREATE TABLE product_log
(
Prod_ID int(10),
Prod_name varchar(20),
Action_Date date,
Updated_by varchar(30),
Action varchar(30)
);`
Next I have created a trigger called products_after_insert which should insert data into the product_log table after a row in products is inserted.
The requirement for after insert trigger is that action date should be inserted in the product_log table and the user name should be inserted like who inserted the data ex: data operator,
and on last the action should be inserted in the product_log table automatically like action here is insertion.
here is my trigger:
`DELIMITER //
CREATE TRIGGER products_after_insert
AFTER INSERT
ON products
FOR EACH ROW
BEGIN
DECLARE data_operator int(10);
DECLARE action_per varchar(200);
SET data_operator = 1;
SET action_per = 'INSERTION';
IF data_operator=1 THEN
INSERT INTO product_log(prod_id,prod_name,Action_date,Updated_by,Action)
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),'data_operator','action_per');
END IF;
END;
//DELIMITER;`
Now, I assume I am constructing my trigger incorrectly because it appears to not be working.
Does anyone know what I am doing wrong? Any help would be greatly appreciated! Thanks!
There's at least this you are doing wrong :
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),'data_operator','action_per');
You have properly defined the variables data_operator and action_per previously:
SET data_operator = 1;
SET action_per = 'INSERTION';
But in your INSERT instruction you surrounded them with quotes, which turned them into strings. Your instruction should be :
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),data_operator,action_per);

How can I use a trigger for populating 3 tables based on another table (populated with 'load data infile')?

I have a final project for a MySQL class so I have the following requests:
I have a table called tab_interm with the following columns:
DateVisit varchar(50),
hourentering time,
SnamePatient varchar(100),
FnamePatient varchar(100),
SNameDoctors varchar(100),
FnameDoctors varchar(100),
Cabinet varchar(100));
The data for populating this table are in a file.txt.
So I have to create a trigger which populates the following tables:
Pacients (id_pacient int auto_increment PK, Sname varchar(50), fname varchar(50))
Cabinets (id_cabinet int auto_increment PK, name) and
visits (datehourvisits datetime, id_doctor, id_patient, id_cabinet, last 3 being FK).
The table doctors (id_doctor, sname, fname) is already populated.
The FK in visits will be populated using last_insert_id().
The trigger will also call a stored function that will transform datavisit and hourentering into a datetime column (probably with str_to_date and concat)
Also I have to declare a continue handler which is activated when a duplicated value is inserted.
I really don't know how to start this trigger. Maybe you will have an idea... thank you.

Create new table procedure

I'm trying to create a procedure which adds / removes a table to the database. I'm working in SQL Server. The query works (it succeeds) but the table isn't added to the database.
I did refreshed...
ALTER procedure [dbo].[upgrade_1]
as
begin
create table awards (
ID int NOT NULL IDENTITY,
name nvarchar(256) DEFAULT 'award',
description nvarchar(256)
PRIMARY KEY (ID)
)
/*update goto_vs
set current_version = 1*/
end
The script you have in the question will only modify the PROCEDURE. The procedure needs to be executed for it to perform the required task e.g. create the table
Execute the procedure with this statement
EXEC upgrade_1
That should create the table