解析大型.NET ERP系统 20条数据库设计规范

数据库设计规范是个技术含量相对低的话题,只需要对标准和规范的坚持即可做到。当系统越来越庞大,严格控制数据库的设计人员,并且有一份规范书供执行参考。在程序框架中,也有一份强制性的约定,当不遵守规范时报错误。

以下20个条款是我从一个超过1000个数据库表的大型ERP系统中提炼出来的设计约定,供参考。

 

1  所有的表的第一个字段是记录编号Recnum,用于数据维护
[Recnum] [decimal] (8, 0) NOT NULL IDENTITY(1, 1)
 

在进行数据维护的时候,我们可以直接这样写:

UPDATE Company SET Code='FLEX'  WHERE Recnum=23
 
2 每个表增加4个必备字段,用于记录该笔数据的创建时间,创建人,最后修改人,最后修改时间
[CreatedDate] [datetime] NULL,
[CreatedBy] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[RevisedDate] [datetime] NULL,
[RevisedBy] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

框架程序中会强制读取这几个字段,默认写入值。

 

3  主从表的主外键设计

主表用参考编号RefNo作为主键,从表用RefNo,EntryNo作为主键。RefNo是字符串类型,可用于单据编码功能中自动填写单据流水号,从表的EntryNo是行号,LineNo是SQL Server 的关键字,所以用EntryNo作为行号。

如果是三层表,则第三层表的主键依次是RefNo,EntryNo,DetailEntryNo,第三个主键用于自动增长行号。

 

4 设计单据状态字段
字段含义
Posted过帐,已确认
Closed已完成
Cancelled已取消
Approved已批核
Issued已发料
Finished已完成
Suspended已取消
 
5 字段含义相近,把相同的单词调成前缀。

比如工作单中的成本核算,人工成本,机器成本,能源成本,用英文表示为LaborCost,MachineCost,EnergyCost

但是为了方便规组,我们把Cost调到字段的前面,于是上面三个字段命名为CostLabor,CostMachine,CostEnergy。

可读性后者要比前者好一点,Visual Studio或SQL Prompt智能感知也可帮助提高字段输入的准确率。

 

6 单据引用键命名 SourceRefNo  SourceEntryNo

销售送货Shipment会引用到是送哪张销售单据的,可以添加如下引用键SourceRefNo,SourceEntryNo,表示送货单引用的销售单的参考编号和行号。Source开头的字段一般用于单据引用关联。

 

7 数据字典键设计

比如员工主档界面的员工性别Gender,我的方法是在源代码中用枚举定义。性别枚举定义如下:

public enum Gender
{
        [StringValue("M")]
        [DisplayText("Male")]
        Male,

        [StringValue("F")]
        [DisplayText("Female")]
        Female
}

在代码中调用枚举的通用方法,读取枚举的StringValue写入到数据库中,读取枚举的DisplayText显示在界面中。

经过这一层设计,数据库中有关字典方面的设计就规范起来了,避免了数据字典的项的增减给系统带来的问题。

 

8 数值类型字段长度设计

Price/Qty 数量/单价  6个小数位   nnnnnnnnnn.nnnnnn 格式 (10.6)
Amount 金额   2个小数位          nnnnnnnnnnnn.nn 格式(12.2)
Total Amt 总金额 2个小数位       nnnnnnnnnnnnnn.nn 格式(14.2)

参考编号默认16个字符长度,不够用的情况下增加到30个字符,再不够用增加到60个字符。这样可以保证每张单据的第一个参考编号输入控件看起来都是一样长度。

除非特别需求,一般而言,界面中控件的长度取自映射的数据库中字段的定义长度。

 

9 每个单据表头和明细各增加10个自定义字段,基础资料表增加20个自定义字段

参考供应商主档的自定义字段,自定义字段的名称统一用UserDefinedField。

ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_1] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_2] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_3] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_4] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_5] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_6] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_7] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_8] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_9] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_10] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_11] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_12] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_13] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_14] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_15] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_16] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_17] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_18] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_19] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
ALTER TABLE Vendor ADD  COLUMN [USER_DEFINED_FIELD_20] nvarchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

 

10 多货币(本位币)转换字段的设计

金额或单价默认是以日记帐中的货币为记录,当默认货币与本位币不同时需要同时记录下本位币的值。

