I have a table named Groups with primary key = Pkey
. In Group there is a recursive association Parent_group references Pkey . I defined a Parent_Group as foreign key in relation Groups. I am using MYSQL.
Table bame: Groups
+------+-----------+------------+----------+---------------+
| PKey | GroupName | Region | Role | Parent_Group |
+------+-----------+------------+----------+---------------+
| k1 | RootGroup | Jaipur | Admin | NULL |
+------+-----------+------------+----------+---------------+
| k2 | G2 | Alwar | Admin | k1 |
+------+-----------+------------+----------+---------------+
| k3 | G3 | Jaipur | Guest | k3 |
+------+-----------+------------+----------+---------------+
| k4 | G4 | Alwar | Operator| k2 |
+------+-----------+------------+----------+---------------+
Query for creating table:
CREATE TABLE IF NOT EXISTS `groups`
(
`PKey` varchar(64) NOT NULL,
`group_name` varchar(64) DEFAULT NULL,
`Region` varchar(128) NOT NULL,
`Role` varchar(128) NOT NULL,
`parent_group` varchar(64) DEFAULT NULL,
PRIMARY KEY (`Pkey`),
KEY `FK_ParentGroup_ChildGroup` (`parent_group`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The Groups
table will be shipped with my application with only one RootGroup
tuple.
And I want to impose two constraints on table?
as follows:
- New inserted row can not have NULL value for
Parent_group
column - Add a constrain so that
RootGroup
row can't be deleted.
I wants to know, Weather it is possible within SQL (if yes
how?) or I have to handle in back-end systems?
Can be use trigger?
EDIT: I wants to impose an extra constraint on table So that a new inserting tuple can not point to itself. e.g.
mysql> INSERT INTO Employee VALUES ("8", "H", "BOSS", "8");
Query OK, 1 row affected (0.04 sec)
Should be fail?