[MySQL光速入门]004 做业解答

建立数据库library

create database library character set utf8;
use library;
复制代码

建立数据表

图书类别表(booktype)

序号 属性名称 含义 数据类型 是否为空 备注
1 typeid 类别编号 int not null 主键
2 typename 类别名称 varchar(20) null
create table booktype(
	typeid int not null primary key,
	typename varchar(20) null
);
复制代码

图书信息表(book)

序号 属性名称 含义 数据类型 是否为空 备注
1 bookid 图书编号 char(10) not null 主键
2 bookname 图书名称 varchar(20) not null
3 typeid 类别编号 int null 外键
4 bookauthor 图书做者 varchar(20) null
5 bookpublisher 出版社 varchar(50) null
6 bookprice 图书价格 doublue null
7 borrowsum 借阅次数 int null
create table book(
	bookid char(10) not null PRIMARY key,
	bookname VARCHAR(20) not null,
	typeid INT,
	bookauthor VARCHAR(20),
	bookpublisher VARCHAR(50),
	bookprice DOUBLE,
	borrowsum int,
	FOREIGN key(typeid) REFERENCES booktype(typeid)
);
复制代码

图书存储信息表(bookstorage)

序号 属性名称 含义 数据类型 是否为空 备注
1 bookbarcode 图书条码 char(20) not null 主键
2 bookid 图书编号 char(10) not null 外键
3 bookintime 图书入馆时间 datetime null
4 bookstatus 图书状态 varchar(4) null
create table bookstorage(
	bookbarcode char(20) not null PRIMARY key,
	bookid char(10) not null,
	bookintime datetime,
	bookstatus VARCHAR(4),
	FOREIGN key(bookid) REFERENCES book(bookid)
);
复制代码

读者类别表(readertype)

序号 属性名称 含义 数据类型 是否为空 备注
1 retypeid 类别编号 int not null
2 typename 类别名称 varchar(20) not null
3 borrowquantity 可借数量 int not null
4 borrowday 可借天数 int null
create table readertype(
	retypeid int not null primary key,
	typename VARCHAR(20) not null,
	borrowquantity int not null,
	borrowday int
);
复制代码

读者信息表(reader)

序号 属性名称 含义 数据类型 是否为空 备注
1 readerid 读者编号 char(10) not null 主键
2 readername 读者姓名 varchar(20) not null
3 readerpass 读者密码 varchar(20) not null
4 retypeid 类别编号 int null 外键
5 readerdate 发证日期 datetime null
6 readerstatus 借书证状态 varchar(4) null
create table reader(
	readerid char(10) not null PRIMARY key,
	readername VARCHAR(20) not null,
	readerpass VARCHAR(20) not null,
	retypeid int,
	readerdate datetime,
	readerstatus VARCHAR(4),
	FOREIGN key(retypeid) REFERENCES readertype(retypeid)
);
复制代码

图书借阅表(bookborrow)

序号 属性名称 含义 数据类型 是否为空 备注
1 borrowid 借阅号 char(10) not null 主键
2 bookbarcode 图书条码 char(20) not null 外键
3 readerid 读者编号 char(10) not null 外键
4 borrowtime 借书日期 datetime null
5 returntime 还书日期 datetime null
6 borrowstatus 借阅状态 varchar(4) null
create table bookborrow(
	borrowid char(10) not null primary key,
	bookbarcode char(20) not null,
	readerid char(10) not null,
	borrowtime datetime,
	returntime datetime,
	borrowstatus VARCHAR(4),
	FOREIGN key(bookbarcode) REFERENCES bookstorage(bookbarcode),
	FOREIGN key(readerid) REFERENCES reader(readerid)
);
复制代码

为建立的表插入以下数据

图书类别表(booktype)

typeid typename
1 天然科学
2 数学
3 计算机
4 建筑水利
5 旅游地理
6 励志/自我实现
7 工业技术
8 基础医学
9 室内设计
10 人文景观
insert into booktype values
	(1,'天然科学'),
	(2,'数学'),
	(3,'计算机'),
	(4,'建筑水利'),
	(5,'旅游地理'),
	(6,'励志/自我实现'),
	(7,'工业技术'),
	(8,'基础医学'),
	(9,'室内设计'),
	(10,'人文景观');
复制代码

图书信息表(book)