销售单销售金额 SalesAmount或SalesAmt,本位币字段定义为SalesAmountLocal或SalesAmtLocal

通常是在原来的字段后面加Local表示本位币的值。

 

11 各种日期字段的设计
字段名称含义
TranDate日期帐日期 Tran是Transaction的简写
PostedDate过帐日期
ClosedDate完成日期
InvoiceDate开发票日期
DueDate截止日期
ScheduleDate计划日期,这个字段用在不同的单据含义不同。比如销售单是指送货日期,采购单是指收货日期。
OrderDate订单日期
PayDate付款日期
CreatedDate创建日期
RevisedDate修改日期
SettleDate付款日期
IssueDate发出日期
ReceiptDate收货日期
ExpireDate过期时间

 

12 财务有关的单据包含三个标准字段

FiscalYear 财年,PeriodNo 会计期间,Period 前面二个的组合。以国外的财年为例子,FiscalYear是2015,PeriodNo是4,Period是2015/04。

欧美会计期间是从每年的4月份开始,需要注意的是会计期间与时间没有必然的联系,看到会计期间是2015/04,不一定是表示2015的4月份,它只是说这是2015财年的第四期,具体在哪个时间段需要看会计期间定义。

 

13 单据自动生成 DirectEntry

有些单据是由其它单据生成过来的,逻辑上应该不支持编辑。比如销售送货Shipment单会产生出仓单,出仓单应该不支持编辑,只能做过帐扣减库存操作。这时需要DirectEntry标准字段来表示。当手工创建一张出仓单时,将DirectEntry设为true,表示可编辑单据中的字段值,当由其它单据传递产生过来产生的出仓单,将DirectEntry设为false,表示不能编辑此单据。这种情况还发生在业务单据产生记帐凭证(Voucher)的功能中,如果可以修改由原始单据传递过来的数量金额等字段,则会导致与源单不匹配,给系统对帐产生困扰。

 

14 百分比值字段的设计

Percentage百分比值,用于折扣率,损耗率等相关比率设定的地方。推荐用数值类型表示,用脚本表示是

[ScrapRate] [decimal] (5, 2) NULL
 

预留两位小数,整数部分支持1-999三位数。常常是整数部分2位就可以,用3位也是为了支持一些特殊行业(物料损耗率超过100)的要求。

 

15 日志表记录编号LogNo字段设计

LogNo字段的设计有些巧妙,以出仓单为例子,一张出仓单有5行物料明细,每一行物料出仓都会扣减库存,再写物料进出日记帐,因为这五行物料出仓来自同一个出仓单,于是将这五行物料的日记帐中的LogNo都设为同一个值。于在查询数据时,以这个字段分组即可看到哪些物料是在同一个时间点上出仓的,对快速查询有很重要的作用。

 

16 基础资料表增加名称,名称长写,代用名称三个字段

比如供应商Vendor表,给它加以下三个字段:

Description 供应商名称,比如微软公司。

ExtDescription 供应商名称长写,比如电气行业的南网的全名是南方国家电网有限公司。

AltDescription 供应商名称替代名称,用在报表或是其它单据引用中。比如采购单中的供应商是用微软,还是用代用名称Microsoft,由参数(是否用代用名称)控制。

 

17 文件类表增加MD5 Hash字段

比如产品数据管理系统要读取图纸,单据功能中增加的附件文件,这类涉及文件读写引用的地方,考虑存放文件的MD5哈希值。文件的MD5相当于文件的唯一识别身份,在网上下载文件时,网站常常会放出文件的MD5值,以方便对比核对。当下载到本机的文件的MD5值与网站上给出的值不一致时,有可能这个文件被第三方程序修改过,不可信任。

 

18 数据表的主键用字符串而不是数字

比如销售单中的货币字段,是存放货币表的货币字符串值RMB/HKD/USD,还是存放货币表的数字键,1/2/3。

存放前者对于报表制作相对容易,但是修改起来相对麻烦。存放后者对修改数据容易,但对报表类或查询类操作都需要增加一个左右连接来看数字代表的货币。金蝶使用的是后者,它的BOS系统也不允许数据表之间有直接的关联,而是间接通过Id值来关联表。

在我看到的系统中,只有一个会计期间功能(财年Fiscal Year)用到数字值作主键,其余的单据全部是字符串做主键。

 

