• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

MySQL Command: ON DELETE

December 20, 2023

MySQL Command: ON DELETE

MySQL is a popular open-source relational database management system that provides a powerful set of commands to manage and manipulate data. One such command is ON DELETE, which allows you to define the action to be taken when a record in a table is deleted.

Understanding ON DELETE

When you create a table in MySQL, you can define relationships between tables using foreign keys. A foreign key is a field or a set of fields in one table that refers to the primary key in another table. This relationship ensures data integrity and consistency.

The ON DELETE command is used to specify the action to be taken when a record in the referenced table is deleted. There are several options available:

  • RESTRICT: This is the default option. It prevents the deletion of a record in the referenced table if there are any related records in the current table.
  • CASCADE: This option automatically deletes all the related records in the current table when a record in the referenced table is deleted.
  • SET NULL: This option sets the foreign key value to NULL in the current table when a record in the referenced table is deleted.
  • SET DEFAULT: This option sets the foreign key value to its default value in the current table when a record in the referenced table is deleted.
  • NO ACTION: This option is similar to RESTRICT and prevents the deletion of a record in the referenced table if there are any related records in the current table.

Examples

Let’s consider an example to understand how the ON DELETE command works. Suppose we have two tables: orders and customers. The orders table has a foreign key customer_id that references the primary key id in the customers table.

If we define the ON DELETE command as CASCADE for the customer_id foreign key in the orders table, deleting a record in the customers table will automatically delete all the related records in the orders table.

CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);

CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT,
  order_date DATE,
  FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE
);

In the above example, if we delete a customer from the customers table, all the orders associated with that customer will be deleted from the orders table.

Conclusion

The ON DELETE command in MySQL allows you to define the action to be taken when a record in a referenced table is deleted. It provides options like RESTRICT, CASCADE, SET NULL, SET DEFAULT, and NO ACTION to handle the deletion of related records. By using the appropriate ON DELETE command, you can ensure data integrity and maintain consistency in your database.

Summary

In summary, the ON DELETE command in MySQL is used to specify the action to be taken when a record in a referenced table is deleted. It provides options like RESTRICT, CASCADE, SET NULL, SET DEFAULT, and NO ACTION. By using the appropriate ON DELETE command, you can ensure data integrity and maintain consistency in your database. To learn more about MySQL and its features, consider exploring Server.HK, a leading VPS hosting company that offers reliable and efficient hosting solutions.

Recent Posts

  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?
  • Why No-ICP-Filing Hong Kong Hosting Is the Smart Choice for Cross-Border E-Commerce
  • Hong Kong VPS vs AWS Hong Kong Region: Cost, Latency, and Control Compared

Recent Comments

  1. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  2. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026
  3. levitra price on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  4. finasterid on Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?
  5. doxycycline hyclate 100mg on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot