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

创建数据库library

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

创建数据表

图书类别表(booktype)

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

图书信息表(book)

序号属性名称含义数据类型是否为空备注
1bookid图书编号char(10)not null主键
2bookname图书名称varchar(20)not null
3typeid类别编号intnull外键
4bookauthor图书作者varchar(20)null
5bookpublisher出版社varchar(50)null
6bookprice图书价格doubluenull
7borrowsum借阅次数intnull
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)

序号属性名称含义数据类型是否为空备注
1bookbarcode图书条码char(20)not null主键
2bookid图书编号char(10)not null外键
3bookintime图书入馆时间datetimenull
4bookstatus图书状态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)

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

读者信息表(reader)

序号属性名称含义数据类型是否为空备注
1readerid读者编号char(10)not null主键
2readername读者姓名varchar(20)not null
3readerpass读者密码varchar(20)not null
4retypeid类别编号intnull外键
5readerdate发证日期datetimenull
6readerstatus借书证状态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)

序号属性名称含义数据类型是否为空备注
1borrowid借阅号char(10)not null主键
2bookbarcode图书条码char(20)not null外键
3readerid读者编号char(10)not null外键
4borrowtime借书日期datetimenull
5returntime还书日期datetimenull
6borrowstatus借阅状态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)

typeidtypename
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)

bookidbooknametypeidbookauthorbookpublisherbookpriceborrowsum
TP39/1712Java程序设计3陈永红机械工业出版社35.530
013452离散数学2张小新机械工业出版社45.510
TP/3452JSP程序设计案例3刘城清电子工业出版社42.88
TH/2345机械设计手册7黄明凡人民邮电出版社4010
R/345677中医的故事8李奇德国防工业出版社20.05
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)

bookbarcodebookidbookintimebookstatus
132782TP39/17122009-08-10 00:00:00在馆
132789TP39/17122009-08-10 00:00:00借出
1452340134522008-12-06 00:00:00借出
145321TP/34522007-11-04 00:00:00借出
156833TH/23452009-12-04 00:00:00借出
345214R/3456772008-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)

retypeidtypenameborrowquantityborrowday
1学生1030
2教师2060
3管理员1530
4职工1520
insert into readertype values
	(1,'学生',10,30),
	(2,'教师',20,60),
	(3,'管理员',15,30),
	(4,'职工',15,20);
复制代码

读者信息表(reader)

readeridreadernamereaderpassretypeidreaderdatereaderstatus
0016苏小东12345611999-09-09 00:00:00有效
0017张明12345612010-09-10 00:00:00有效
0018梁君红12345612010-09-10 00:00:00有效
0021赵清远12345622010-07-01 00:00:00有效
0034李瑞清12345632009-08-03 00:00:00有效
0042张明月12345641997-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)

borrowidbookbarcodereaderidborrowtimereturntimeborrowstatus
00132813278900172011-01-24 00:00:002011-02-28 00:00:00已还
00135614523400182011-02-12 00:00:002011-02-27 00:00:00已还
00143213278200162011-03-04 00:00:002011-04-05 00:00:00已还
00143514532100212011-08-09 00:00:002011-09-02 00:00:00已还
00157815683300342011-10-01 00:00:002011-11-01 00:00:00未还
00167934521400422011-02-21 00:00:002011-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

没有表结构, 根据数据, 自己决定使用哪种数据类型

create database stucourse;
use stucourse;
复制代码

学生表(student)

sidsnamesexagedept
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)

tidtnametitlesalarydeptcid
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)

cidcnamecbookctestdept
C1计算机基础b12312009-4-6计算机系
C2工商管理基础b12322009-7-16工商管理
C3生物科学b12332010-3-6生物系
C4大学物理b12342009-4-26物理系
C5数据库原理b12352010-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)

sidscorecidtid
100187C13102
100177C24105
100163C33108
100156C45102
300278C33108
300278C45102
100389C19103
100456C23106
400587C45102
5006nullC17101
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)

bidbnamebpublishbpricequantity
b1231Image Processing人民大学出版社34.568
b1212Signal Processing清华大学出版社51.7510
b1233Digital Signal Processing邮电出版社48.511
b1234The Logic Circuit北大出版社49.240
b1235SQL Techniques邮电出版社65.420
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);
复制代码

快速跳转

转载于:https://juejin.im/post/5c9b2f266fb9a070aa5cff9b

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值