rendered paste bodymysql> create table ug1 (gid int not null, uid int not null, primary key (uid, gid));
Query OK, 0 rows affected (0.01 sec)
mysql> create table ug2 (gid int not null, uid int not null, foreign key (uid) references ug1(uid) on update cascade on delete restrict);
Query OK, 0 rows affected (0.01 sec)
...
mysql> select * from ug2;
+-----+-----+
| gid | uid |
+-----+-----+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
+-----+-----+
6 rows in set (0.00 sec)
mysql> delete from ug2 where uid = 1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from ug2;
+-----+-----+
| gid | uid |
+-----+-----+
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
+-----+-----+
5 rows in set (0.00 sec)
mysql> insert ug1 values (4,4);
Query OK, 1 row affected (0.00 sec)
mysql> insert ug1 values (4,4);
ERROR 1062 (23000): Duplicate entry '4-4' for key 1
mysql> insert ug1 values (3,3);
Query OK, 1 row affected (0.00 sec)
mysql> delete from ug2 where uid = 3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from ug2;
+-----+-----+
| gid | uid |
+-----+-----+
| 2 | 2 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
+-----+-----+
4 rows in set (0.00 sec)
mysql> select * from ug1;
+-----+-----+
| gid | uid |
+-----+-----+
| 3 | 3 |
| 4 | 4 |
| 6 | 6 |
+-----+-----+
3 rows in set (0.00 sec)