{"id":21285035,"url":"https://github.com/puneetkumar041/mysql_development_work","last_synced_at":"2026-05-14T23:33:12.360Z","repository":{"id":109679929,"uuid":"416187493","full_name":"puneetkumar041/MySQL_Development_Work","owner":"puneetkumar041","description":"DaB Development work| Stored Procedure | Trigger | Cursor | Events |  Table Partition","archived":false,"fork":false,"pushed_at":"2024-07-17T19:55:16.000Z","size":3105,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T05:14:34.962Z","etag":null,"topics":["autokill","cursor","cursors","database","development","events","mysql-database","optimization","partitioning","plsql","sql","stored-procedure","stored-procedures","table-partitioning","trigger","triggers"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/puneetkumar041.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-12T04:48:53.000Z","updated_at":"2024-07-17T19:55:19.000Z","dependencies_parsed_at":"2023-03-22T20:48:10.430Z","dependency_job_id":null,"html_url":"https://github.com/puneetkumar041/MySQL_Development_Work","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puneetkumar041%2FMySQL_Development_Work","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puneetkumar041%2FMySQL_Development_Work/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puneetkumar041%2FMySQL_Development_Work/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/puneetkumar041%2FMySQL_Development_Work/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/puneetkumar041","download_url":"https://codeload.github.com/puneetkumar041/MySQL_Development_Work/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243746200,"owners_count":20341203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autokill","cursor","cursors","database","development","events","mysql-database","optimization","partitioning","plsql","sql","stored-procedure","stored-procedures","table-partitioning","trigger","triggers"],"created_at":"2024-11-21T11:17:54.870Z","updated_at":"2026-05-14T23:33:12.320Z","avatar_url":"https://github.com/puneetkumar041.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg width=\"150\" alt=\"Screenshot 2023-02-17 at 9 48 17 AM\" src=\"https://user-images.githubusercontent.com/25247630/219548391-874c9fe8-45c8-46a0-ae7e-425659632eb3.png\"\u003e\n\n\n### ⚡ Development Tasks for Day to Day Activities including stored procedures, cursors, triggers, events... \n\n\u003cimg width=\"180\" alt=\"image\" src=\"https://user-images.githubusercontent.com/25247630/219543934-9b5d5fd8-1b72-4173-b3a4-f16499f954b3.png\"\u003e\n\nIf we consider the enterprise application, we always need to perform specific tasks such as database cleanup, processing payroll, and many more on the database regularly. Such tasks involve multiple SQL statements for executing each task. This process might easy if we group these tasks into a single task. We can fulfill this requirement in MySQL by creating a stored procedure in our database.\n\n### Stored Procedure Features...\n\n*) Stored Procedure increases the performance of the applications. Once stored procedures are created, they are compiled and stored in the database.\n\n*) Stored procedure reduces the traffic between application and database server. Because the application has to send only the stored procedure's name and parameters instead of sending multiple SQL statements.\n\n*) Stored procedures are reusable and transparent to any applications.\n\n*) A procedure is always secure. The database administrator can grant permissions to applications that access stored procedures in the database without giving any permissions on the database tables.\n\n### Procedure without Parameter...\n\n    DELIMITER $$\n    \n    DROP PROCEDURE IF EXISTS `getcount_student` $$\n    \n    CREATE PROCEDURE `getcount_student`()\n    \n    BEGIN \n    \n    SELECT * FROM student_info WHERE marks \u003e 70;  \n    \n    SELECT COUNT(stud_code) AS Total_Student FROM student_info;    \n\n    END $$\n   \n    DELIMITER ;\n\nLet us call the procedure to verify the output:\n\n    CALL getcount_student();  \n\n\n### Procedure without Parameter\n\n    DELIMITER $$\n    \n    DROP PROCEDURE IF EXISTS `getcount_student` $$\n    \n    CREATE PROCEDURE `getcount_student`()\n    \n    BEGIN \n    \n    SELECT * FROM student_info WHERE marks \u003e 70;  \n    \n    SELECT COUNT(stud_code) AS Total_Student FROM student_info;    \n\n    END $$\n   \n    DELIMITER ;\n\nLet us call the procedure to verify the output:\n\n    CALL getcount_student(); \n    \n    \n    \n\u003cimg width=\"98\" alt=\"Screenshot 2023-02-17 at 9 53 52 AM\" src=\"https://user-images.githubusercontent.com/25247630/219549151-1ff5eaaa-edab-471c-b4ca-5e218f19d640.png\"\u003e\n\nTo handle a result set inside a stored procedure, you use a cursor. A cursor allows you to iterate a set of rows returned by a query and process each row individually.\n\n\n### Features of a MySQL Cursor...\n\n*) A cursor is read-only and cannot update or remove data in the result set from the procedure.\n\n*) A cursor needs to be declared before it can be used. The cursor definition is only a step to tell MySQL that such a cursor exists and does not retrieve and data.\n\n*) You can only retrieve data in the order specified by the select statement and not in any reverse order, commonly known as non-scrollable.\n\n*) You use a cursor by opening it and then perform fetch operations on the data stored.\n\n*) You must close a cursor after the fetch operations complete.\n\n*) cursor makes the specific task easier by iterating each table row. \n\n\n### Working with MySQL cursor...\n\nFirst, declare a cursor by using the DECLARE statement:\n\n    DECLARE cursor_name CURSOR FOR SELECT_statement;\n    \n\nThe cursor declaration must be after any variable declaration. If you declare a cursor before the variable declarations, MySQL will issue an error. A cursor must always associate with a SELECT statement.\n\nNext, open the cursor by using the OPEN statement. The OPEN statement initializes the result set for the cursor, therefore, you must call the OPEN statement before fetching rows from the result set.\n\n    OPEN cursor_name;\n\n\nThen, use the FETCH statement to retrieve the next row pointed by the cursor and move the cursor to the next row in the result set.\n\n    FETCH cursor_name INTO variables list;\n\n\nAfter that, check if there is any row available before fetching it.\n\nFinally, deactivate the cursor and release the memory associated with it  using the CLOSE statement:\n\n    CLOSE cursor_name;\n\n\nIt is a good practice to always close a cursor when it is no longer used.\nWhen working with MySQL cursor, you must also declare a NOT FOUND handler to handle the situation when the cursor could not find any row.\n\nBecause each time you call the FETCH statement, the cursor attempts to read the next row in the result set. When the cursor reaches the end of the result set, it will not be able to get the data, and a condition is raised. The handler is used to handle this condition.\n\nTo declare a NOT FOUND handler, you use the following syntax:\n\n    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;\n\n\nThe finished is a variable to indicate that the cursor has reached the end of the result set. Notice that the handler declaration must appear after variable and cursor declaration inside the stored procedures.\n\n\n\n \n    \n### How it works...\n\n### \u003cimg width=\"591\" alt=\"Screenshot 2023-02-17 at 9 58 44 AM\" src=\"https://user-images.githubusercontent.com/25247630/219570739-03b54dc8-f52e-47b5-8b9b-6140ceea2f36.png\"\u003e\n\n\n    \n ### Procedure using cursor...\n \n \tuse tests;\n\n\tuse DB;\n\n\tDELIMITER $$\n\n\tDROP PROCEDURE IF EXISTS delete_duplicate_emp_email$$\n\n\tCREATE PROCEDURE delete_duplicate_emp_email()\n\n\tBEGIN \n\n\tDECLARE v_id int(11);\n\tDECLARE v_name varchar(20);\n\tDECLARE v_email varchar(100);\n\tDECLARE done_handler INT(5) DEFAULT '0';\n\tDECLARE done INT DEFAULT 0;\n\tDECLARE crsr CURSOR FOR SELECT id,name,email FROM employees where done_flag=0;\n\n\tDECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;\n\tSET done = 0;\n\n\t   \n\tOPEN crsr;\n\n\tREPEAT\n\n\tFETCH  crsr  INTO v_id,v_name,v_email ;\n\n\tIF NOT done THEN \n\n\tdelete b.* from employee_email b where  b.uw_item_id= v_id and b.name =v_name and  b.id!=v_email;\n\n\tupdate employees set done_flag=1 where uw_item_id= v_id and name =v_name and email=v_email;\n\n\tSET done = done_handler;\n\n\tEND IF;      \n\n\tUNTIL done END REPEAT;\n\n\tCLOSE crsr;\n\n\tEND $$\n\n\tDELIMITER ;\n \n \n\u003cimg width=\"144\" alt=\"Screenshot 2023-02-17 at 9 54 23 AM\" src=\"https://user-images.githubusercontent.com/25247630/219549075-24d2b924-f81d-4904-a17d-cfe09b951bc6.png\"\u003e\n\nPartitioning in MySQL is used to split or partition the rows of a table into separate tables in different locations, but still, it is treated as a single table. It distributes the portions of the table's data across a file system based on the rules we have set as our requirement. \nBenefits of Partitioning\n\nThe following are the benefits of partitioning in MySQL:\n\n*) It optimizes the query performance. When we query on the table, it scans only the portion of a table that will satisfy the particular statement.\n\n*) It is possible to store extensive data in one table that can be held on a single disk or file system partition.\n\n*) It provides more control to manage the data in your database.\n\nTypes of MySQL Partitioning\nMySQL has mainly six types of partitioning, which are given below:\n\n*) RANGE Partitioning\n\n*) LIST Partitioning\n\n*) COLUMNS Partitioning\n\n*) HASH Partitioning\n\n*) KEY Partitioning\n\n*) Subpartitioning\n\nHere we have discussed about Range Partitining \n \n ### MySQL RANGE Partitioning...\n\nThis partitioning allows us to partition the rows of a table based on column values that fall within a specified range. The given range is always in a contiguous form but should not overlap each other, and also uses the VALUES LESS THAN operator to define the ranges.\n\nHere we have added script for add partition and remove partition keeping certain number of days partitons and made the process automatic using mysql events\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpuneetkumar041%2Fmysql_development_work","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpuneetkumar041%2Fmysql_development_work","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpuneetkumar041%2Fmysql_development_work/lists"}