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

  • SEARCH
  • Home
  • 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 8021825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:06:08+00:00 2026-06-04T22:06:08+00:00

I have three mysql tables. Tables are already created. Requests – request_id, request_message, user_id

  • 0

I have three mysql tables. Tables are already created.

Requests -  request_id, request_message, user_id
Responses - response_id, response_message, user_id
users -     user_id, user_name

Now i want to define foreign key constraints on that, such that

1. If user_id is not present in Users table, and someone is inserting the data in Requests or Responses for that user_id -- then error
2. If request_id is not present in Requests table, then if someone is inserting in responses table for that request_id -- then error 

3. If someone deletes an user_id, all associated requests and responses with that user_id should be deleted automatically.
4. If someone deletes an request_id, all the associated responses with it, should be deleted automatically.    

If i am missing any thing please let me know.

How to achieve this functionality?

Thanks

Devesh

  • 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-04T22:06:10+00:00Added an answer on June 4, 2026 at 10:06 pm

    Here is full sql to create your tables:

    CREATE TABLE IF NOT EXISTS `reponses` (
      `response_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `response_message` varchar(45) DEFAULT NULL,
      `user_id` int(10) unsigned NOT NULL,
      PRIMARY KEY (`response_id`,`user_id`),
      KEY `fk_reponses_users1` (`user_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=3 ;
    
    
    CREATE TABLE IF NOT EXISTS `requests` (
      `request_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `request_message` varchar(45) DEFAULT NULL,
      `user_id` int(10) unsigned NOT NULL,
      PRIMARY KEY (`request_id`,`user_id`),
      KEY `fk_requests_users` (`user_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=3 ;
    
    
    CREATE TABLE IF NOT EXISTS `users` (
      `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `user_name` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`user_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=3 ;
    
    
    ALTER TABLE `reponses`
      ADD CONSTRAINT `reponses_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE NO ACTION;
    
    ALTER TABLE `requests`
      ADD CONSTRAINT `requests_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE NO ACTION;
    

    Option that allows you to delete records related to user is ON DELETE CASCADE. By default MySql sets NO ACTION which refers to RESTRICT and doesn’t allow parent record to be deleted while it has related objects. I think that you didn’t mention the relation between responses and requests but you should get the idea ;).

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

Sidebar

Related Questions

I have three MYSQL tables, simplified, with following columns: ModuleEntries (MDE_ID) ModuleFields (MDF_ID, MDF_Label)
I have three mysql tables items =========== id title items_in_categories ============================ id item_id category_id
I have three MySQL InnoDB tables: Debtors Companies Private individuals Now I would like
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I have a MySQL query that: gets data from three tables linked by unique
I have a mysql table with three columns: username, location, timestamp. This is basically
I have multiple columns in a mySQL table. Three of the columns are named
I have a MySQL database set-up in a hierarchy style. There are 4 tables
I have a MySQL database which contains a table of users. The primary key

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.