数据库应用大作业
校园一卡通系统
1. E-R 模型
学号
姓名
消费日期
卡号
姓名
消费金额
消费
卡
学号
余额
卡号
学生
卡
一卡通
卡号
性别
院系
密码
挂失
卡
银行卡
姓名
余额
银 行 卡
身 份 证
2.ER 模型到关系模型的转换
学生:Student
( StuID, StuName, StuAcademy,
StuClass);
卡:Card (CardID,
StuID, CreateTime , EndTime, Balance,Status);
刷卡机:Smac
(PosID, Balance);
消 费 情 况 : Consume_Record(CardID , Consume_Time,
Expenditure,fangshi);
销户记录表 destroy_record(cardid ,destroy_time ,StuID );
刷卡机和一卡通是多对多的关系,生成的表:
CARD_POS (CARDID, POSID, CARD_POS_CON);
充值机和一卡通是多对多的关系,生成的表:
CARD_CHARGE (CARDID, CHARGEID, CARD_CHARGE_CON);
3.关系模式的规范化与反规范化
为了方便同学查询餐厅的消费情况,特将消费性刷卡联系转化为独立关系模
式;为了了解同学借阅图书的情况,将身份认证中的借书刷卡转化为独立的关系
模式;同时,考虑同学要向校园卡中充值,在学生校园卡联系中抽取充值关系转
化为独立关系模式;考虑同学可能会挂失等,从学生校园卡联系中抽取挂失关系
转化为独立关系模式。
具体的关系模式转化结果如下
规范化:
学生信息(学号,姓名,性别,班级)
一卡通信息(学号,卡号,余额,挂失情况)
管理员(管理员编号,姓名,级别)
高级管理员(管理员编号,密码)
设备(设备编号,设备状态,管理员编号)
日志(卡号,消费时间,消费类型,设备编号,管理员编号,消费金额)
反规范化:
一卡通(学号,姓名,性别,挂失情况,卡号,余额)
日志(卡号,消费时间,消费类型,设备编号,管理员编,消费金额)
管理员(管理员编号,姓名,级别,设备编号,设备状态)
4.建表 SQL 语句
--创建学生信息表
create table Student(
StuID VARCHAR2(15) primary key,--学号
StuName VARCHAR2(15) not null, --学生姓名
StuAcademy VARCHAR2(20) not null, --学院
StuClass VARCHAR2(15) not null)
--班级
--创建卡表
create table Card(
CardID number(15) primary key, --卡号
StuID VARCHAR2(15) unique,
foreign key(StuID)references
Student(StuID), --学号
CreateTime
date not null,
--办卡日期
EndTime date default '08-8 月-14',
--卡失效日期
Balance number(6,2) default 0.00 ,--卡内余额
Status number(1) default 0)--0 表示可正常使用,1 表示已挂失
--创建刷卡机表
create table POS(
PosID VARCHAR2(15) primary key,
Balance number not null
)
--创建消费情况表
create table update_action(
stuID number(8),
oldbalance number(8),
newbalance number(8),
spend number(8),
stuname char(8),
--销户的记录表
create table destroy_record(
cardid number(15),
destroy_time date not null,
StuID varchar2(15))
CREATE TABLE
"CARD_CHARGE"
(
"CARDID" VARCHAR2(15),
"CHARGEID" VARCHAR2(15),
CONSTRAINT "CARD_CHARGE_CON" PRIMARY KEY ("CARDID",
"CHARGEID") ENABLE
)
CREATE TABLE
"CARD_POS"
(
"CARDID" VARCHAR2(15),
"POSID" VARCHAR2(15),
CONSTRAINT "CARD_POS_CON" PRIMARY KEY ("POSID", "CARDID")
ENABLE
)
5.约束定义
ORACLE 系统一共提供了以下五种约束:
非空(NOT NULL)约束 – 所定义的列决不能为空。
唯一(UNIQUE)约束– 在表中每一行中所定义的这列或这些列的值都不能
相同。
主键(PRIMARY KEY)约束 - 唯一地标示表中的每一行。
外键(FOREIGN KEY)约束 – 用来维护从表和主表之间的引用完整性
条件(CHECK)约束 – 表中每行都要满足该约束条件
DEFAULT 约束-严格来讲,DEFAULT 并不是一种约束
REF 约束-允许对位于某个参考表中的现有记录指定一个引用
6.索引设计
create unique index S_Sno on student(STUNUM);
create unique index Card_CardID on Card(CardID);;
7.触发器
--消费记录触发器
create or replace trigger con_cord
before
insert or update of balance
on card
for each row
begin
if inserting then
if :new.balance is null or :new.balance<0 then
dbms_output.put_line('不得低于 0 元');
END if;
End if;
if updating then
if :new.balance is null or :new.balance<0 then
dbms_output.put_line('不得低于 0 元');
END if;
insert into consume_record values(
:old.stuID,:old.balance,:new.balance,(:new.balance-:old.balance),:old.stu
name,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
END if;
END;--销户触发器
create or replace trigger xiaohui_record
after delete on card
for each row
BEGIN
insert into destroy_record values(:old.cardid,sysdate,:old.STuid);
end;
8.存储过程
--消费查询的存储过程
create or replace procedure readrecord(studentid VARCHAR2,STIME
DATE,ETIME DATE)
AS
CURSOR c_record IS
select
Consume_Record.cardid,Consume_Record.Consume_Time,Consume_Re
cord.Expenditure
from consume_record,card
where
card.stuid=studentid
and
card.cardid=Consume_Record.cardid
and
Consume_Record.Consume_Time between STIME AND ETIME;
v_record c_record%ROWTYPE;
BEGIN
open c_record;
LOOP
fetch c_record into v_record;
exit when c_record%notfound;
dbms_output.put_line(v_record.cardid||'
'||v_record.Consume_time||' '||v_record.Expenditure);
END LOOP;
close c_record;
end;
9.各个业务功能对应的 SQL 语句、存储过程
----卡的解挂函数
create or replace function KaJieGua(StuIDer in VARCHAR2)
return number AS
Current_Status card.status%type;
begin
select Status into Current_Status from Card where StuID=StuIder;
if Current_Status=1 then –1
update Card set Status=0 where StuID =StuIDer;