当前位置:Gxlcms > mysql > 关于用户角色权限的设计

关于用户角色权限的设计

时间:2021-07-01 10:21:17 帮助过:29人阅读

一直想整理一篇关于用户角色权限的设计,这几天通过网上资料和自己实际工作中运用到的资源把用户角色权限的数据库表设计出来。 基础表有3张: 角色表--用来记录角色名称 用户表--用来记录登录用户名称和登录密码 菜单表--用来记录菜单名称、子菜单名称 每个

一直想整理一篇关于用户角色权限的设计,这几天通过网上资料和自己实际工作中运用到的资源把用户角色权限的数据库表设计出来。

基础表有3张:

角色表--用来记录角色名称

用户表--用来记录登录用户名称和登录密码

菜单表--用来记录菜单名称、子菜单名称

每个用户会有多个角色、而一个角色也会有多个用户,即角色和用户的关系是n:m,多对多的关系。

每个角色会有多个菜单权限、每个菜单也会有多个角色拥有,即角色和菜单的关系也是n:m,多对多的关系。

所以还有有两张关联表:

角色用户对应表--用来记录角色和用户的关系

角色菜单对应表--用来记录角色和菜单的关系

这里是下载地址

http://download.csdn.net/detail/u010192842/6819561

下面是数据库表设计:

\

----------------------------------------------------------------
-- Author    :Yole_JZ
-- Date      :2014-01-07 7:44:00
-- Table Name:[Sys_Role]
----------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sys_Role](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [RoleName] [varchar](20) NULL,
 [DeleteFlag] [int] NOT NULL CONSTRAINT [DF_Sys_Role_DeleteFlag]  DEFAULT ((0)),
 [RoleRemark] [varchar](50) NULL,
 CONSTRAINT [PK_Sys_Role] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0:正常  1:删除' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role', @level2type=N'COLUMN', @level2name=N'DeleteFlag'

\

----------------------------------------------------------------
-- Author    :Yole_JZ
-- Date      :2014-01-07 7:45:00
-- Table Name:[Sys_Use]
----------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sys_User](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [UserName] [varchar](20) NOT NULL,
 [UserPassword] [varchar](50) NOT NULL,
 [StaffID] [int] NOT NULL,
 [DeleteFlag] [int] NOT NULL CONSTRAINT [DF_Sys_User_DeleteFlag]  DEFAULT ((0)),
 [UserRemark] [varchar](50) NULL,
 CONSTRAINT [PK_Sys_User] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'职工信息表ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_User', @level2type=N'COLUMN', @level2name=N'StaffID'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0:正常  1:删除' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_User', @level2type=N'COLUMN', @level2name=N'DeleteFlag'

\

----------------------------------------------------------------
-- Author :Yole_JZ
-- Date :2014-01-07 8:10:00
-- Table Name:[Sys_Menu]
----------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sys_Menu](
[ID] [int] NOT NULL,
[MenuPID] [int] NULL,
[MenuName] [varchar](50) NOT NULL,
[DeleteFlag] [int] NOT NULL CONSTRAINT [DF_Sys_Menu_DeleteFlag] DEFAULT ((0)),
[MenuRemark] [varchar](50) NULL,
CONSTRAINT [PK_Sys_Menu] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'菜单表ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Menu', @level2type=N'COLUMN', @level2name=N'MenuPID'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0:正常 1:删除' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Menu', @level2type=N'COLUMN', @level2name=N'DeleteFlag'


\

----------------------------------------------------------------
-- Author :Yole_JZ
-- Date :2014-01-07 8:22:00
-- Table Name:[Sys_Role_User]
----------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sys_Role_User](
[ID] [int] NOT NULL,
[RoleID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[DeleteFlag] [int] NOT NULL CONSTRAINT [DF_Sys_Role_User_DeleteFlag] DEFAULT ((0)),
[MenuRemark] [varchar](50) NULL,
CONSTRAINT [PK_Sys_Role_User] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'角色表ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_User', @level2type=N'COLUMN', @level2name=N'RoleID'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户表ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_User', @level2type=N'COLUMN', @level2name=N'UserID'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0:正常 1:删除' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_User', @level2type=N'COLUMN', @level2name=N'DeleteFlag'


\

----------------------------------------------------------------
-- Author :Yole_JZ
-- Date :2014-01-07 8:32:00
-- Table Name:[Sys_Role_Menu]
----------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sys_Role_Menu](
[ID] [int] NOT NULL,
[RoleID] [int] NOT NULL,
[MenuID] [int] NOT NULL,
[PermissionType] [int] NOT NULL CONSTRAINT [DF_Sys_Role_Menu_PermissionType] DEFAULT ((31)),
[DeleteFlag] [int] NOT NULL CONSTRAINT [DF_Sys_Role_Menu_DeleteFlag] DEFAULT ((0)),
[MenuRemark] [varchar](50) NULL,
CONSTRAINT [PK_Sys_Role_Menu] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'角色表ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_Menu', @level2type=N'COLUMN', @level2name=N'RoleID'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'菜单表ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_Menu', @level2type=N'COLUMN', @level2name=N'MenuID'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'eg.1:录入 2:浏览 4:修改 8:删除 16:执行(默认所有权限)' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_Menu', @level2type=N'COLUMN', @level2name=N'PermissionType'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0:正常 1:删除' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'Sys_Role_Menu', @level2type=N'COLUMN', @level2name=N'DeleteFlag'

PS.这里的权限类型没有单独做成一个表,是因为要是再加上权限表(增删改查)会是角色和菜单关系过于复杂,不利于后期的维护。

如果遇到同一个菜单需要有不同的权限,可以用权限类型来控制,例如:1:录入 2:浏览 4:修改 8:删除 16:执行(默认所有权限)

如果是增删改查权限,这个权限类型就是15(1+2+4+8=15),当然这样做会有弊端,例如有修改权限就会相应的有浏览权限,会出现权限包含的情况。遇到这样的情况需要判断一下即可。

人气教程排行