In the past, system permissions or status were expressed by different fields or a permission string to indicate combined permissions. A few days ago, when developers of the tax department developed a system together, it is found that they use an int field to represent multiple states, and use Sybase to check by location. So I came back and used SQL Server2000 for a test. This method is also supported on SQL Server. (The younger brother has little knowledge before. Don't throw eggs if the prawns are used more often)
First, we design a Status field rule in bits.
Location |
4 |
3 |
2 |
1 |
Status |
Disable |
Display |
Important |
Retained |
Note:
Position: it is the serial number of the bits in a field from right to left.
Status: each bit indicates the permission. 1 indicates the permission. 0 indicates that the permission is not granted.
Reserved bits are used for other purposes and are not used in this example.
Sample Database Design
Field name |
Field Type |
Default Value |
AppObject |
Varchar (64) |
|
Status |
Int |
15 |
The default value of Status permission is 15, indicating that all four of the Status fields are 1.
Data Table Sample Data
App1 |
15 |
App2 |
15 |
App3 |
7 |
App4 |
7 |
Use SQL statements to retrieve objects in the corresponding State
1. All enabled and enabled objects
Select * from AppObjects Where Status & 12 = 12
2. All enabled objects, including and not displayed objects
Select * from AppObjects Where Status & 8 = 8
3. All enabled, important, but not displayed objects
Select * from AppObjects Where Status & 10 = 10
Other combinations are based on principles and so on. Some may not be clear, but I just wrote it to let beginners know that there is such an implementation method.