主要是想咨询权限这部分。比如有三个部门A、B、C。四个模块财务、采购、生产、管理。
如何实现权限的自由分配,如A可以拥有财务、采购、生产、管理的权限,B拥有采购、生产,C拥有财务的权限。
数据库应该怎么建?

解决方案 »

  1.   

    用户表
    权限表 
    职位表
    用户职位关系表
    职位权限关系表--字段主外键关系
    --你看看OK不
    if exists (select 1
       from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
       where r.fkeyid = object_id('用户职位关系表') and o.name = 'FK_用户职位关系表_REFERENCE_用户表')
    alter table 用户职位关系表
       drop constraint FK_用户职位关系表_REFERENCE_用户表
    goif exists (select 1
       from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
       where r.fkeyid = object_id('用户职位关系表') and o.name = 'FK_用户职位关系表_REFERENCE_职位表')
    alter table 用户职位关系表
       drop constraint FK_用户职位关系表_REFERENCE_职位表
    goif exists (select 1
       from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
       where r.fkeyid = object_id('职位权限关系表') and o.name = 'FK_职位权限关系表_REFERENCE_职位表')
    alter table 职位权限关系表
       drop constraint FK_职位权限关系表_REFERENCE_职位表
    goif exists (select 1
       from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
       where r.fkeyid = object_id('职位权限关系表') and o.name = 'FK_职位权限关系表_REFERENCE_权限表')
    alter table 职位权限关系表
       drop constraint FK_职位权限关系表_REFERENCE_权限表
    goif exists (select 1
                from  sysobjects
               where  id = object_id('权限表')
                and   type = 'U')
       drop table 权限表
    goif exists (select 1
                from  sysobjects
               where  id = object_id('用户职位关系表')
                and   type = 'U')
       drop table 用户职位关系表
    goif exists (select 1
                from  sysobjects
               where  id = object_id('用户表')
                and   type = 'U')
       drop table 用户表
    goif exists (select 1
                from  sysobjects
               where  id = object_id('职位权限关系表')
                and   type = 'U')
       drop table 职位权限关系表
    goif exists (select 1
                from  sysobjects
               where  id = object_id('职位表')
                and   type = 'U')
       drop table 职位表
    go/*==============================================================*/
    /* Table: 权限表                                                   */
    /*==============================================================*/
    create table 权限表 (
       权限ID                 int                  not null,
       角色                   numeric(30)          null,
       constraint PK_权限表 primary key (权限ID)
    )
    go/*==============================================================*/
    /* Table: 用户职位关系表                                               */
    /*==============================================================*/
    create table 用户职位关系表 (
       用户ID                 int                  null,
       职位ID                 int                  null
    )
    go/*==============================================================*/
    /* Table: 用户表                                                   */
    /*==============================================================*/
    create table 用户表 (
       用户ID                 int                  not null,
       用户名                  numeric(10)          null,
       密码                   numeric(30)          null,
       constraint PK_用户表 primary key (用户ID)
    )
    go/*==============================================================*/
    /* Table: 职位权限关系表                                               */
    /*==============================================================*/
    create table 职位权限关系表 (
       权限ID                 int                  null,
       职位ID                 int                  null
    )
    go/*==============================================================*/
    /* Table: 职位表                                                   */
    /*==============================================================*/
    create table 职位表 (
       职位ID                 int                  not null,
       职位名称                 nvarchar(30)         null,
       constraint PK_职位表 primary key (职位ID)
    )
    goalter table 用户职位关系表
       add constraint FK_用户职位关系表_REFERENCE_用户表 foreign key (用户ID)
          references 用户表 (用户ID)
    goalter table 用户职位关系表
       add constraint FK_用户职位关系表_REFERENCE_职位表 foreign key (职位ID)
          references 职位表 (职位ID)
    goalter table 职位权限关系表
       add constraint FK_职位权限关系表_REFERENCE_职位表 foreign key (职位ID)
          references 职位表 (职位ID)
    goalter table 职位权限关系表
       add constraint FK_职位权限关系表_REFERENCE_权限表 foreign key (权限ID)
          references 权限表 (权限ID)
    go