How to implement a dynamic MySQL View

Source: Internet
Author: User
Tags mysql view

The implementation method of MySQL dynamic view should be all we need to know. The following describes how to implement MySQL dynamic view, hoping to help you.

The user is in a C/S structure. Each user can view different data based on different roles. The system generates a data content based on a specific Identifier and then accesses it through a unified view.

Requirements:

The user is in a C/S structure. Each user can view different data based on different roles. The system generates a data content based on a specific Identifier and then accesses it through a unified MySQL dynamic view.

Required. The MySQL dynamic view cannot be modified, or a layer of query cannot be nested outside the attempt.

Design:

The system generates a unique ID (either the application or the uuid of the database) in some way, and then tries to associate it with this id.

Code:
 

 
 
  1. Drop table if exists test;
  2. Create table test (
  3. Id int not null,
  4. Name varchar (20) not null
  5. );
  6. Insert into test values (1, 'test1 ');
  7. Insert into test values (1, 'test11 ');
  8. Insert into test values (1, 'test111 ');
  9. Insert into test values (2, 'test2 ');
  10. Insert into test values (2, 'test22 ');
  11. Drop function if exists getSpid;
  12. Delimiter |
  13. CREATE function getSpid ()
  14. RETURNS int
  15. RETURN @ spid;
  16. |
  17. Delimiter;
  18. Drop view if exists v_test;
  19. Create view v_test
  20. Select * from test where id = getSpid ();
  21. -- Test code
  22. -- Enable session 1
  23. Set @ spid = 1;
  24. Select * from v_test;
  25. -- Enable session 2
  26. Set @ spid = 2;
  27. Select * from v_test;

Note:

Maintain the generated ID in the session variable.

Create a UDF and return this variable.

Finally, call this function in the MySQL dynamic view.

Back up and restore a database using MySQL Command Line

How to delete duplicate records in MySQL

About MySQL query Cache Mechanism

Use MySQL temporary tables to accelerate queries

How to properly use MySQL Indexes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.