snippetsqlMinor
How to Update same table on deletion in MYSQL?
Viewed 0 times
sameupdatemysqlhowdeletiontable
Problem
In my Database I have a table:
Following is query I used to create table:
And I could successfully create my table:
Then I inserted some tuple in Employee table using following query.
Now I have following hierarchical relation among the rows in table:
Following is Select statement for table:
```
mysql> SELECT * FROM Employee;
+-----+------+-------------+------+
| SSN | name | designation | MSSN |
+-----+------+-------------+------+
| 1 | A | OWNER | NULL |
| 2 | B | BOSS | 1 |
| 3 | C | WORKER | 2 |
| 4 | D | B
Employee with recursive association, an employee can be boss of other employee. Following is query I used to create table:
CREATE TABLE IF NOT EXISTS `Employee` (
`SSN` varchar(64) NOT NULL,
`name` varchar(64) DEFAULT NULL,
`designation` varchar(128) NOT NULL,
`MSSN` varchar(64) DEFAULT NULL,
PRIMARY KEY (`SSN`),
CONSTRAINT `FK_Manager_Employee` FOREIGN KEY (`MSSN`) REFERENCES Employee(SSN)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;And I could successfully create my table:
mysql> DESCRIBE Employee;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| SSN | varchar(64) | NO | PRI | NULL | |
| name | varchar(64) | YES | | NULL | |
| designation | varchar(128) | NO | | NULL | |
| MSSN | varchar(64) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)Then I inserted some tuple in Employee table using following query.
mysql> INSERT INTO Employee VALUES
-> ("1", "A", "OWNER", NULL),
-> ("2", "B", "BOSS", "1"),
-> ("3", "C", "WORKER", "2"),
-> ("4", "D", "BOSS", "2"),
-> ("5", "E", "WORKER", "4"),
-> ("6", "F", "WORKER", "1"),
-> ("7", "G", "WORKER", "4")
-> ;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0Now I have following hierarchical relation among the rows in table:
A
/ \
B F
/ \
C D
/ \
G EFollowing is Select statement for table:
```
mysql> SELECT * FROM Employee;
+-----+------+-------------+------+
| SSN | name | designation | MSSN |
+-----+------+-------------+------+
| 1 | A | OWNER | NULL |
| 2 | B | BOSS | 1 |
| 3 | C | WORKER | 2 |
| 4 | D | B
Solution
The easiest way to delete
First, I'll load your sample data
View all current relationships
With this
let's elevate
At this point,
Now, delete it and show relationships:
Give it a Try !!!
Please notice that you do not need a Trigger
D is to do the following:- Have
BinheritGandE, putting them on the same level asD.
- Delete
D
First, I'll load your sample data
mysql> DROP DATABASE IF EXISTS grijesh;
Query OK, 1 row affected (0.06 sec)
mysql> CREATE DATABASE grijesh;
Query OK, 1 row affected (0.00 sec)
mysql> USE grijesh
Database changed
mysql> CREATE TABLE IF NOT EXISTS `Employee` (
-> `SSN` varchar(64) NOT NULL,
-> `name` varchar(64) DEFAULT NULL,
-> `designation` varchar(128) NOT NULL,
-> `MSSN` varchar(64) DEFAULT NULL,
-> PRIMARY KEY (`SSN`),
-> CONSTRAINT `FK_Manager_Employee` FOREIGN KEY (`MSSN`) REFERENCES Employee(SSN)
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO Employee VALUES
-> ("1", "A", "OWNER", NULL),
-> ("2", "B", "BOSS", "1"),
-> ("3", "C", "WORKER", "2"),
-> ("4", "D", "BOSS", "2"),
-> ("5", "E", "WORKER", "4"),
-> ("6", "F", "WORKER", "1"),
-> ("7", "G", "WORKER", "4");
Query OK, 7 rows affected (0.05 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Employee;
+-----+------+-------------+------+
| SSN | name | designation | MSSN |
+-----+------+-------------+------+
| 1 | A | OWNER | NULL |
| 2 | B | BOSS | 1 |
| 3 | C | WORKER | 2 |
| 4 | D | BOSS | 2 |
| 5 | E | WORKER | 4 |
| 6 | F | WORKER | 1 |
| 7 | G | WORKER | 4 |
+-----+------+-------------+------+
7 rows in set (0.00 sec)
mysql>View all current relationships
mysql> SELECT
-> A.name,A.designation,
-> B.name,B.designation
-> FROM
-> `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> ORDER BY A.SSN,B.SSN
-> ;
+------+-------------+------+-------------+
| name | designation | name | designation |
+------+-------------+------+-------------+
| A | OWNER | B | BOSS |
| A | OWNER | F | WORKER |
| B | BOSS | C | WORKER |
| B | BOSS | D | BOSS |
| D | BOSS | E | WORKER |
| D | BOSS | G | WORKER |
+------+-------------+------+-------------+
6 rows in set (0.00 sec)
mysql>With this
UPDATE JOIN queryUPDATE `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
SET B.MSSN = A.MSSN WHERE A.SSN = '4';let's elevate
G and E to the same level as D and show relationshipsmysql> UPDATE `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> SET B.MSSN = A.MSSN WHERE A.SSN = '4';
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> SELECT
-> A.name,A.designation,
-> B.name,B.designation
-> FROM
-> `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> ORDER BY A.SSN,B.SSN
-> ;
+------+-------------+------+-------------+
| name | designation | name | designation |
+------+-------------+------+-------------+
| A | OWNER | B | BOSS |
| A | OWNER | F | WORKER |
| B | BOSS | C | WORKER |
| B | BOSS | D | BOSS |
| B | BOSS | E | WORKER |
| B | BOSS | G | WORKER |
+------+-------------+------+-------------+
6 rows in set (0.00 sec)
mysql>At this point,
D is nobody's boss.Now, delete it and show relationships:
mysql> DELETE FROM `Employee` WHERE SSN = '4';
Query OK, 1 row affected (0.05 sec)
mysql> SELECT
-> A.name,A.designation,
-> B.name,B.designation
-> FROM
-> `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> ORDER BY A.SSN,B.SSN
-> ;
+------+-------------+------+-------------+
| name | designation | name | designation |
+------+-------------+------+-------------+
| A | OWNER | B | BOSS |
| A | OWNER | F | WORKER |
| B | BOSS | C | WORKER |
| B | BOSS | E | WORKER |
| B | BOSS | G | WORKER |
+------+-------------+------+-------------+
5 rows in set (0.00 sec)
mysql>Give it a Try !!!
Please notice that you do not need a Trigger
Code Snippets
mysql> DROP DATABASE IF EXISTS grijesh;
Query OK, 1 row affected (0.06 sec)
mysql> CREATE DATABASE grijesh;
Query OK, 1 row affected (0.00 sec)
mysql> USE grijesh
Database changed
mysql> CREATE TABLE IF NOT EXISTS `Employee` (
-> `SSN` varchar(64) NOT NULL,
-> `name` varchar(64) DEFAULT NULL,
-> `designation` varchar(128) NOT NULL,
-> `MSSN` varchar(64) DEFAULT NULL,
-> PRIMARY KEY (`SSN`),
-> CONSTRAINT `FK_Manager_Employee` FOREIGN KEY (`MSSN`) REFERENCES Employee(SSN)
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO Employee VALUES
-> ("1", "A", "OWNER", NULL),
-> ("2", "B", "BOSS", "1"),
-> ("3", "C", "WORKER", "2"),
-> ("4", "D", "BOSS", "2"),
-> ("5", "E", "WORKER", "4"),
-> ("6", "F", "WORKER", "1"),
-> ("7", "G", "WORKER", "4");
Query OK, 7 rows affected (0.05 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Employee;
+-----+------+-------------+------+
| SSN | name | designation | MSSN |
+-----+------+-------------+------+
| 1 | A | OWNER | NULL |
| 2 | B | BOSS | 1 |
| 3 | C | WORKER | 2 |
| 4 | D | BOSS | 2 |
| 5 | E | WORKER | 4 |
| 6 | F | WORKER | 1 |
| 7 | G | WORKER | 4 |
+-----+------+-------------+------+
7 rows in set (0.00 sec)
mysql>mysql> SELECT
-> A.name,A.designation,
-> B.name,B.designation
-> FROM
-> `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> ORDER BY A.SSN,B.SSN
-> ;
+------+-------------+------+-------------+
| name | designation | name | designation |
+------+-------------+------+-------------+
| A | OWNER | B | BOSS |
| A | OWNER | F | WORKER |
| B | BOSS | C | WORKER |
| B | BOSS | D | BOSS |
| D | BOSS | E | WORKER |
| D | BOSS | G | WORKER |
+------+-------------+------+-------------+
6 rows in set (0.00 sec)
mysql>UPDATE `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
SET B.MSSN = A.MSSN WHERE A.SSN = '4';mysql> UPDATE `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> SET B.MSSN = A.MSSN WHERE A.SSN = '4';
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> SELECT
-> A.name,A.designation,
-> B.name,B.designation
-> FROM
-> `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> ORDER BY A.SSN,B.SSN
-> ;
+------+-------------+------+-------------+
| name | designation | name | designation |
+------+-------------+------+-------------+
| A | OWNER | B | BOSS |
| A | OWNER | F | WORKER |
| B | BOSS | C | WORKER |
| B | BOSS | D | BOSS |
| B | BOSS | E | WORKER |
| B | BOSS | G | WORKER |
+------+-------------+------+-------------+
6 rows in set (0.00 sec)
mysql>mysql> DELETE FROM `Employee` WHERE SSN = '4';
Query OK, 1 row affected (0.05 sec)
mysql> SELECT
-> A.name,A.designation,
-> B.name,B.designation
-> FROM
-> `Employee` A INNER JOIN `Employee` B ON A.SSN= B.MSSN
-> ORDER BY A.SSN,B.SSN
-> ;
+------+-------------+------+-------------+
| name | designation | name | designation |
+------+-------------+------+-------------+
| A | OWNER | B | BOSS |
| A | OWNER | F | WORKER |
| B | BOSS | C | WORKER |
| B | BOSS | E | WORKER |
| B | BOSS | G | WORKER |
+------+-------------+------+-------------+
5 rows in set (0.00 sec)
mysql>Context
StackExchange Database Administrators Q#29047, answer score: 2
Revisions (0)
No revisions yet.