MySQL combat: how to design a database structure for a commonly used background management system

Part of the data source: ChatGPT  

What is a background management system?

        The background management system refers to the background interface system used to manage and maintain websites or applications. It usually includes functions such as user management, authority management, and data management, and can manage websites or applications conveniently and quickly. Common background management systems include Cms, OA, etc. Next, we will introduce how to use MySQL to design a common background management system.

How to use MySQL to design a common background management system?

1. Database design

We first need to design an appropriate database structure and create different data tables to store data.

After installing MySQL and Navicat database tools, we can log in to MySQL and choose to create a database:

CREATE DATABASE management_system;

In the newly created database, we can create multiple data tables such as user table (user), role table (role), permission table (permission), user role association table (user_role), role permission association table (role_permission).

  • User table (user): stores all user information, including user ID, username, password, etc.
  • Role table (role): stores all role information, including role ID, role name, etc.
  • Permission table (permission): stores all permission information, including permission ID, permission name, permission URL, etc.
  • User role association table (user_role): Stores association information between users and roles, including user ID and role ID.
  • Role permission association table (role_permission): stores the association information between roles and permissions, including role ID and permission ID.

How to create these data tables and perform related operations on these tables will be described in more detail below.

2. Database table design

[User table (user)]

        The user table (user) stores all user information, including user ID, username, password, etc. The following is the design and creation code of the user table:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT, -- 用户ID,自动增加
  `username` varchar(50) NOT NULL, -- 用户名
  `password` varchar(50) NOT NULL, -- 密码
  PRIMARY KEY (`id`), -- 设置用户ID为主键
  UNIQUE KEY `username` (`username`) -- 设置用户名唯一
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

We can insert some data into the data table for later use:

INSERT INTO `user` (`id`, `username`, `password`) VALUES
(1, 'admin', '123456'),
(2, 'user01', '123456');

The above code inserts two pieces of data into the user table. The user with id 1 is the administrator, the user name is admin, and the password is 123456; the user with id 2 is a normal user, the user name is user01, and the password is 123456.

[role table (role)]

        The role table (role) stores all role information, including role ID, role name, etc. The following is the design and creation code of the role table:

CREATE TABLE `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT, -- 角色ID,自动增加
  `name` varchar(50) NOT NULL, -- 角色名
  PRIMARY KEY (`id`), -- 设置角色ID为主键
  UNIQUE KEY `name` (`name`) -- 设置角色名唯一
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

We can insert some data into the data table for later use:

INSERT INTO `role` (`id`, `name`) VALUES
(1, 'admin'),
(2, 'user');

Two roles are inserted in the above code, the role with id 1 is the administrator, and the name is admin; the role with id 2 is the common user, and the name is user.

[Permission table (permission)]

        The permission table (permission) stores all permission information, including permission ID, permission name, permission URL, etc. The following is the design and creation code of the permission table:

CREATE TABLE `permission` (
  `id` int(11) NOT NULL AUTO_INCREMENT, -- 权限ID,自动增加
  `name` varchar(50) NOT NULL, -- 权限名称
  `url` varchar(50) NOT NULL, -- 权限URL
  PRIMARY KEY (`id`), -- 设置权限ID为主键
  UNIQUE KEY `name` (`name`) -- 设置权限名称唯一
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

We can insert some data into the data table for later use:

INSERT INTO `permission` (`id`, `name`, `url`) VALUES
(1, '查看用户', '/user/list'),
(2, '添加用户', '/user/add');

Two permissions are inserted in the above code, the permission with id 1 is to view user information, and the URL is /user/list; the permission with id 2 is to add user information, and the URL is /user/add.

[User role association table (user_role)]

        The user role association table (user_role) stores the association information between users and roles, including user ID and role ID. The following is the design and creation code of the user role association table:

CREATE TABLE `user_role` (
  `user_id` int(11) NOT NULL, -- 用户ID
  `role_id` int(11) NOT NULL, -- 角色ID
  PRIMARY KEY (`user_id`,`role_id`), -- 设置用户ID和角色ID为联合主键
  KEY `role_id` (`role_id`), -- 设置角色ID为索引
  CONSTRAINT `user_role_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), -- 设置外键规则,关联用户表
  CONSTRAINT user_role_ibfk_2 FOREIGN KEY (role_id) REFERENCES role (id) -- 设置外键规则,关联角色表
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

We can insert some data into the data table for later use:

INSERT INTO `user_role` (`user_id`, `role_id`) VALUES
(1, 1),
(1, 2),
(2, 2);

Two records are inserted in the above code, indicating that the user with id 1 is an administrator, and the user with id 2 is a normal user.

【Role permission association table (role_permission)】

        The role permission association table (role_permission) stores the association information between roles and permissions, including role ID and permission ID. The following is the design and creation code of the role permission association table:

CREATE TABLE `role_permission` (
  `role_id` int(11) NOT NULL,     -- 角色ID
  `permission_id` int(11) NOT NULL,  -- 权限ID
  PRIMARY KEY (`role_id`,`permission_id`), -- 设置角色ID和权限ID为联合主键
  KEY `permission_id` (`permission_id`),   -- 设置权限ID为索引
  CONSTRAINT `role_permission_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`), -- 设置外键规则,关联角色表
  CONSTRAINT `role_permission_ibfk_2` FOREIGN KEY (`permission_id`) REFERENCES `permission` (`id`) -- 设置外键规则,关联权限表
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

