Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:15:27+00:00 2026-06-15T05:15:27+00:00

I have a table named Groups with primary key = Pkey . In Group

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:15:28+00:00Added an answer on June 15, 2026 at 5:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of groups: id -- a unique ID (primary key) group
I have three tables: SN | Table Name | Primary Key | Foreign Key
I have the following two table: -Groups- Id Name -Members- Id GroupId (Group.Id is
I have a datatable named testable, it has two columns id(primary key, int) and
I have the following two tables in my mysql database. Table Name: groups id
I have an entity mapped as following: @Entity @Table(name = Groups) @IdClass(value = GroupId.class)
I have a table like this: name code group john 12 smith 15 how
I have a table that includes some student group name, lesson time, day names
Lets say I have table named Place with columns: placeId int not null auto_increment,
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.