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 7789399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:17:12+00:00 2026-06-01T21:17:12+00:00

I’ve read similar questions but there aren’t very detailed explanations on using ON DELETE

  • 0

I’ve read similar questions but there aren’t very detailed explanations on using ON DELETE and ON UPDATE so i’m having trouble understanding that and also what are the correct procedures to update your database when you want to remove or update a record with FK constraint but not delete the records in other tables when it was referenced, but set those references to null or 0 or something in the other tables.

Background

I’m trying to create a library of functions for this database (for an e-cart system) so that the database can be managed by an admin (like a CMS).

I chose InnoDB engine to make use of foreign keys and transactions.

I’m also using PHP PDO objects rather than the standard mysql_-functions to make use of parametrized queries.

This is the SQL creation code for these tables:

# TBL_MENU_CATEGORY

DROP TABLE IF EXISTS tbl_menu_category;
CREATE TABLE tbl_menu_category(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    name VARCHAR(50)
) ENGINE = InnoDB;

# TBL_PRODUCT_CATEGORY

DROP TABLE IF EXISTS tbl_product_category;
CREATE TABLE tbl_product_category(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    name VARCHAR(50)
) ENGINE = InnoDB;


# TBL_MENU_CAT_BASKET

DROP TABLE IF EXISTS tbl_menu_cat_basket;
CREATE TABLE tbl_menu_cat_basket(
    menu_id INT,
    FOREIGN KEY(menu_id) REFERENCES tbl_menu_category(id),
    cat_id INT,
    FOREIGN KEY(cat_id) REFERENCES tbl_product_category(id)
) ENGINE = InnoDB;


# TBL_PRODUCT_CAT_BASKET

DROP TABLE IF EXISTS tbl_product_cat_basket;
CREATE TABLE tbl_product_cat_basket(
    cat_id INT,
    FOREIGN KEY(cat_id) REFERENCES tbl_product_category(id),
    product_id INT,
    FOREIGN KEY(product_id) REFERENCES tbl_product(id)
) ENGINE = InnoDB;

This is a function in the library to add a Submenu to a Menu (Menu > Submenu > Product).

function addSubmenu($menu_id, $sub_name) {
    $success = false;
    if ( dbTransaction() ) {
        $sql = "INSERT INTO tbl_product_cat(name) VALUES ('?');";
        dbQuery($sql, array($sub_name));
        $rows = dbRowsAffected();
        if ($rows == 1) {
            $cat_id = dbLastInsertId();
            $sql = "INSERT INTO tbl_menu_cat_basket(menu_id, cat_id) VALUES ('?','?');";
            dbQuery($sql, array((int)$menu_id, (int)$cat_id));
            $rows = dbRowsAffected();
            if ($rows == 1) {
                $success = true;
            }
        }
        if ($success)
            dbCommit();
            return "Add submenu successful.";
        else
            dbRollback();
            return "Add submenu failed.";
    }
}

The query is executed like this:

require_once "pdo.php"; # Creates PDO object '$db'
$query;

function dbQuery($sql, $data) {
    global $db, $query;
    $query = $db->prepare($sql);
    $query->execute($data);
}

# other PDO & PDOStatement methods like this
# (rowCount, lastInsertId, fetchAll, transaction-stuff).

Problems

I’m not even sure if this is correct. Am I following the right path or doing things wrongly?

Now I’m stuck on creating similar functions for updating a record which also have foreign keys. I know that InnoDB will throw an error that the record can’t be updated because it has references (I think?).

I need to know the correct procedure for updating records and the functions available to me for doing so.

I want to know about ON DELETE and ON UPDATE constraints but I can’t seem to find any good, newb-friendly tutorials (the MySQL docs are very unfriendly and don’t even highlight syntax).

What I want to do

When updating records I want to cascade the update so that all references are updated.

When deleting records I just want to delete the record that was deleted BUT any references to it can be reset instead (if a product category is deleted the products should NOT be deleted; if a product is deleted all orders with that product should NOT be deleted but the product value set to 0 or null)

How can I do this with ON DELETE and ON UPDATE -or- what procedures should I take to achieve what I want to do here?

Thank you for reading.

  • 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-01T21:17:14+00:00Added an answer on June 1, 2026 at 9:17 pm

    Specifying ON UPDATE CASCADE ON DELETE SET NULL in your foreign key specification should do the trick.

    For example, FOREIGN KEY(product_id) REFERENCES tbl_product(id) ON UPDATE CASCADE ON DELETE SET NULL. Though I have to wonder where do you foresee situations where changing primary key values would make sense.

    You might be interested in MySQL Workbench – it can help you design schemas visually and display corresponding DDL statements.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.