We can insert some data into the data table for later use:

INSERT INTO `role_permission` (`role_id`, `permission_id`) VALUES
(1, 1),
(1, 2),
(2, 1);

Three records are inserted in the above code, indicating that the administrator role has two permissions (view user information and add user information), and the common user role has only one permission (view user information).

3. Database table operation

        After the database and data tables are established, we need to add, delete, modify and query the tables or modify the table structure. Let's introduce them in detail below.

【Add data】

To add data, you can use the INSERT INTO statement. The specific examples are as follows:

INSERT INTO `user` (`username`, `password`) VALUES ('user02', '123456'); -- 插入一条用户数据

INSERT INTO `role` ( `name`) VALUES ('visitor');  -- 插入一条角色数据

INSERT INTO `permission` (`id`, `name`, `url`) VALUES ('修改用户', '/user/edit');  -- 插入一条权限数据

INSERT INTO `user_role` (`user_id`, `role_id`) VALUES (3, 1); -- 插入一条用户角色关联信息

INSERT INTO `role_permission` (`role_id`, `permission_id`) VALUES (3, 3);  -- 插入一条角色权限关联信息

In the above code, a piece of data is inserted into the user, role, permission, user role association and role permission association tables respectively. Note that in this article, for the sake of simplicity of demonstration, some data tables temporarily only have a small amount of data, and more data may be needed in actual applications.

【Query data】

You can use the SELECT statement to query data, you can choose to list all the data, or you can filter the data according to conditions. The following is the basic statement for querying data:

SELECT * FROM `user`; -- 查询所有用户信息
SELECT `id`, `username` FROM `user` WHERE id=1; -- 查询id为1的用户ID和用户名
SELECT `name`,`url` FROM `permission` WHERE `id` IN (1,2); -- 查询id为1和2的权限名称和URL
SELECT * FROM `user_role_permission`; -- 查询用户角色权限视图中所有记录

【update data】

You can use the UPDATE statement to update data, and you can modify the data according to conditions. Here is an example of updating data:

UPDATE `user` SET `password` = 'newpassword' WHERE `id` = 3; -- 将id为3的用户密码修改为newpassword
UPDATE `role` SET `name` = 'vip' WHERE `id` = 3; -- 将id为3的角色名称修改为vip

【delete data】

To delete data, you can use the DELETE statement to delete data according to conditions. Here is an example of deleting data:

DELETE FROM `user` WHERE `id` = 3; -- 删除id为3的用户信息
DELETE FROM `role` WHERE `id` = 3; -- 删除id为3的角色信息
DELETE FROM `permission` WHERE `id` = 3; -- 删除id为3的权限信息
DELETE FROM `user_role` WHERE `user_id` = 3; -- 删除和id为3的用户关联的角色信息
DELETE FROM `role_permission` WHERE `permission_id` = 3; -- 删除和id为3的权限关联的角色信息

【View creation】

A view is a virtual table that can be defined in terms of data in an actual table, rather than actually existing. We can perform operations such as addition, deletion, modification, and query on the view. Here is an example of creating a user role permission view:

CREATE VIEW `user_role_permission` AS
SELECT `user`.`username`, `role`.`name`, `permission`.`url` 
FROM `user`, `user_role`, `role_permission`, `role`, `permission` 
WHERE `user`.`id` = `user_role`.`user_id` AND `user_role`.`role_id` = `role_permission`.`role_id` AND `role_permission`.`permission_id` = `permission`.`id` AND `role`.`id` = `role_permission`.`role_id`;

In the above code, we created a user role permission view to display information about all users, roles and permissions. This view contains usernames, role names, and permission URLs. We can use the SELECT statement to query the view, such as:

SELECT * FROM `user_role_permission`;

It can be seen that at this point we can query based on this view, and the actual query is the associated information of multiple tables.

【Trigger Creation】

Triggers can automatically execute a program when data is inserted, deleted, or updated in a table. Here is an example of creating a trigger that automatically updates the row count of a user table when data is inserted:

CREATE TRIGGER `user_count` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
   -- ...执行动作...
   SET @total_count = (SELECT COUNT(*) FROM `user`);
   UPDATE `system_info` SET `value` = @total_count WHERE `key` = 'user_count';
END;

-- 这段代码是用来创建一个触发器的,该触发器被命名为user_count,在每次向user表格插入一行新数据后,就会被触发执行。

In the above code, we define a trigger to automatically update the number of user rows in the system information table when data is inserted in the user table. This operation will be executed after each insert operation, counting all the rows in the user table, and updating the values ​​in the system information table. After inserting user data, we can query the value of the system information table to confirm whether it has been updated.

The final rendering

Summarize

        This article mainly introduces how to use MySQL to design a common background management system, including database design, table operation, view and trigger creation, etc. It is suitable for users who have a certain MySQL foundation to learn. Through the study of this article, you can learn how to design the database structure of a common background management system, and how to perform common table operations, such as adding, deleting, modifying, and checking. At the same time, you can also learn how to create views and triggers to optimize database operations. Hope this article helps you!

Guess you like

Origin blog.csdn.net/weixin_43263566/article/details/131119261