Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

My this question is in sequence of my previous question I asked.

I could write two solutions using Stored Procedure instead of trigger or nested-query .

Both use a helper function my_signal(msg).

A Stored Procedure to delete employee from Employee Table.

  • Fist Solution: use UPDATE rows in table, without join operation:
CREATE PROCEDURE delete_employee(IN dssn varchar(64))
BEGIN
    DECLARE empDesignation varchar(128);
    DECLARE empSsn         varchar(64);
    DECLARE empMssn        varchar(64);
     SELECT SSN, designation, MSSN  
       INTO empSsn, empDesignation, empMssn 
     FROM Employee 
     WHERE SSN = dssn;

   IF (empSsn IS NOT NULL) THEN
    CASE       
           WHEN empDesignation = 'OWNER' THEN 
               CALL my_signal('Error: OWNER can not deleted!');

           WHEN empDesignation = 'WORKER' THEN 
            DELETE FROM Employee WHERE SSN = empSsn;               

           WHEN empDesignation = 'BOSS' THEN 
               BEGIN 
                   UPDATE Employee
                   SET MSSN = empMssn
                   WHERE MSSN = empSsn;

                DELETE FROM Employee WHERE SSN = empSsn;                   

               END;
    END CASE;
   ELSE 
               CALL my_signal('Error: Not a valid row!');
   END IF;
END//  
  • Second solution: as I was suggested in my previous question using INNER JOIN
CREATE PROCEDURE delete_employee(IN dssn varchar(64))
BEGIN
    DECLARE empDesignation varchar(128);
    DECLARE empSsn         varchar(64);
    DECLARE empMssn        varchar(64);
      SELECT SSN, designation, MSSN  
        INTO empSsn, empDesignation, empMssn 
      FROM Employee 
      WHERE SSN = dssn;

   IF (empSsn IS NOT NULL) THEN
       IF (empDesignation = 'OWNER') THEN 
        CALL my_signal('Error: OWNER can not deleted!');
       END IF;

       UPDATE `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
       SET B.MSSN = A.MSSN WHERE A.SSN = empSsn;

       DELETE FROM `Employee` WHERE SSN = empSsn;
   ELSE 
       CALL my_signal('Error: Not a valid row!');
   END IF;    
END//

I read here that using join is efficient for Efficient SELECT. But my problem includes only one table and I feel my solution(first) is much efficient than second because join will consume memory comparatively.

Please suggest me which is better and efficient, if Employee table is sufficiently large. Which is better for me? Reason

EDIT: I checked for small table consist of 7 rows only, and both solution take same time.

mysql> CALL delete_employee(4);
Query OK, 1 row affected (0.09 sec)

I know SQL function behaves non-deterministically because table heuristics. Which choice is better? Either if you have some idea How query can be further optimised.

share|improve this question
@ypercube : Thanks for your attention...But Sorry! I can not understood your point. Please revisit question ~ my request.. As both solutions are working correctly. I have checked for all cases. – Grijesh Chauhan Nov 23 '12 at 8:07
1  
There's probably only minor difference in efficiency but I don't see a reason to do the join again, since you already have the data you need from the first SELECT. So, keep the first version. – ypercube Nov 23 '12 at 8:53
Same I feels, But I was suggested in answers by very expert persons and both used similar way so I have doubt. – Grijesh Chauhan Nov 23 '12 at 8:58
1  
And testing for efficiency with a 7-rows table is useless. Try with a 7000-rows table to see if there is any. But I prefer the simpler way, which works fine with sending the error messages. If you had no error messages, you could probably write the whole procedure without IFs and the SELECT, with only two statements, an UPDATE and a DELETE. – ypercube Nov 23 '12 at 9:03
@ypercube : Yes, correct..Will try..Thanks ypercube! – Grijesh Chauhan Nov 23 '12 at 9:09

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.