19 使用约定俗成的简写

模块Module 简写

简写全名
SLSales 销售
PUPurchasing 采购
ICInventory 仓库
ARAccount Receivable 应收
APAccount Payable 应付
GLGeneral Ledger 总帐
PRProduction 生产

名称Name 简写

简写全名
UomUnit of Measure 单位
CcyCurrency 货币
AmtAmount  金额
QtyQuantity 数量
Qty PerQuantity Per 用量
Std OutputStandard Output 标准产量
ETAEstimated Time of Arrival 预定到达时间
ETDEstimated Time of Departure  预定出发时间
CODCash On Delivery 货到付款
SOSales Order 销售单
POPurchase Order 采购单

 

20  库存单据数量状态

Qty On Hand 在手量

Qty Available 可用量

Qty On Inspect 在验数量

Qty On Commited 提交数量

Qty Reserved 预留数量

以上每个字段都有标准和行业约定的含义,不可随意修改取数方法。

转载于:https://www.cnblogs.com/JamesLi2015/p/4669308.html

--------语法 --建立视图 --if exists(select * from sysobjects where name='视图名') -- drop view 视图名 --go --create view 视图名 --as --select 字段名 from 表名 [件] --go --主外健约束语句没有执行 use T90ERP go --***********人力资源 --部门表:Depet if exists(select * from sysobjects where name='Depet') drop table Depet go create table Depet ( dept_id Int primary key identity(1,1) not null, --部门编号 主键,自增 dept_name Varchar(20) not null --部门名称 ) ----约束 --alter table Depet add constraint UQ_dept_name unique (dept_name) go --职位表:Post if exists(select * from sysobjects where name='Post') drop table Post go create table Post ( Post_id Int primary key identity(1,1) not null, --职位编号 主键 自增 Post_name Varchar(50) not null, --职位名称 唯一 Post_money Money not null, --职位工资 Dept_id int not null --部门编号 外 Int 级联删除 ) ----约束 --alter table post add constraint UQ_post_name unique (post_name) alter table post add constraint FK_post_deptId foreign key(post_deptId) references depet(dept_id) go --员工信息表:Employee if exists(select * from sysobjects where name='Employee') drop table Employee go create table Employee ( Emp_id Int primary key identity(1,1) not null, --员工信息编号 主键,自增 Emp_number Varchar(50) not null, --员工工号 唯一 Emp_postId Int not null, --职位编号 外键 级联删除 Emp_hire Datetime not null, --录用时间 Emp_state Bit not null, --状态 默认1 (1在职/0离职) ) ----约束 --alter table Employee add constraint UQ_emp_number unique (emp_number) alter table Employee add constraint FK_emp_postId foreign key (emp_postId) references post(post_id) --alter table Employee add constraint DF_emp_state default(1) for emp_state go --简历表:Resume if exists(select * from sysobjects where name='Resume') drop table Resume go create table Resume ( Res_id Int primary key identity(1,1) not null, --职员信息ID 主键 非空自增 Int Emp_id Int not null, --职员ID 外键 Res_name Varchar(50) not null, --真实姓名 Res_englishname Varchar(50) null, --英文名 空 Res_idcard Varchar(19) not null, --身份证号 唯一索引 只有18位数字或18位数字加X Res_sex bit not null, --性别 默认1 男 只有男和女两种 Res_bornDate datetime not null, --出生年月 Res_nativeplace varchar(50) not null, --籍贯 Res_nation Varchar(50) not null, --民族 默认汉族 Res_health text null, --健康状况 空 默认健康 Res_diploma Varchar(50) not null, --学历 Res_address Varchar(50) null, --联系地址 空,默认地址不详 Res_Tel Varchar(50) not null, --电话 只能是11为数字 Res_photo image null --照片 空 ) ----约束 alter table Resume add constraint FK_res_empid foreign key (res_empid) references Employee(emp_id) --alter table Resume add constraint UQ_res_idcard unique (res_idcard) --alter table Resume add constraint CK_res_idcard check (res_idcard like '[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9] --[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9]' or res_idcard like '[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9] --[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9]X') --alter table Resume add constraint DF_res_sex default (1) for res_sex --alter table Resume add constraint CK_res_sex check(res_sex=1 or res_sex=0) --alter table Resume add constraint DF_res_nation default ('汉族') for res_nation --alter table Resume add constraint DF_res_health default ('健康') for res_health --alter table Resume add constraint DF_res_health defatult(1) for res_health --alter table Resume add constraint DF_res_address default ('地址不详') for res_address --alter table Resume add constraint CK_res_tel check(len(res_tel)=11) go --考勤类型表:CheckType if exists(select * from sysobjects where name='CheckType') drop table CheckType create table CheckType ( Checkt_id int primary key identity(1,1) not null, --考勤类型 主键 自增 Int Checkt_name varchar(50) not null --考勤名称(干什么) Varchar(50) ) go --考勤表:Check if exists(select * from sysobjects where name='CheckInfo') drop table CheckInfo create table CheckInfo ( Check_id int primary key identity(1,1) not null, --考勤id 主键 自增 Int Emp_id int not null, --员工id 外键 Int Check_hire datetime not null, --考勤开始时间 开始时间必须 Datetime Check_end datetime not null, --考勤结束时间 要在结束时间之前 Datatime Checkt_id int not null, --考勤类型 外键 Int Check_gtime int not null, --工休天数 Int check_time int not null --扣薪天数 Int ) ----约束 alter table CheckInfo add constraint FK_check_empId foreign key (check_empId) references Employee(emp_id) alter table CheckInfo add constraint FK_check_checktId foreign key (check_empId) references checkt(checkt_id) --alter table CheckInfo add constraint CK_check_hire check(check_hirecheck_hire) go --培训管理:Train if exists(select * from sysobjects where name='Train') drop table Train create table Train ( Train_id int primary key identity(1,1) not null, --培训编号 主键 自增 Int Train_time datetime not null, --培训日期 Datetime Train_address varchar(200) not null, --地址 Varchar(200) Train_content text not null, --内容 text Emp_id int not null, --职员编号 Int Train_teacher varchar(20) not null --讲师 Varchar(20) ) go --奖罚记录类型:PrizeAmerceType if exists(select * from sysobjects where name='PrizeAmerceType') drop table PrizeAmerceType create table PrizeAmerceType ( Prizet_id int primary key identity(1,1) not null, --奖罚记录类型id 主键 自增 Int Prizet_name varchar(50) not null, --奖罚记录名称 Varchar(50) Prizet_money money not null --奖罚金额 Money ) go --奖罚记录表:PrizeAmerceRecord if exists(select * from sysobjects where name='PrizeAmerceRecord') drop table PrizeAmerceRecord create table PrizeAmerceRecord ( Prize_id int primary key identity(1,1) not null, --奖罚记录id 主键 自增 Int Emp_id int not null, --员工id 外键 Int Prize_time datetime not null, --时间 Datetime Prizet_id int not null, --引用奖罚类型id 外键 Int Prize_desc text null, --描述 空,默认没有描述 text ) ----约束 --alter table PrizeAmerceRecord add constraint FK_prize_empid foreign key(prize_empId) references Employee(emp_id) --alter table PrizeAmerceRecord add constraint FK_prize_prizetid foreign key(prize_prizetId) references PrizeAmerceType(prizet_id) alter table PrizeAmerceRecord add constraint DF_prize_desc default('没有描述') for prize_desc go --档案:Record if exists(select * from sysobjects where name='Record') drop table Record create table Record ( Re_id int primary key identity(1,1) not null, --档案id 主键 自增 Int Re_code varchar(50) not null, --档案代码 日期+随即数生成 Varchar(50) Emp_id int not null, --员工id 外键 Int Prize_id int not null --奖罚记录id 外键 Int ) ----约束 --alter table Record add constraint FK_re_empId foreign key (re_empId) references Employee(emp_id) go --薪资表:SalaryInfo if exists(select * from sysobjects where name='SalaryInfo') drop table SalaryInfo create table SalaryInfo ( Sal_id int primary key identity(1,1) not null, --薪水表ID 主键 自增 Int Emp_id int not null, --职员ID 外键 Int Sal_bonus money not null, --奖金 Money Sal_deduct numeric(18,2) not null, --扣除 Numerica(18,2) Sal_tax money not null, --扣税 Money 默认为0 Sal_sum money not null, --总薪水 Money Sal_date smalldatetime not null --发放日期 smalldatetime ) ----约束 --alter table SalaryInfo add constraint FK_sal_empid foreign key (sal_empId) references Employee(emp_id) go --***********采购管理 --供应商级别:LevelInfo if exists(select * from sysobjects where name='LevelInfo') drop table LevelInfo create table LevelInfo ( Level_id int primary key identity(1,1) not null, --级别编号 主 自增 Int Level_name varchar(10) not null --级别名称 Varchar(10) ) go --供应商:Victualer if exists(select * from sysobjects where name='Victualer') drop table Victualer create table Victualer ( Victu_id int primary key identity(1,1) not null, --供应商编号 主 自增 Int Victu_name varchar(100) not null, --名称 Varchar(100) Level_id int not null, --级别编号 外,LevelInfo表 Int Victu_people varchar(20) not null, --联系人 Varchar(20) Victu_telephone varchar(11) not null, --联系电话 Varvhar(11),必须是11位 Victu_email varchar(50) null, --邮件 空,必须符合邮件格式 Varchar(50) Victu_address text not null, --联系地址 Text 默认地址不详 Victu_remark text null --备注 空,默认没有备注 Text ) ----约束 --alter table Victualer add constraint FK_Victu_levelId foreign key(Victu_levelId) references LevelInfo(Level_id) alter table Victualer add constraint CK_Victu_telephone check(len(victu_telephone)=11) alter table Victualer add constraint CK_Victu_email check(victu_email like '%@%.%') alter table Victualer add constraint DF_Victu_remark default ('没有描述') for victu_remark alter table Victualer add constraint DF_Victu_address default ('地址不详') for victu_address go --商品类别:Sort if exists(select * from sysobjects where name='Sort') drop table Sort create table Sort ( Sort_id int primary key identity(1,1) not null, --类别编号 主 自增 Int Sort_name varchar(50) not null --类别名称 Varchar(50) ) go --商品规格表:Spec if exists(select * from sysobjects where name='Spec') drop table Spec create table Spec ( Spec_id int primary key identity(1,1) not null, --规格编号 主 自增 Int Spec_name varchar(50) not null --规格名称 Varchar(50) ) go --商品表:Product if exists(select * from sysobjects where name='Product') drop table Product create table Product ( Pro_id int primary key identity(1,1) not null, --商品编号 主 自增 Int Pro_code varchar(20) not null, --商品代码 按日期+随机数生成,唯一 Varchar(20) Pro_name varchar(50) not null, --商品名称 Varchar(50) Sort_id int not null, --类别编号 外,Sort表 Int Spec_id int not null, --规格编号 外,Spec表 Int Pro_count int not null, --数量 Int Pro_inPrice money not null, --进货价 Money Pro_outPrice money not null, --销售价 Money Victu_id int not null, --供应商编号 外,Victualer表 Int 级联删除 Pro_remark text null --备注 空,默认没有备注 Text ) ----约束 go --询价单:AskPrice if exists(select * from sysobjects where name='AskPrice') drop table AskPrice create table AskPrice ( Ask_id int primary key identity(1,1) not null, --询价单编号 主 自增 Int Victu_id int not null, --供应商编号 外,Victualer表 Int Pro_id int not null, --商品编号 外,Product表 Int ask_price money not null, --报价 Money Ask_time datetime not null --添加时间 Datetime ) ----约束 go --采购单: BuyBill if exists(select * from sysobjects where name='BuyBill') drop table BuyBill create table BuyBill ( Buybill_id int primary key identity(1,1) not null, --采购单编号 主 自增 Int Buybill_num varchar(20) not null, --采购单据号 按日期+随机数生成,唯一 varchar(20) Emp_id int not null, --采购员编号 外,职员表 Int Buybill_time datetime not null, --采购时间 采购时间必须在交货时间之前 Datetime Buybill_delitime datetime not null, --交货时间 Datetime Buybill_remark text not null, --合同备注 Text Buybill_Isexam bit not null --库管是否审批 bit ) ----约束 go --采购明细表: BuyList if exists(select * from sysobjects where name='BuyList') drop table BuyList create table BuyList ( Buylist_id int primary key identity(1,1) not null, --采购明细编号 主 自增 Int Buybill_id int not null, --采购单编号 外,Buybill表 Int Pro_id int not null, --商品编号 外,Prduct表 Int Buylist_Count int not null, --采购数量 Int Buylist_price money not null, --采购价 Money Victu_id int not null, --供应商编号 外,Vitcualer表 Int Dsub_id int not null --采购单商品仓库编号 外,DepotSubarea表 Int ) ----约束 go --采购付款单:PayBill if exists(select * from sysobjects where name='PayBill') drop table PayBill create table PayBill ( Pay_id int primary key identity(1,1) not null, --付款单编号 主 自增 Int Buybill_id int not null, --采购单编号 外,Buybill表 Int Pay_oncoming money not null, --此次付款 如果应付款分为几次付 Money Pay_deal money not null, --应付款 则应付款应该要保持一致 Money Emp_id int not null, --财务部审批人编号 外,职员表 Int Pay_Isexam bit not null, --财务部是否审批 bit Pay_remark text null --备注 空,默认没有备注 text ) ----约束 go --采购退货单:MoveBill if exists(select * from sysobjects where name='MoveBill') drop table MoveBill create table MoveBill ( Move_id int primary key identity(1,1) not null, --退货单编号 主 自增 Int Buybill_id int not null, --采购单编号 外,Buybill表 Int Move_time datetime not null, --退单日期 退单日期必须是在采购日期之后 Datetime Dsub_id int not null, --库管审批人编号 外, Int Move_Isexam bit not null, --库管是否审批 Bit Move_remark text null --备注 空,默认没有备注 Text ) ----约束 go --***********仓库管理 --库区表:DepotSubarea if exists(select * from sysobjects where name='DepotSubarea') drop table DepotSubarea create table DepotSubarea ( Dsub_id int primary key identity(1,1) not null, --库区id 主键 自增 Int Dsub_name varchar(10) not null,--库区名称 Varchar(10) Dsub_type varchar(10) not null--仓库类别 Varchar(10) ) go --移动类型表:Transfer if exists(select * from sysobjects where name='Transfer') drop table Transfer create table Transfer ( Tran_id int primary key identity(1,1) not null, --移动类型id 主键 自增 Int Tran_name varchar(20) not null --移动类型名称 Varchar(20) ) go --入库明细表:PutInfo if exists(select * from sysobjects where name='PutInfo') drop table PutInfo create table PutInfo ( Put_id Int primary key identity(1,1) not null, --入库明细id 主键 自增 Put_code Varchar(20) not null, --入库单代码 日期+随即数生成 Buybill_id Int not null, --采购单编号 外键 Put_time Datetime not null, --入库时间 Put_people varchar(20) not null, --入库人 Dsub_id Int not null, --库区 外键 级联删除 Tran_id Int not null --移动类型 外键 ) ----约束 go --库存表:Stock if exists(select * from sysobjects where name='Stock') drop table Stock create table Stock ( Stock_id Int primary key identity(1,1) not null, --库存编号 主键 自增 Dsub_id Int not null,            --库区id 外键 Pro_id Int not null,              --商品id 外键 Stock_number Int not null             --商品数量 ) ----约束 go --出库明细表:OutInfo if exists(select * from sysobjects where name='OutInfo') drop table OutInfo create table OutInfo ( Out_id Int primary key identity(1,1) not null, --出库明细id 主键 自增 Out_code Varchar(20) not null, --出库单据号 唯一 Out_time Datetime not null, --出库时间 Out_llr Varchar(20) not null, --领料人 Out_flr Varchar(20) not null, --发料人 Out_tranId Int not null, --移动类型 Out_dsubId Int not null --库区 ) ----约束 go --补仓管理:RepairDepot if exists(select * from sysobjects where name='RepairDepot') drop table RepairDepot create table RepairDepot ( Repa_id Int primary key identity(1,1) not null, --补仓id 主键 自增 Pro_id Int not null, --商品id 外键 Repa_number Int not null, --补仓数量 Repa_dsubId Int not null, --库区表 外键 Repa_remark text null --备注 空,默认没有备注 ) ----约束 go --***********销售管理 --客户级别表(CustLevel) if exists(select * from sysobjects where name='CustLevel') drop table CustLevel create table CustLevel ( Cl_id Int primary key identity(1,1) not null,   --编号 主,自增 Cl_name Varchar(10) not null,           --级别名称 Cl_discount float not null            --折扣 ) --约束 go --客户信息表(customer) if exists(select * from sysobjects where name='customer') drop table customer create table customer ( C_id int primary key identity(1,1) not null,   --编号 主,自动增长 C_number Varchar(10) not null,          --客户代号 C_name Varchar(20) not null,            --客户名称 C_linkman Varchar(20) not null,        --联系人 C_phone Varchar(11) not null,          --联系电话 C_address Text null,              --公司地址 空,默认地址不详 Cl_id Int not null,              --级别编号 外 C_remark text null,               --备注信息 默认没有备注 空 ) --约束 go --订单表(orders) if exists(select * from sysobjects where name='orders') drop table orders create table orders ( O_id int primary key identity(1,1) not null,  --编号 主,自增 O_number Varchar(20) not null,         --订单代码 日期+随即数生成 O_timestart datetime not null,        --下单日期 下单时间必须在交货时间之前 O_timestop Datetime not null,         --交货日期 O_money Money not null,             --下单金额 C_id Int not null,             --客户编号 外 级联删除 Emp_id int not null              --员工编号 外 ) --约束 go --订单明细表(OrderDetails) if exists(select * from sysobjects where name='OrderDetails') drop table OrderDetails create table OrderDetails ( Od_id Int primary key identity(1,1) not null,   --编号 主,自增 O_id int not null,                --订单编号 外 Pro_id int not null,                --商品编号 外 Od_price Money not null,               --单件金额 Od_accounts Int not null               --单件数量 ) --约束 go --销售单表(Sells) if exists(select * from sysobjects where name='Sells') drop table Sells create table Sells ( Sell_id int primary key identity(1,1) not null,    --编号 主,自增 O_id int not null,                 --订单编号 外 Sell_timestart datetime not null,           --销售日期 下单时间必须在交货时间之前 Sell_timestop Datetime not null,            --交货日期 Sell_money Money not null,              --销售金额 C_id Int not null,              --客户编号 外 Emp_id int not null,               --员工编号 外 Sell_remark text null                   --备注 空 ) --约束 go --销售单明细表(SellDetails) if exists(select * from sysobjects where name='SellDetails') drop table SellDetails create table SellDetails ( Selld_id int primary key identity(1,1) not null,   --编号 主,自增 O_id int not null,               --订单编号 Pro_id int not null,              --商品编号 Selld_price Money not null,             --单件金额 Selld_accounts Int not null              --单件数量 ) --约束 go --***********财务管理 --财务科目表:FinaSub if exists(select * from sysobjects where name='FinaSub') drop table FinaSub create table FinaSub ( Fina_id Int primary key identity(1,1) not null, --科目编号 主,自增 Fina_name Varchar(50) not null, --科目名称 Fina_accounts Varchar(50) not null, --银行账号 随机数生成 Fina_people Varchar(50) not null, --联系人 Fina_telephone Varchar(11) not null, --联系电话 Fina_mode Varchar(10) not null, --是借或贷 Fina_play Varchar(10) not null, --借贷方式(现金、发票) Fian_money money not null --金额 ) go --发票信息表:Invoice if exists(select * from sysobjects where name='Invoice') drop table Invoice create table Invoice ( Invo_id Int primary key identity(1,1) not null, --发票编号 主,自增 Invo_code Varchar(50) not null, --发票单据号 日期+随机数生成,唯一 Invo_type Varchar(10) not null, --发票类型 Invo_money money not null, --金额 Invo_use Varchar(50) not null, --发票用途 Invo_datetime Datetime not null, --发票日期 Emp_id int not null --财务员编号 外,职员表 ) ----约束 go --固定资产表:FixedAssets if exists(select * from sysobjects where name='FixedAssets') drop table FixedAssets create table FixedAssets ( Fix_id Int primary key identity(1,1) not null, --资产编号 主,自增 Fix_name Varchar(100) not null, --资产名称 Fix_money Money not null, --可汇兑金额 Fix_datetime Datetime not null, --添加时间 Fix_remark Text null, --备注 空,默认没有备注 ) ----约束 go --财务员统计视图 --***********权限管理 --用户表:UserInfo if exists(select * from sysobjects where name='UserInfo') drop table UserInfo create table UserInfo ( u_id int primary key identity(1,1) not null, --用户编号,主,自增 u_name varchar(20) not null, --用户名,即登录名 u_pass varchar(10) not null, --登录密码 u_time datetime not null --登录时间 ) --insert into UserInfo values('admin','admin','2008-08-05') --insert into UserInfo values('yqh','yqh','2008-08-05') --insert into UserInfo values('gogo','gogo','2008-08-05') --insert into UserInfo values('wangwang','wangwang','2008-08-05') select * from UserInfo go --角色表:RolesInfo if exists(select * from sysobjects where name='RolesInfo') drop table RolesInfo create table RolesInfo ( r_id int primary key identity(1,1) not null, --角色编号,主,自增 r_name varchar(20) not null, --角色名称,即职位名称,唯一 r_desc text null --角色描述,空,默认没有描述 ) alter table RolesInfo add constraint UQ_r_name unique (r_name) alter table RolesInfo add constraint DF_r_desc default ('没有描述') for r_desc --insert into RolesInfo values('系统管理员','可以有任何操作') --insert into RolesInfo values('总经理','最高管理者') --insert into RolesInfo values('部门经理','管理者') --insert into RolesInfo values('普通员工',default) select * from RolesInfo go --用户和角色中间表(因为是、一对多的关系):UserRolesCenter if exists(select * from sysobjects where name='UserRolesCenter') drop table UserRolesCenter create table UserRolesCenter ( c_id int primary key identity(1,1) not null, --中间表编号,主,自增 u_id int not null, --外,用户编号,修改和删除规则都是层叠 r_id int not null --外,角色编号,修改和删除规则都是层叠 ) alter table UserRolesCenter add constraint FK_u_id foreign key (u_id) references UserInfo(u_id) alter table UserRolesCenter add constraint FK_r_id foreign key (r_id) references RolesInfo(r_id) --insert into UserRolesCenter values(1,1) --insert into UserRolesCenter values(1,2) --insert into UserRolesCenter values(2,2) --insert into UserRolesCenter values(3,3) --insert into UserRolesCenter values(4,4) select * from UserRolesCenter --delete from UserRolesCenter where c_id=5 --查询视图 select * from UserRolesView --查询角色1中已有的用户,利用视图 select * from UserRolesView where r_id=1 --查询角色1中没有的用户,利用子查询,用户表中的所有用户减去角色中已有的用户 Select * from UserInfo where u_id not in (select u_id from UserRolesView where r_id=1) go --菜单表:MenuInfo if exists(select * from sysobjects where name='MenuInfo') drop table MenuInfo create table MenuInfo ( M_id Int primary key identity(1,1) not null,--菜单编号 主,自增 M_name Varchar(50) not null,--菜单名称 M_url Varchar(50) null,--菜单链接 空 M_parentId int not null,--父菜单编号 ) --约束 --insert into MenuInfo values('系统权限管理','',0) --insert into MenuInfo values('人力资源管理','',0) --insert into MenuInfo values('采购管理','',0) --insert into MenuInfo values('仓库管理','',0) --insert into MenuInfo values('销售管理','',0) --insert into MenuInfo values('财务管理','',0) select * from MenuInfo go --4.角色和菜单中间表即权限表:PowerInfo if exists(select * from sysobjects where name='PowerInfo') drop table PowerInfo create table PowerInfo ( P_id Int primary key identity(1,1) not null,--权限编号 主 R_id Int not null,--角色编号 外 ,修改和删除规则都是层叠 M_id int not null,--菜单编号 外 ,修改和删除规则都是层叠 ) --约束 alter table PowerInfo add constraint FK_pr_id foreign key (r_id) references RolesInfo(r_id) alter table PowerInfo add constraint FK_m_id foreign key (m_id) references MenuInfo(m_id) --insert into PowerInfo values(1,1) --insert into PowerInfo values(1,2) --insert into PowerInfo values(2,2) --insert into PowerInfo values(3,3) --insert into PowerInfo values(4,4) select * from PowerInfo --查询视图 select * from RolesMenuView --查询角色1中已有的功能,利用视图 select * from RolesMenuView where r_id=1 --查询角色1中没有的功能,利用子查询,菜单表中的所有功能减去角色中已有的功能 Select * from MenuInfo where m_id not in (select m_id from RolesMenuView where r_id=1) go
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值