实验三 SJK数据库设计

2024-05-01 20:04
文章标签 设计 数据库 实验 sjk

本文主要是介绍实验三 SJK数据库设计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实验题目

实验三 SJK数据库设计

实验时间

2023.3.22

实验地点

软件工程基础实验室

实验课时

2

实验目的

​掌握数据库设计基本方法及数据库设计工具。

实验要求

​ 掌握数据库设计基本步骤,包括数据库概念结构设计、逻辑结构设计,物理结构设计,数据库模式SQL语句生成。能够使用数据库设计工具进行数据库设计。

实验步骤

及内容

 设计一个教务信息系统的数据库。数据库包含院系信息,学生信息,教师信息,课程信息,学生选课信息,考试信息。其中,一个学生属于一个院系,可以选修多门课程。一个教师属于一个院系,可以任教多门课程,一门课程也可以有多个老师任教。一门课程有多个课程安排,可以有多场考试安排。

(1)数据库概念结构设计

​ 识别出学生,教师,院系,课程,考试,课程安排六个实体。每个实体的属性、码如下:

学生Student:学号sno,姓名sname,性别ssex,年龄sage,班级class,院系编号dno。主码:学号sno。

教师teacher:编号tno,姓名tname,性别tsex,年龄tage,院系dno,职称title。主码:编号tno。

院系department:院系名depart_name,院系编号dno,院长编号dean_tno。主码:院系编号dno。

课程course:课程号cno,课程名course_name,学时数hours,学分credit,类型type,开设院系dno。主码:课程号。

课程安排course_arrangement:课程号cno,周次week,节次course_time,教室classroom。主码:课程号cno,周次week,节次course_time

考试安排exam_arrangement:课程号cno,考试时间exam_time,考试时长hours,考试地点classroom。主码:课程号,考试地点。

​ 根据语义,分析实体之间的联系,确定实体之间一对一,一对多和多对多联系。

SQL语句生成

​ 由PowerDesigner生成的SQL语句如下:

if exists(select 1 from sys.sysforeignkey where role='fk_sc_s') then

    alter table sc

       delete foreign key fk_sc_s

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_sd') then

    alter table student

       delete foreign key fk_sd

end if;

