MySQL Set 3 right discrete

Source: Internet
Author: User

The rights module in the background of the website is very common, so it is necessary to master 3 right separation.

3 The right discrete table has the user table, the role table, the Permissions table, the corresponding relationship is as follows

Users have "reader", "author" and "Administrator" roles, roles have different permissions, such as publishing articles, reviewing articles, viewing articles, etc.

We set the user and role here to be a one-to-many relationship where a user has only one role;

The relationship between roles and permissions is a many-to-many relationship. That is, a role has multiple permissions, and similarly, a permission can be assigned to different roles.

The corresponding Database build table statements are as follows:

User table:

Create tablecreate TABLE ' orm_user ' (  ' i_id ' int (one) not NULL auto_increment,  ' user_name ' varchar "not null,
    ' user_pass ' varchar (not NULL,  PRIMARY KEY (' i_id ')) Engine=innodb auto_increment=7 DEFAULT Charset=utf8

  

Role table

Create tablecreate table ' orm_role ' (  ' i_id ' int (one) not NULL auto_increment COMMENT ' permission table ',  ' s_name ' varchar () N OT null,  ' s_description ' varchar (default NULL,  PRIMARY KEY (' i_id ')) Engine=innodb auto_increment=4 Default Charset=utf8

  

Permission table

Create tablecreate TABLE ' orm_permission ' (  ' i_id ' int (one) not null auto_increment,  ' s_name ' varchar (a) NOT NULL ,  ' s_description ' varchar default NULL,  PRIMARY KEY (' i_id ') engine=innodb auto_increment=13 default Charset=utf8

  

User Role Table

Create tablecreate TABLE ' orm_user_role ' (  ' i_id ' int (one) not NULL auto_increment,  ' i_user_id ' int (one) not null,< c10/> ' i_role_id ' int (one) not NULL,  PRIMARY KEY (' i_id ')) Engine=innodb auto_increment=7 DEFAULT Charset=utf8

  

Role Permission Table

Create tablecreate TABLE ' orm_role_permission ' (  ' i_id ' int (one) not NULL auto_increment,  ' i_role_id ' int (one) not NULL,  ' i_permission_id ' int (one) not NULL,  PRIMARY KEY (' i_id ')) Engine=innodb auto_increment=26 DEFAULT CHARSET =utf8

  

Inserting related data

Then use the PHP operation database to query the relevant data

MySQL Set 3 right discrete

Related Article

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.