Grijesh Chauhan
Grijesh Chauhan

Reputation: 58261

Constraint in MYSQL table?

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?

Upvotes: 0

Views: 266

Answers (1)

Rachcha
Rachcha

Reputation: 8816

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.

Upvotes: 1

Related Questions