数据库作业-一个简单的火车购买系统库

设计一个火车订票系统数据库,记录乘客的购票情况,其中乘客有Cid,Cname,Tel,各属性分别表示客户身份证号,客户姓名,客户电话;火车班次有Tno,Start,End,各属性分别表示火车班次号,始发地,目的地;乘客在购买车票的时候要记录价格,时间,座位等级信息。
•根据以上信息完成数据库的设计(要求CDM、PDM、sql代码、数据库生成结果截图)

首先使用powerdesigner生成cdm图

数据库作业-一个简单的火车购买系统库_第1张图片
使用cdm生成pdm图

数据库作业-一个简单的火车购买系统库_第2张图片

需要在购买那个关系增加一些属性

 

连接数据库 生成sql代码

/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2017                    */
/* Created on:     2022/4/26 15:01:22                           */
/*==============================================================*/


if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('购买') and o.name = 'FK_购买_购买_乘客')
alter table 购买
   drop constraint FK_购买_购买_乘客
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('购买') and o.name = 'FK_购买_购买2_火车班次')
alter table 购买
   drop constraint FK_购买_购买2_火车班次
go

if exists (select 1
            from  sysobjects
           where  id = object_id('乘客')
            and   type = 'U')
   drop table 乘客
go

if exists (select 1
            from  sysobjects
           where  id = object_id('火车班次')
            and   type = 'U')
   drop table 火车班次
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('购买')
            and   name  = '购买2_FK'
            and   indid > 0
            and   indid < 255)
   drop index 购买.购买2_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('购买')
            and   name  = '购买_FK'
            and   indid > 0
            and   indid < 255)
   drop index 购买.购买_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('购买')
            and   type = 'U')
   drop table 购买
go

/*==============================================================*/
/* Table: 乘客                                                    */
/*==============================================================*/
create table 乘客 (
   cid                  char(18)             not null,
   cname                char(10)             null,
   tel                  char(11)             null,
   constraint PK_乘客 primary key (cid)
)
go

/*==============================================================*/
/* Table: 火车班次                                                  */
/*==============================================================*/
create table 火车班次 (
   tno                  char(10)             not null,
   start                char(20)             null,
   "end"                char(20)             null,
   constraint PK_火车班次 primary key (tno)
)
go

/*==============================================================*/
/* Table: 购买                                                    */
/*==============================================================*/
create table 购买 (
   cid                  char(18)             not null,
   tno                  char(10)             not null,
   价格                   float                null,
   时间                   datetime             null,
   座位等级                 char(1)              null,
   constraint PK_购买 primary key (cid, tno)
)
go

/*==============================================================*/
/* Index: 购买_FK                                                 */
/*==============================================================*/




create nonclustered index 购买_FK on 购买 (cid ASC)
go

/*==============================================================*/
/* Index: 购买2_FK                                                */
/*==============================================================*/




create nonclustered index 购买2_FK on 购买 (tno ASC)
go

alter table 购买
   add constraint FK_购买_购买_乘客 foreign key (cid)
      references 乘客 (cid)
go

alter table 购买
   add constraint FK_购买_购买2_火车班次 foreign key (tno)
      references 火车班次 (tno)
go

测试代码:

create database test;
select * from 火车班次;
select * from 乘客;
select * from 购买;
insert into 乘客 values('001','张三','123456789');
insert into 乘客 values('002','李四','123454389');
insert into 乘客 values('003','沈五','987654321');
insert into 火车班次 values('t001','北京','重庆');
insert into 火车班次 values('t002','上海','重庆');
insert into 火车班次 values('t003','广东','重庆');

insert into 购买 values('001','t001','100','2022-4-26','A');
insert into 购买 values('001','t002','100','2022-4-26','A');

insert into 购买 values('002','t003','100','2022-4-26','B');
insert into 购买 values('003','t002','100','2022-4-26','C');


select * from 购买 join 火车班次 on 购买.tno=火车班次.tno
join 乘客 on 乘客.cid=购买.cid;

你可能感兴趣的:(数据库,database,sqlserver,mysql,学习)