if exists(

   select 1 from sys.systable

   where table_name='student'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table student

end if;

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

/* Table: student                                               */

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

create table student

(

   sno                  char(12)                       not null,

   sname                char(25)                       null,

   ssex                 char(2)                        null,

   sage                 small int                       null,

   class                char(8)                        null,

   dno                  char(2)                        null,

   constraint PK_STUDENT primary key clustered (sno)

);

alter table student

   add constraint fk_sd foreign key (dno)

      references department (dno)

      on update restrict

      on delete restrict;

if exists(select 1 from sys.sysforeignkey where role='fk_cd') then

    alter table course

       delete foreign key fk_cd

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_ca_c') then

    alter table course_arrangement

       delete foreign key fk_ca_c

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_exam_c') then

    alter table exam_arrangement

       delete foreign key fk_exam_c

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_sc_c') then

    alter table sc

       delete foreign key fk_sc_c

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_tc_c') then

    alter table tc

       delete foreign key fk_tc_c

end if;

if exists(

   select 1 from sys.systable

   where table_name='course'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table course

end if;

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

/* Table: course                                                */

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

create table course

(

   cno                  char(8)                        not null,

   cname                char(25)                       null,

   hours                small int                       null,

   credit               small int                       null,

   type                 char(8)                        null,

   dno                  char(2)                        null,

   constraint PK_COURSE primary key clustered (cno)

);

alter table course

   add constraint fk_cd foreign key ()

      references department (dno)

      on update restrict

      on delete restrict;

if exists(select 1 from sys.sysforeignkey where role='fk_tc_t') then

    alter table tc

       delete foreign key fk_tc_t

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_td') then

    alter table teacher

       delete foreign key fk_td

end if;

if exists(

   select 1 from sys.systable

   where table_name='teacher'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table teacher

end if;

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

/* Table: teacher                                               */

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

create table teacher

(

   tno                  char(12)                       not null,

   tname                char(25)                       null,

   tsex                 char(2)                        null,

   tage                 small int                       null,

   dno                  char(2)                        null,

   title                char(10)                       null,

   constraint PK_TEACHER primary key clustered (tno)

);

alter table teacher

   add constraint fk_td foreign key (dno)

      references department (dno)

      on update restrict

      on delete restrict;

if exists(select 1 from sys.sysforeignkey where role='fk_cd') then

    alter table course

       delete foreign key fk_cd

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_sd') then

    alter table student

       delete foreign key fk_sd

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_td') then

    alter table teacher

       delete foreign key fk_td

end if;

if exists(

   select 1 from sys.systable

   where table_name='department'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table department

end if;

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

/* Table: department                                            */

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

create table department

(

   dno                  char(2)                        not null,

   dame                 char(25)                       null,

   tno                  char(12)                       null,

   constraint PK_DEPARTMENT primary key clustered (dno)

);

if exists(select 1 from sys.sysforeignkey where role='fk_ca_c') then

    alter table course_arrangement

       delete foreign key fk_ca_c

end if;

if exists(

   select 1 from sys.systable

   where table_name='course_arrangement'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table course_arrangement

end if;

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

/* Table: course_arrangement                                    */

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

create table course_arrangement

(

   cno                  char(8)                        not null,

   week                 small int                       not null,

   course_time          char(12)                       not null,

   classroom            char(10)                       null,

   constraint PK_COURSE_ARRANGEMENT primary key clustered (cno, week, course_time)

);

alter table course_arrangement

   add constraint fk_ca_c foreign key (cno)

      references course (cno)

      on update restrict

      on delete restrict;

if exists(select 1 from sys.sysforeignkey where role='fk_exam_c') then

    alter table exam_arrangement

       delete foreign key fk_exam_c

end if;

if exists(

   select 1 from sys.systable

   where table_name='exam_arrangement'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table exam_arrangement

end if;

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

/* Table: exam_arrangement                                      */

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

create table exam_arrangement

(

   cno                  char(8)                        not null,

   exam_time            char(10)                       null,

   classroom            char(10)                       not null,

   hour                 small int                       null,

   constraint PK_EXAM_ARRANGEMENT primary key clustered (cno, classroom)

);

alter table exam_arrangement

   add constraint fk_exam_c foreign key (cno)

      references course (cno)

      on update restrict

      on delete restrict;

if exists(select 1 from sys.sysforeignkey where role='fk_sc_c') then

    alter table sc

       delete foreign key fk_sc_c

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_sc_s') then

    alter table sc

       delete foreign key fk_sc_s

end if;

if exists(

   select 1 from sys.systable

   where table_name='sc'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table sc

end if;

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

/* Table: sc                                                    */

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

create table sc

(

   sno                  char(12)                       not null,

   cno                  char(8)                        not null,

   grade                small int                       null,

   constraint PK_SC primary key clustered (sno, cno)

);

alter table sc

   add constraint fk_sc_c foreign key (cno)

      references course (cno)

      on update restrict

      on delete restrict;

alter table sc

   add constraint fk_sc_s foreign key (sno)

      references student (sno)

      on update restrict

      on delete restrict;

if exists(select 1 from sys.sysforeignkey where role='fk_tc_c') then

    alter table tc

       delete foreign key fk_tc_c

end if;

if exists(select 1 from sys.sysforeignkey where role='fk_tc_t') then

    alter table tc

       delete foreign key fk_tc_t

end if;

if exists(

   select 1 from sys.systable

   where table_name='tc'

     and table_type in ('BASE', 'GBL TEMP')

) then

    drop table tc

end if;

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

/* Table: tc                                                    */

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

create table tc

(

   tno                  char(12)                       not null,

   cno                  char(8)                        not null,

   term                 char(8)                        not null,

   class                char(8)                        null,

   constraint PK_TC primary key clustered (tno, cno, term)

);

alter table tc

   add constraint fk_tc_c foreign key (cno)

      references course (cno)

      on update restrict

      on delete restrict;

alter table tc

   add constraint fk_tc_t foreign key (tno)

      references teacher (tno)

      on update restrict

      on delete restrict;

实验结果及分析

经检验,实验基本达到预期效果。数据库的设计的基本步骤包括数据库概念结构设计、逻辑结构设计,物理结构设计。其中概念结构设计通常用E-R图表示。逻辑结构设计比较重要,需要从E-R图转换得到,并需要完成一些细节,进行调整优化。物理结构设计则需要完成存储路径的设计和存储结构的设计,需要根据实际情况进行设计。例如每个关系的存储结构,具体的数据类型和长度都要符合实际,同时尽量减小开销。使用类似PowerDesigner的工具可以辅助设计,方便的进行模型间的转化,并可以直接生成SQL语句,有必要掌握使用的基本方法。

这篇关于实验三 SJK数据库设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/952553

相关文章

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 数据库表与查询操作实战案例

《MySQL数据库表与查询操作实战案例》本文将通过实际案例,详细介绍MySQL中数据库表的设计、数据插入以及常用的查询操作,帮助初学者快速上手,感兴趣的朋友跟随小编一起看看吧... 目录mysql 数据库表操作与查询实战案例项目一:产品相关数据库设计与创建一、数据库及表结构设计二、数据库与表的创建项目二:员

MybatisPlus中removeById删除数据库未变解决方案

《MybatisPlus中removeById删除数据库未变解决方案》MyBatisPlus中,removeById需实体类标注@TableId注解以识别数据库主键,若字段名不一致,应通过value属... 目录MyBATisPlus中removeBypythonId删除数据库未变removeById(Se

在 Spring Boot 中连接 MySQL 数据库的详细步骤

《在SpringBoot中连接MySQL数据库的详细步骤》本文介绍了SpringBoot连接MySQL数据库的流程,添加依赖、配置连接信息、创建实体类与仓库接口,通过自动配置实现数据库操作,... 目录一、添加依赖二、配置数据库连接三、创建实体类四、创建仓库接口五、创建服务类六、创建控制器七、运行应用程序八

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结