返回介绍

附录 A 数据库模型描述与级联学生关系建表语句

发布于 2025-04-26 13:08:36 字数 6308 浏览 0 评论 0 收藏

1. 数据库模型描述

如果没有提及特殊的场景,本书一般都采用 MySQL 数据库,并运用了以下的数据模型,如图附录 A-1 所示。

264_0001

图附录 A-1 本书使用的数据库模型

这个场景还是比较简单的,适合我们入门使用,角色和用户作为一个独立的实体,存放在角色和用户表中,它们之间使用一个用户角色表关联,该表有用户的编号和角色的编号。一个用户可以有多个角色,同样一个角色也可以有多个用户,二者是多对多的关系。

下面我们给出它们的建表语句,如代码清单附录 A-1 所示。

代码清单附录 A-1:数据库模型建表语句

/*==============================================================*/
/* Table: T_ROLE                                                */
/*==============================================================*/
create table T_ROLE
(
   id                   int(20) not null auto_increment comment '编号',
   role_name            varchar(60) not null comment '角色名称',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: T_USER                                                */
/*==============================================================*/
create table T_USER
(
   id                   bigint(20) not null auto_increment comment '编号',
   user_name            varchar(60) not null comment '用户名称',
   cnname               varchar(60) not null comment '姓名',
   sex                  tinyint(3) not null comment '性别',
   mobile               varchar(20) not null comment '手机号码',
   email                varchar(60) comment '电子邮件',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: T_USER_ROLE                                           */
/*==============================================================*/
create table T_USER_ROLE
(
   user_id              bigint(20) not null comment '用户编号',
   role_id              int(20) not null comment '角色编号',
   primary key (user_id, role_id)
);

alter table T_USER_ROLE add constraint FK_Reference_1 foreign key (user_id)
      references T_USER (id) on delete restrict on update restrict;

alter table T_USER_ROLE add constraint FK_Reference_2 foreign key (role_id)
      references T_ROLE (id) on delete restrict on update restrict;

2. 级联学生关系建表语句

代码清单附录 A-2:级联学生关系建表语句

drop table if exists t_lecture;

drop table if exists t_student;

drop table if exists t_student_health_female;

drop table if exists t_student_health_male;

drop table if exists t_student_lecture;

drop table if exists t_student_selfcard;

/*=================================================*/
/* Table: t_lecture                                             */
/*=================================================*/
create table t_lecture
(
   id                   int(20) not null auto_increment comment '编号',
   lecture_name         varchar(60) not null comment '课程名称',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: t_student                                             */
/*==============================================================*/
create table t_student
(
   id                   int(20) not null auto_increment comment '编号',
   cnname               varchar(60) not null comment '学生姓名',
   sex                  tinyint(4) not null comment '性别',
   selfcard_no          int(20) not null comment '学生证号',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: t_student_health_female                               */
/*==============================================================*/
create table t_student_health_female
(
   id                   int(20) not null auto_increment comment '编号',
   student_id           varchar(60) not null comment '学生编号',
   check_date           varchar(60) not null comment '检查日期',
   heart                varchar(60) not null comment '心',
   liver                varchar(60) not null comment '肝',
   spleen               varchar(60) not null comment '脾',
   lung                 varchar(60) not null comment '肺',
   kidney               varchar(60) not null comment '肾',
   uterus               varchar(60) not null comment '子宫',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: t_student_health_male                                 */
/*==============================================================*/
create table t_student_health_male
(
   id                   int(20) not null auto_increment comment '编号',
   student_id           varchar(60) not null comment '学生编号',
   check_date           varchar(60) not null comment '检查日期',
   heart                varchar(60) not null comment '心',
   liver                varchar(60) not null comment '肝',
   spleen               varchar(60) not null comment '脾',
   lung                 varchar(60) not null comment '肺',
   kidney               varchar(60) not null comment '肾',
   prostate             varchar(60) not null comment '前列腺',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: t_student_lecture                                     */
/*==============================================================*/
create table t_student_lecture
(
   id                   int(20) not null auto_increment comment '编号',
   student_id           int(20) not null comment '学生编号',
   lecture_id           int(20) not null comment '课程编号',
   grade                decimal(16,2) not null comment '评分',
   note                 varchar(1024) comment '备注',
   primary key (id)
);

/*==============================================================*/
/* Table: t_student_selfcard                                    */
/*==============================================================*/
create table t_student_selfcard
(
   id                   int(20) not null auto_increment comment '编号',
   student_id           int(20) not null comment '学生编号',
   native               varchar(60) not null comment '籍贯',
   issue_date           date not null comment '发证日期',
   end_date             date not null comment '结束日期',
   note                 varchar(1024) comment '备注',
   primary key (id));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。