Let's say that I have a series of tables in mysql, and some of them are dependent on other tables (so if I didn't want to force delete, i would have to delete them in order). Now let's say I had a little script to delete them in order...Now let's say I wanted to run that in mysql workbench, and better yet have a function that took in a parameter (like userId) and did the above... How would I do such a thing in Mysql workbench in a way that I could easily retrieve and run the code (like for example if I wanted to delete a user and all the other objects associated with the user.)
You can use a stored procedure
DELIMITER //
CREATE PROCEDURE delete_user(IN _user_id INT)
BEGIN
START TRANSACTION;
DELETE FROM user_data WHERE user_id = _user_id;
-- your other delete statements go here in a proper order
DELETE FROM users WHERE id = _user_id;
COMMIT;
END//
DELIMITER ;
Sample usage:
CALL delete_user(2); -- delete a user with id = 2
Here is a SQLFiddle demo
Related
Within my database, I have a large number of tables which contain dates. These are expiry dates to be precise.
Currently, I perform the following commands to remove any rows which are older than a specified date using:
DELETE FROM tablename WHERE DATE(datepoint) < '2019-03-18'
1) Is there a way I can instead search and delete from an entire database instead of manually changing the table name and running the command each time?
You could use a PHP function, Anyway I wouldn't recommend deleting any records, just filter your queries for the last 3 months only
Perhaps consider having expiry dates in their own table EXPIRY_DATES. It sounds like it could be an important concept in your domain. Then have your other tables point to the correct expiry date using a foreign key. Using on delete cascade rules from EXPIRY_DATES would clean out records from all child tables when you delete one expiry date from EXPIRY_DATES.
you shoould use procedure (or function):
Define a procedure :
mysql> CREATE PROCEDURE delete_from_table(OUT table_name char(20))
-> BEGIN
-> delete from table_name WHERE DATE(datepoint) < '2019-03-18';
-> END//
mysql> delimiter ;
CALL procedure :
mysql> CALL delete_from_table(#table1);
mysql> CALL delete_from_table(#table2);
mysql> CALL delete_from_table(#table3);
Background - I have a DB created from a single large flat file. Instead of creating a single large table with 106 columns. I created a "columns" table which stores the column names and the id of the table that holds that data, plus 106 other tables to store the data for each column. Since not all the records have data in all columns, I thought this might be a more efficient way to load the data (maybe a bad idea).
The difficulty with this was rebuilding a single record from this structure. To facilitate this I created the following procedure:
DROP PROCEDURE IF EXISTS `col_val`;
delimiter $$
CREATE PROCEDURE `col_val`(IN id INT)
BEGIN
DROP TEMPORARY TABLE IF EXISTS tmp_record;
CREATE TEMPORARY TABLE tmp_record (id INT(11), val varchar(100)) ENGINE=MEMORY;
SET #ctr = 1;
SET #valsql = '';
WHILE (#ctr < 107) DO
SET #valsql = CONCAT('INSERT INTO tmp_record SELECT ',#ctr,', value FROM col',#ctr,' WHERE recordID = ',#id,';');
PREPARE s1 FROM #valsql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
SET #ctr = #ctr+1;
END WHILE;
END$$
DELIMITER ;
Then I use the following SQL where the stored procedure parameter is the id of the record I want.
CALL col_val(10);
SELECT c.`name`, t.`val`
FROM `columns` c INNER JOIN tmp_record t ON c.ID = t.id
Problem - The first time I run this it works great. However, each subsequent run returns the exact same record even though the parameter is changed. How does this persist even when the stored procedure should be dropping and re-creating the temp table?
I might be re-thinking the whole design and going back to a single table, but the problem illustrates something I would like to understand.
Unsure if it matters but I'm running MySQL 5.6 (64 bit) on Windows 7 and executing the SQL via MySQL Workbench v5.2.47 CE.
Thanks,
In MySQL stored procedures, don't put an # symbol in front of local variables (input parameters or locally declared variables). The #id you used refers to a user variable, which is kind of like a global variable for the session you're invoking the procedure from.
In other words, #id is a different variable from id.
That's the explanation of the immediate problem you're having. However, I would not design the tables as you have done.
Since not all the records have data in all columns, I thought this might be a more efficient way to load the data
I recommend using a conventional single table, and use NULL to signify missing data.
It is possible to use a version control system with mysql databases?
Or, is there a version control system already implemented?
I want to say e.g.: SELECT foo FROM bar WHERE version = X
Whereeby version is a mysql internal colum with last update date.
Very late response... similar to Ruben's suggestion, I have setup triggers to update a version_control table to increment version number every time there is an INSERT, UPDATE & DELETE.
I laid out the steps on my site mradamfrancis.tumblr.com
** update **
I’ve decided to use triggers to assist with version control. Here’s how…
I have a table containing players, if there are changes (INSERT, DELETE, or UPDATE) I want to increment the version number in my version_control table.
This is how the version_control table looks:
version_id (key), table_name (varchar), version (integer)
I then create 3 triggers on the players table, one for INSERT, DELETE & UPDATE.
INSERT:
delimiter //
CREATE TRIGGER `player_table_INSERT` AFTER INSERT ON `players`
FOR EACH ROW BEGIN
UPDATE version_control SET version=version+1 WHERE table_name=’players’;
END;//
delimiter ;
DELETE:
delimiter //
CREATE TRIGGER `player_table_DELETE` AFTER DELETE ON `players`
FOR EACH ROW BEGIN
UPDATE version_control SET version=version+1 WHERE table_name=’players’;
END;//
delimiter ;
UPDATE:
delimiter //
CREATE TRIGGER `player_table_UPDATE` AFTER UPDATE ON `players`
FOR EACH ROW BEGIN
UPDATE version_control SET version=version+1 WHERE table_name=’players’;
END;//
delimiter ;
** I have additional SQL statements in the FOR EACH section of the trigger, hence I’ve used delimiter (1st line and last line) along with BEGIN & END.
You could also define extra tables for logging. For instance if you already have a table news, you can duplicate it as news_log. then add columns for logging data such as: modified date, action (update, delete, add) and so on.
next you define triggers on the original tables that will insert the data into your logging table. for instance when you update a record in you news table the news_log_trigger that you define is executed and a new record is inserted into new_log with the action value "UPDATE" and the current date as modified date.
for more info on mysql triggers:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
In case you want to do this for every project you can problably write a generic stored procedure to do the actual logging. that way you can reuse it and you only have to define the triggers and logging tables.
To futher clarify i was trying to create a trigger that checks a table for a number in sql.
If it finds said number then it erases that entire row.
It uses a separate table of names to check.
I thought it be could done using a join but have had no luck.
So it would look like this I suppose if(tb1.name = tb2.name) then DELETE row.
I'm sorry if the formatting is off.
EDIT; I am using phpmyadmin so some of the the code may be missing but here is the code from my latest "attempt"
It uses on INSERT and time is set to AFTER
SELECT * FROM flights WHERE NOT EXISTS (SELECT * FROM no fly list WHERE PassengerId.Id = Passenger.Id)
have not added the DELETE as of now but the work is somewhat ongoing
Assuming that flight and dnf both have passenger_id column that uniquely identifies a passenger of interest then your trigger might look like this
DELIMITER $$
CREATE TRIGGER dnf_insert AFTER INSERT
ON dnf
FOR EACH ROW BEGIN
DELETE FROM flights WHERE passenger_id = NEW.passenger_id;
END$$
DELIMITER ;
If you post DDL (create table statements) for all relevant tables we can refine the query
I've been using this MySQL trigger on a dedicated server, so every time a table is updated, it calculates a new sum and updates another table:
delimiter |
CREATE TRIGGER after_insert_rep_points AFTER INSERT ON users_reputation
FOR EACH ROW
BEGIN
DECLARE new_total INT(10);
SELECT SUM(rep_points) INTO new_total FROM users_reputation
WHERE user_id = NEW.user_id;
UPDATE users SET rep_total = new_total WHERE user_id = NEW.user_id;
END
|
delimiter ;
I now need to use this on a shared hosting environment, but unfortunately, due to security precautions, I cannot have SUPER USER privileges in a shared environment, which is required for triggers. I don't understand this. I think everybody should be able to use triggers.
I'm not that clued up on stored procedures, in-fact I'm useless, but I'm wondering if this query can be executed using one and if so, can anybody can give me any pointers?
The person who wrote the trigger obviously has very limited SQL skills - the whole body can be expressed as a simple query:
UPDATE users SET
rep_total = (SELECT SUM(rep_points)
FROM users_reputation
WHERE user_id = users.user_id)
WHERE user_id = ?; -- "?" is NEW.user_id inside the trigger. You must provide it
You don't need a stored procedure - just run this query.