Home  >  Article  >  Database  >  MySQL creates a navigation menu table to implement navigation menu management functions

MySQL creates a navigation menu table to implement navigation menu management functions

WBOY
WBOYOriginal
2023-07-02 08:30:061281browse

MySQL creates a navigation menu table to implement navigation menu management functions

The navigation menu is one of the common functions in website development. The navigation menu can facilitate users to browse various pages of the website. In the MySQL database, we can implement the navigation menu management function by creating a navigation menu table. This article will introduce how to create a navigation menu table and demonstrate how to add, delete and modify the navigation menu through code examples.

  1. Create navigation menu table

First, we need to create a navigation menu table to store navigation menu information. The following is an example of a SQL statement to create a navigation menu table:

CREATE TABLE `navigation_menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `url` varchar(255) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `parent_id` (`parent_id`),
  CONSTRAINT `navigation_menu_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `navigation_menu` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

The above SQL statement creates a table named navigation_menu, which contains the following fields:

  • id: The unique identifier of the navigation menu item, generated by self-increment;
  • name: The name of the navigation menu item;
  • url: The link address of the navigation menu item;
  • parent_id: The identifier of the parent menu item of the navigation menu item, used to implement the multi-level structure of the navigation menu.
  1. Add navigation menu items

To add navigation menu items in the navigation menu table, you can use the following INSERT statement example:

INSERT INTO `navigation_menu` (`name`, `url`, `parent_id`)
VALUES ('首页', '/home', NULL);

Above The INSERT statement adds a navigation menu item named "Home" to the navigation menu table. The link address is "/home" and the parent menu item is empty.

  1. Delete navigation menu items

To delete navigation menu items in the navigation menu table, you can use the following DELETE statement example:

DELETE FROM `navigation_menu` WHERE `id` = 1;

The above DELETE statement The navigation menu item with id in the navigation menu table will be deleted.

  1. Modify navigation menu items

To modify the navigation menu items in the navigation menu table, you can use the following UPDATE statement example:

UPDATE `navigation_menu` SET `name` = '关于我们', `url` = '/about' WHERE `id` = 2;

UPDATE above The statement changes the name of the navigation menu item with id in the navigation menu table to "About Us" and the link address to "/about".

Through the above code examples, we can implement basic management functions of the navigation menu, including adding, deleting and modifying navigation menu items. In the actual development process, we can also expand the fields of the navigation menu table according to needs to meet more complex navigation menu requirements.

Summary:

This article introduces how to create a navigation menu table through MySQL to implement the management function of the navigation menu. We created a navigation menu table containing the id, name, url and parent_id fields, and demonstrated how to add, delete and modify the navigation menu through code examples. I hope this article can help you understand and implement the navigation menu management function.

The above is the detailed content of MySQL creates a navigation menu table to implement navigation menu management functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn