快速建立用户角色权限sql语句

1、建立数据库

use master
go

--创建数据库
create database CheShi001
go

 

2、建立表

--使用数据库
use CheShi001
go
 

--
创建用户表 create table T_Users( --自动增长并设置主键 Id bigint primary key identity(1,1) not null, --用户名字 Name nvarchar(50) not null, --手机号 PhoneNum nvarchar(20) not null, --邮件 Email nvarchar(30) not null, --密码盐 PasswordSalt nvarchar(20) not null, --密码哈希 PasswordHash nvarchar(100) not null, --登录错误的次数 LoginErrorCount int not null default(0), --最后登录的时间 LastLoginErrorDateTime datetime null, --创建日期 CreateDateTime DateTime not null, --是否逻辑删除 IsDeleted bit not null, ) go --创建角色表 create table T_Roles( --自动增长并设置主键 Id bigint primary key identity(1,1) not null, --角色名字 Name nvarchar(50) not null, --创建日期 CreateDateTime DateTime not null, --是否逻辑删除 IsDeleted bit not null, ) go --创建权限表 create table T_Permissions( --自动增长并设置主键 Id bigint primary key identity(1,1) not null, --权限名字 Name nvarchar(50) not null, --权限描述 Description nvarchar(1024) not null, --创建日期 CreateDateTime DateTime not null, --是否逻辑删除 IsDeleted bit not null, ) go --创建角色对应权限表 一对多 create table T_RolePermissions ( --外键角色Id RoleId bigint not null, --外键权限Id PermissionId bigint not null , --添加RoleId和PermissionId的组合主键 constraint T_RolePermissions_PK_RolePermissions Primary key nonclustered(RoleId,PermissionId), constraint T_RolePermissions_FK_RoleId foreign key(RoleId) references T_Roles(Id), constraint T_RolePermissions_FK_PermissionId foreign key(PermissionId) references T_Permissions(Id) ) go --创建角色对应权限表 一对多 create table T_UserRoles ( --外键用户Id UserId bigint not null , --外键角色Id RoleId bigint not null , --添加UserId和RoleId的组合主键 constraint T_UserRoles_PK_UserRoles Primary key nonclustered(UserId,RoleId), constraint T_UserRoles_FK_UserId foreign key(UserId) references T_Users(Id), constraint T_UserRoles_FK_RoleId foreign key(RoleId) references T_Roles(Id) ) go

 

 

3、插入数据

posted on 2019-06-19 14:27  520bug  阅读(313)  评论(0)    收藏  举报

导航