create database spring_security;
if exists(select * from sysobjects where name='users')
drop table users;
if exists(select * from sysobjects where name='authorities')
drop table authorities;
if exists(select * from sysobjects where name='roles')
drop table roles;
if exists(select * from sysobjects where name='resources')
drop table resources;
if exists(select * from sysobjects where name='users_roles')
drop table users_roles;
if exists(select * from sysobjects where name='roles_authorities')
drop table roles_authorities;
if exists(select * from sysobjects where name='authorities_resources')
drop table authorities_resources;
--建议MSSQL最好user_name改为username或者[user_name]由于经本人测试过在添加
--users_roles和users两个表约束时,报没候选主外键异常..user_id可能是关键
--字,所以改为userid(MYSQL ORACLE没测试过.)
--用户表
create table users(
userid varchar(32)primary key,
user_account varchar(30),
user_name varchar(40),
user_password varchar(100),
enabled int,
issys int
);
--权限表
create table authorities(
authority_id varchar(32) primary key,
authority_name varchar(40),
authority_desc varchar(100),
enabled int,
issys int,
);
--角色表
create table roles(
role_id varchar(32)primarykey,
role_name varchar(40),
role_desc varchar(100),
enabled int,
issys int
);
--资源表
create table resources(
resource_id varchar(32) primary key,
resource_desc varchar(100),
resource_type varchar(40),
resource_string varchar(200),
priority int,
enabled int,
issys int
);
--用户角色表
create table users_roles(
id numeric(12,0) identity not null primary key,
userid varchar(32),
role_id varchar(32),
enabled int
);
--角色权限表
create table roles_authorities(
id numeric(12,0)identity not null,
role_id varchar(32),
authority_id varchar(32),
enabled int
);
--权限资源表
create table authorities_resources(
id numeric(12,0)identity not null,
authority_id varchar(32),
resource_id varchar(32),
enabled int
);
----------------------------------主外键约束-----------------------------------
alter table users_roles add constraint fk_users_roles_users
foreign key(userid) references users(userid);
alter table users_roles add constraint fk_users_roles_roles
foreign key(role_id) references roles(role_id);
alter table roles_authorities add constraint pk_roles_authorities primary key(id);
alter table roles_authorities add constraint fk_roles_authorities_authorities
foreign key (authority_id) references authorities(authority_id);
alter table roles_authorities add constraint fk_roles_roles_authorities
foreign key(role_id) references roles(role_id);
alter table authorities_resources add constraint pk_authorities_resources primary key(id);
alter table authorities_resources add constraint fk_authorities_resources_authorities
foreign key(authority_id) references authorities(authority_id);
alter table authorities_resources add constraint fk_authorities_resources_resources
foreign key(resource_id) references resources(resource_id);