記帳系統從需求到實現 ----數據庫的設計

需求跟UI原型設計完成以後,現在就開始對數據庫進行設計和實施了,我用的是Powerdesigner做的數據庫模型圖,然後自動轉化成MySql的腳本代碼。模型圖如下:

數據模型圖

 

由於表內已經有了描述,所以數據字典我也不寫了,大家看看描述就大概知道了。

 

drop table if exists TAccount;

 

drop table if exists TAccountEvent;

 

drop table if exists TAccountType;

 

drop table if exists TConsumeType;

 

drop table if exists TEvent;

 

drop table if exists TFriendRelation;

 

drop table if exists TGroup;

 

drop table if exists TLeaveword;

 

drop table if exists TLovenote;

 

drop table if exists TUser;

 

drop table if exists TUserConsumeType;

 

drop table if exists TUserGroup;

 

/*==============================================================*/

/* Table: TAccount                                              */

/*==============================================================*/

create temporary table TAccount

(

   accountId            bigint not null,

   userId               bigint comment '帳戶使用者ID',

   typeId               bigint comment '帳戶類型ID',

   No                   char(16) not null comment '帳號',

   name                 varchar(32) not null comment '帳戶名稱',

   remark               varchar(64) comment '帳戶描述',

   balance              float comment '帳戶餘額',

   insertDate           datetime,

   modyDate             datetime,

   delType              varchar(1) default '''U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (accountId)

)

comment = "帳戶表";

 

/*==============================================================*/

/* Table: TAccountEvent                                         */

/*==============================================================*/

create table TAccountEvent

(

   aeId                 bigint not null,

   accountId            bigint not null comment '帳戶ID',

   eventId              bigint not null comment '收支項ID',

   money                float comment '分配的收支',

   status               varchar(1) default 'U' comment '''U'': 等待確認; ''A'': 確認',

   recorder             bigint comment '記錄用戶ID',

   confirmer            bigint comment '確認用戶ID',

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (aeId)

)

comment = "收支項分配帳戶表";

 

/*==============================================================*/

/* Table: TAccountType                                          */

/*==============================================================*/

create table TAccountType

(

   typeId               bigint not null,

   name                 varchar(32) not null comment '帳戶類型名稱',

   remark               varchar(64) comment '類型描述',

   insertDate           date,

   modyDate             date,

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (typeId)

)

comment = "帳戶類型";

 

/*==============================================================*/

/* Table: TConsumeType                                          */

/*==============================================================*/

create table TConsumeType

(

   typeId               bigint not null,

   typeName             varchar(32) not null comment '類型名稱',

   typeSort             varchar(1) not null default 'P' comment 'I 收入,’P‘支出,''T'' 轉帳',

   insertDate           datetime,

   modyDate             datetime,

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (typeId)

)

comment = "收支類型,如’食品‘,’工資‘等"

ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

/*==============================================================*/

/* Table: TEvent                                                */

/*==============================================================*/

create table TEvent

(

   eventId              bigint not null,

   typeId               bigint not null comment '收支項ID',

   money                float not null comment '收支金額',

   remark               varchar(64) comment '收支事件描述',

   eventDate            datetime not null comment '收支事件發生的日期',

   insertDate           datetime,

   modyDate             datetime,

   recorder             bigint comment '記錄用戶的ID',

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (eventId)

)

comment = "收支事件信息表"

ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

/*==============================================================*/

/* Table: TFriendRelation                                       */

/*==============================================================*/

create table TFriendRelation

(

   friendId             bigint not null,

   master               bigint not null comment '建關係的用戶ID',

   client               bigint not null comment '建交對象用戶ID',

   status               varchar(1) default 'U' comment 'U’沒有確認,‘R‘拒絕建交,‘A’已經確認',

   remark               varchar(64) comment '建交留言',

   modyDate             date,

   insertDate           date,

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (friendId)

)

comment = "好友關係表";

 

/*==============================================================*/

/* Table: TGroup                                                */

/*==============================================================*/

create table TGroup

(

   groupId              bigint not null comment 'ID',

   name                 varchar(32) not null comment '組名稱',

   remark               varchar(64) comment '組描述',

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (groupId)

)

comment = "組信息,一個或多個用戶可能組成一個組。";

 

/*==============================================================*/

/* Table: TLeaveword                                            */

/*==============================================================*/

create table TLeaveword

