0

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:

  1. New inserted row can not have NULL value for Parent_group column
  2. 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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208

1 Answers1

1

There are a few things about the two constraints you wish to impose:

  1. New inserted row can not have NULL value for Parent_group column.

    • You can impose a NOT NULL constraint on a column only if it contains all non-null values. You need a null value in this column for the root node.
    • For this, you can use the CHECK constraint. Read more about the CHECK CONSTRAINT here.
    • You can put

CHECK ((peky= AND parent_group IS NULL)
OR
(peky!= AND parent_group IS NOT NULL))

This will allow a NULL value only for the root node and will enforce a NOT NULL value for every other row in the table.

  1. Add a constrain so that RootGroup row can't be deleted.

    • That you have already defined a foreign key between parent_group and pkey, the database will automatically enforce referential integrity and forbid the root node (or for that matter any parent node) from being deleted. The database will return an error if a DELETE is attempted on any parent or root node.
  2. For the point mentioned in the EDIT section, you can put a simple check constraint on the table like
    CHECK (parent_group != pkey). This should do the job for you.

Read about how to define foreign key constraints and how to use them to enforce referential integrity. Also, go through the link I have posted above or here before you apply these suggestions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rachcha
  • 8,486
  • 8
  • 48
  • 70
  • The CHECK clause is parsed but ignored by all storage engine in MySQL . [The bug is reported] (http://bugs.mysql.com/bug.php?id=25660) – Grijesh Chauhan Nov 27 '12 at 04:43
  • [Learn here too] (http://dba.stackexchange.com/questions/29170/restriction-on-self-referencing-on-insert-in-mysql) – Grijesh Chauhan Nov 27 '12 at 04:47
  • Can you create a trigger on `INSERT` and `UPDATE` of these rows? You can read about triggers [here](http://dev.mysql.com/doc/refman/5.0/en/triggers.html). A trigger is always handy for advanced auditing of the database. – Rachcha Nov 27 '12 at 04:48
  • Excuse me, but it's Rachcha, not Rachana. I'm a guy. Good that you have found the answer. That was helpful for me too. – Rachcha Nov 27 '12 at 04:51
  • 1
    Voting up for your interest! Thanks! – Grijesh Chauhan Nov 27 '12 at 04:55
  • accepting as this is the only answer and correct for other DBMS. Thanks Rachcha! – Grijesh Chauhan Aug 09 '13 at 11:57