bookid bookname typeid bookauthor bookpublisher bookprice borrowsum
TP39/1712 Java程序设计 3 陈永红 机械工业出版社 35.5 30
013452 离散数学 2 张小新 机械工业出版社 45.5 10
TP/3452 JSP程序设计案例 3 刘城清 电子工业出版社 42.8 8
TH/2345 机械设计手册 7 黄明凡 人民邮电出版社 40 10
R/345677 中医的故事 8 李奇德 国防工业出版社 20.0 5
insert into book values
	('TP39/1712','Java程序设计',3,'陈永红','机械工业出版社',35.5,30),
	('013452','离散数学',2,'张小新','机械工业出版社',45.5,10),
	('TP/3452','JSP程序设计案例',3,'刘城清','电子工业出版社',42.8,8),
	('TH/2345','机械设计手册',7,'黄明凡','人民邮电出版社',40,10),
	('R/345677','中医的故事',8,'李奇德','国防工业出版社',20.0,5);
复制代码

图书存储信息表(bookstorage)

bookbarcode bookid bookintime bookstatus
132782 TP39/1712 2009-08-10 00:00:00 在馆
132789 TP39/1712 2009-08-10 00:00:00 借出
145234 013452 2008-12-06 00:00:00 借出
145321 TP/3452 2007-11-04 00:00:00 借出
156833 TH/2345 2009-12-04 00:00:00 借出
345214 R/345677 2008-11-03 00:00:00 在馆
insert into bookstorage values
	('132782','TP39/1712','2009-8-10','在馆'),
	('132789','TP39/1712','2009-8-10','借出'),
	('145234','013452','2008-12-6','借出'),
	('145321','TP/3452','2007-11-4','借出'),
	('156833','TH/2345','2009-12-4','借出'),
	('345214','R/345677','2008-11-3','在馆');
复制代码

读者类别表(readertype)

retypeid typename borrowquantity borrowday
1 学生 10 30
2 教师 20 60
3 管理员 15 30
4 职工 15 20
insert into readertype values
	(1,'学生',10,30),
	(2,'教师',20,60),
	(3,'管理员',15,30),
	(4,'职工',15,20);
复制代码

读者信息表(reader)

readerid readername readerpass retypeid readerdate readerstatus
0016 苏小东 123456 1 1999-09-09 00:00:00 有效
0017 张明 123456 1 2010-09-10 00:00:00 有效
0018 梁君红 123456 1 2010-09-10 00:00:00 有效
0021 赵清远 123456 2 2010-07-01 00:00:00 有效
0034 李瑞清 123456 3 2009-08-03 00:00:00 有效
0042 张明月 123456 4 1997-04-23 00:00:00 有效
insert into reader values
	('0016','苏小东','123456',1,'1999-9-9','有效'),
	('0017','张明','123456',1,'2010-9-10','有效'),
	('0018','梁君红','123456',1,'2010-9-10','有效'),
	('0021','赵清远','123456',2,'2010-7-1','有效'),
	('0034','李瑞清','123456',3,'2009-8-3','有效'),
	('0042','张明月','123456',4,'1997-4-23','有效');
复制代码

图书借阅表(bookborrow)

borrowid bookbarcode readerid borrowtime returntime borrowstatus
001328 132789 0017 2011-01-24 00:00:00 2011-02-28 00:00:00 已还
001356 145234 0018 2011-02-12 00:00:00 2011-02-27 00:00:00 已还
001432 132782 0016 2011-03-04 00:00:00 2011-04-05 00:00:00 已还
001435 145321 0021 2011-08-09 00:00:00 2011-09-02 00:00:00 已还
001578 156833 0034 2011-10-01 00:00:00 2011-11-01 00:00:00 未还
001679 345214 0042 2011-02-21 00:00:00 2011-03-05 00:00:00 未还
insert into bookborrow values
	('001432','132782','0016','2011-3-4','2011-4-5','已还'),
	('001328','132789','0017','2011-1-24','2011-2-28','已还'),
	('001356','145234','0018','2011-2-12','2011-2-27','已还'),
	('001435','145321','0021','2011-8-9','2011-9-2','已还'),
	('001578','156833','0034','2011-10-1','2011-11-1','未还'),
	('001679','345214','0042','2011-2-21','2011-3-5','未还');
复制代码

建立数据库stucourse

没有表结构, 根据数据, 本身决定使用哪一种数据类型mysql

create database stucourse;
use stucourse;
复制代码

学生表(student)

sid sname sex age dept
1001 宋江 25 计算机系
3002 张明 23 生物系
1003 李小鹏 26 计算机系
1004 郑冬 25 计算机系
4005 李晓红 27 工商管理
5006 赵紫月 24 外语系
create table student(
	sid char(10) not null primary key,
	sname varchar(20) not null,
	sex char(1) not null,
	age tinyint(2) not null,
	dept varchar(20) not null
);
复制代码
insert into student values('1001','宋江','男',25,'计算机系');
insert into student values('3002','张明','男',23,'生物系');
insert into student values('1003','李小鹏','男',26,'计算机系');
insert into student values('1004','郑东','女',25,'计算机系');
insert into student values('4005','李小红','女',27,'工商管理');
insert into student values('5006','赵紫月','女',24,'外语系');
复制代码