(

   leavewordId          bigint not null,

   lyUser               bigint not null comment '留言用戶ID',

   content              mediumtext comment '留言內容',

   revertUser           bigint comment '回答用戶ID',

   revert               mediumtext comment '回答內容',

   state                varchar(1) default 'U' comment 'U‘等待回答,’A‘已答',

   insertDate           datetime,

   modyDate             datetime,

   faceUrl              varchar(100) default NULL comment '標題圖像',

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (leavewordId)

)

ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

/*==============================================================*/

/* Table: TLovenote                                             */

/*==============================================================*/

create table TLovenote

(

   noteId               bigint not null,

   noteTitle            varchar(32) not null comment '通訊標題',

   Content              text not null comment '通訊內容',

   Listener             bigint not null comment '接收方ID',

   Sender               bigint not null comment '傳送方ID',

   times                datetime comment '發送時間',

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (noteId)

)

comment = "兩用戶的通訊"

ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

/*==============================================================*/

/* Table: TUser                                                 */

/*==============================================================*/

create table TUser

(

   userId               bigint not null,

   email                varchar(100),

   password             varchar(32) not null,

   userName             varchar(32) not null,

   hand                 varchar(100) default '../images/user.gif' comment '用戶頭像的URL',

   sex                  varchar(1) default 'M' comment '性別,‘M’男性,‘F’女性',

   link                 varchar(100) default '' comment '鏈接',

   intro                mediumtext comment '個人簡介',

   userType             varchar(1) default 'U' comment 'U’普通用戶,‘A’管理員',

   insertDate           datetime comment '建立時間',

   modyDate             datetime comment '最後修改時間',

   home                 varchar(100) default '' comment '個人主頁',

   delType              varchar(1) default 'U' comment '是否刪除了,‘U’爲末刪除,‘D’爲已刪除',

   primary key (userId)

)

comment = "用戶信息表"

ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

/*==============================================================*/

/* Table: TUserConsumeType                                      */

/*==============================================================*/

create table TUserConsumeType

(

   userId               bigint comment '用戶ID',

   typeId               bigint comment '用戶對應的收支類型ID'

)

comment = "用戶和收支類型的關係,多對多";

 

/*==============================================================*/

/* Table: TUserGroup                                            */

/*==============================================================*/

create table TUserGroup

(

   userId               bigint comment '用戶ID',

   groupId              bigint comment '相關的組ID'

)

comment = "UserGroup的關係,多對多";

 

alter table TAccount add constraint FK_Reference_12 foreign key (userId)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TAccount add constraint FK_Reference_15 foreign key (typeId)

      references TAccountType (typeId) on delete restrict on update restrict;

 

alter table TAccountEvent add constraint FK_Reference_16 foreign key (recorder)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TAccountEvent add constraint FK_Reference_17 foreign key (confirmer)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TAccountEvent add constraint FK_Reference_3 foreign key (accountId)

      references TAccount (accountId) on delete restrict on update restrict;

 

alter table TAccountEvent add constraint FK_Reference_4 foreign key (eventId)

      references TEvent (eventId) on delete restrict on update restrict;

 

alter table TEvent add constraint FK_Reference_18 foreign key (recorder)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TEvent add constraint FK_Reference_5 foreign key (typeId)

      references TConsumeType (typeId) on delete restrict on update restrict;

 

alter table TFriendRelation add constraint FK_Reference_19 foreign key (master)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TFriendRelation add constraint FK_Reference_20 foreign key (client)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TLeaveword add constraint FK_Reference_10 foreign key (lyUser)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TLeaveword add constraint FK_Reference_11 foreign key (revertUser)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TLovenote add constraint FK_Reference_8 foreign key (Sender)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TLovenote add constraint FK_Reference_9 foreign key (Listener)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TUserConsumeType add constraint FK_Reference_13 foreign key (userId)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TUserConsumeType add constraint FK_Reference_14 foreign key (typeId)

      references TConsumeType (typeId) on delete restrict on update restrict;

 

alter table TUserGroup add constraint FK_Reference_6 foreign key (userId)

      references TUser (userId) on delete restrict on update restrict;

 

alter table TUserGroup add constraint FK_Reference_7 foreign key (groupId)

      references TGroup (groupId) on delete restrict on update restrict;

 

這一段是MySql的腳本語言,打開windows命令控制檯,如:

                    

                     創建數據庫

這裏把前面的腳本代碼copy過來運行,數據庫便生成好了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
發佈了31 篇原創文章 · 獲贊 16 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章