教师表(teacher)

tid tname title salary dept cid
3102 李明 初级 2500 计算机系 C1
3108 黄小明 初级 4000 生物系 C3
4105 张小红 中级 3500 工商管理 C2
5102 宋力跃 高级 3500 物理系 C4
3106 赵明阳 初级 1500 地理系 C2
7108 张丽 高级 3500 生物系 C3
9103 王彬 高级 3500 计算机系 C1
7101 王力号 初级 1800 生物系 C1
create table teacher(
	tid char(10) not null primary key,
	tname varchar(20) not null,
	title char(2) not null,
	salary int(11) not null,
	dept varchar(20) not null,
	cid char(2) not null,
	FOREIGN key(cid) REFERENCES courseinfo(cid)
);
复制代码
insert into teacher values('3102','李明','初级',2500,'计算机系','C1');
insert into teacher values('3108','黄小明','初级',4000,'生物系','C3');
insert into teacher values('4105','张小红','中级',3500,'工商管理','C2');
insert into teacher values('5102','宋力月','高级',1500,'物理系','C4');
insert into teacher values('3106','赵明阳','初级',3500,'地理系','C2');
insert into teacher values('7108','张丽','高级',3500,'生物系','C3');
insert into teacher values('9103','王彬','高级',3500,'计算机系','C1');
insert into teacher values('7101','王力号','初级',1800,'生物系','C1');
复制代码

课程表(courseinfo)

cid cname cbook ctest dept
C1 计算机基础 b1231 2009-4-6 计算机系
C2 工商管理基础 b1232 2009-7-16 工商管理
C3 生物科学 b1233 2010-3-6 生物系
C4 大学物理 b1234 2009-4-26 物理系
C5 数据库原理 b1235 2010-2-6 计算机系
create table courseinfo(
	cid char(2) not null PRIMARY key,
	cname varchar(20) not null,
	cbook char(5) not null,
	ctest date not null,
	dept varchar(20)
);
复制代码
insert into courseinfo values('C1','计算机基础','b1231','2009-4-6','计算机系');
insert into courseinfo values('C2','工商管理基础','b1232','2009-7-16','工商管理');
insert into courseinfo values('C3','生物科学','b1233','2010-3-6','生物系');
insert into courseinfo values('C4','大学物理','b1234','2009-4-26','物理系');
insert into courseinfo values('C5','数据库原理','b1235','2010-2-6','计算机系');
复制代码

选课表(scourse)

sid score cid tid
1001 87 C1 3102
1001 77 C2 4105
1001 63 C3 3108
1001 56 C4 5102
3002 78 C3 3108
3002 78 C4 5102
1003 89 C1 9103
1004 56 C2 3106
4005 87 C4 5102
5006 null C1 7101
create table scourse(
	sid char(4) not null,
	score int,
	cid char(2) not null,
	tid char(10)
);
复制代码
insert into scourse values('1001',87,'C1','3102');
insert into scourse values('1001',77,'C2','4105');
insert into scourse values('1001',63,'C3','3108');
insert into scourse values('1001',56,'C4','5102');
insert into scourse values('3002',78,'C3','3108');
insert into scourse values('3002',78,'C4','5102');
insert into scourse values('1003',89,'C1','9103');
insert into scourse values('1004',56,'C2','3106');
insert into scourse values('4005',87,'C4','5102');
insert into scourse values('5006',null,'C1','7101');
复制代码

教材表(bookinfo)

bid bname bpublish bprice quantity
b1231 Image Processing 人民大学出版社 34.56 8
b1212 Signal Processing 清华大学出版社 51.75 10
b1233 Digital Signal Processing 邮电出版社 48.5 11
b1234 The Logic Circuit 北大出版社 49.2 40
b1235 SQL Techniques 邮电出版社 65.4 20
create table bookinfo(
	bid char(5) not null PRIMARY key,
	bname varchar(50) not null,
	bpublish varchar(20) not null,
	bprice double not null,
	quantity int
);
复制代码
insert into bookinfo values('b1231', 'Image Processing', '人民大学出版社', 34.56, 8);
insert into bookinfo values('b1232', 'Signal Processing', '清华大学出版社', 51.75, 10);
insert into bookinfo values('b1233', 'Digital Signal Processing', '邮电出版社', 48.5, 11);
insert into bookinfo values('b1234', 'The Logic Circuit', '北大出版社', 49.2, 40);
insert into bookinfo values('b1235', 'SQL Techniques', '邮电出版社', 65.4, 20);
复制代码

快速跳转

相关文章
相关标签/搜索