一、使用SQLyog创建数据库用来管理学生信息
#创建数据库student
DROP DATABASE IF EXISTS Myschool;
CREATE DATABASE Myschool; #在数据库中新建四张数据表
USE Myschool; #创建年级表
CREATE TABLE grade(
gradeID INT(4) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '年级编号',
gradeName VARCHAR(50) NOT NULL COMMENT '年级名称'
)COMMENT="年级表"; #创建学生信息表
CREATE TABLE student(
studentNo INT(4) NOT NULL PRIMARY KEY COMMENT '学号',
loginPwd VARCHAR(20) NOT NULL DEFAULT '' COMMENT '密码',
studentName VARCHAR(50) NOT NULL COMMENT '姓名',
sex CHAR(2) NOT NULL DEFAULT '男' COMMENT '性别',
gradeID INT(4) UNSIGNED COMMENT '年级编号',
phone VARCHAR(50) COMMENT '电话',
address VARCHAR(255) DEFAULT '地址不详' COMMENT '地址',
bornDate DATETIME DEFAULT NOW() COMMENT '出生日期',
email VARCHAR(50) DEFAULT 'xx@163.com' COMMENT '邮件账号',
identityCard VARCHAR(18) COMMENT '身份证号'
)COMMENT="学生信息表"; #创建科目表
CREATE TABLE `subject`(
subjectNo INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '课程编号',
subjectName VARCHAR(50) COMMENT '课程名称',
classHour INT(4) COMMENT '学时',
gradeID INT(4) UNSIGNED COMMENT '年级编号'
)COMMENT="科目表"; #创建成绩表
CREATE TABLE result(
studentNo INT(4) NOT NULL COMMENT '学号',
subjectNo INT(4) NOT NULL COMMENT '课程编号',
examDate DATETIME DEFAULT NOW() NOT NULL COMMENT '考试日期',
studentResult INT(4) NOT NULL COMMENT '考试成绩'
)COMMENT="成绩表"; #为表添加外键(关系) #在student表中添加gradeID外键
ALTER TABLE student
ADD CONSTRAINT fk_student_grade FOREIGN KEY(gradeID)
REFERENCES grade(gradeID); #在subject表中添加gradeID外键
ALTER TABLE `subject`
ADD CONSTRAINT fk_subject_grade FOREIGN KEY(gradeID)
REFERENCES grade(gradeID); #在result表中添加studentNo,subjectNo外键
ALTER TABLE result
ADD CONSTRAINT fk_result_student FOREIGN KEY(studentNo)
REFERENCES student(studentNo); ALTER TABLE result
ADD CONSTRAINT fk_result_subject FOREIGN KEY(subjectNo)
REFERENCES `subject`(subjectNo); #为表添加初始数据 #年级表grade的数据
INSERT INTO grade(gradeID,gradeName)
VALUES(1,'一年级'),(2,'二年级'),(3,'三年级'),(4,'四年级'),(5,'五年级'); #科目表subject的数据
INSERT INTO `subject`(subjectNo,subjectName,classHour,gradeID)
VALUES(1,'Logic Java',220,1),(2,'HTML',160,1),(3,'Java OOP',230,2); #学生信息表student的数据
INSERT INTO student(studentNo,studentName,sex,gradeID,phone,address,bornDate)
VALUES(10000,'郭靖','男',1,13645667783,'天津市河西区','1990-09-08'),
(10001,'李文才','男',1,13645667890,'地址不详','1994-04-12'),
(10002,'李斯文','男',1,13645556793,'河南洛阳','1993-07-23'),
(10003,'张萍','女',1,13642345112,'地址不详','1995-06-10'),
(10004,'韩秋洁','女',1,13812344566,'北京市海淀区','1995-07-15'),
(10005,'张秋丽','女',1,13567893246,'北京市东城区','1994-01-17'),
(10006,'肖梅','女',1,13563456721,'河北省石家庄市','1991-02-17'),
(10007,'秦洋','男',1,13056434411,'上海市卢湾区','1992-04-18'),
(10008,'何睛睛','女',1,13053445221,'广州市天河区','1997-07-23'),
(20000,'王宝宝','男',2,15076552323,'地址不详','1996-06-05'),
(20010,'何小华','女',2,13318877954,'地址不详','1995-09-10'),
(30011,'陈志强','女',3,13689965430,'地址不详','1994-09-27'),
(30012,'李露露','女',3,13685678854,'地址不详','1992-09-27'); #成绩表result的数据
INSERT INTO result(studentNo,subjectNo,examDate,studentResult)
VALUES(10000,1,'2016-02-15',71),
(10000,1,'2016-02-17',60),
(10001,1,'2016-02-17',46),
(10002,1,'2016-02-17',83),
(10003,1,'2016-02-17',65),
(10004,1,'2016-02-17',70),
(10005,1,'2016-02-17',95),
(10006,1,'2016-02-17',93),
(10007,1,'2016-02-17',23),
(20000,3,'2016-07-17',68),
(20010,3,'2016-07-17',90),
(20000,2,'2016-07-17',88),
(20010,2,'2016-07-17',78); ##学生信息数据库备份##
二、使用SQLyog创建数据库制作图书馆管理系统
#建立一个图书馆管理系统的数据库来存放图书馆的相关信息,包括图书的基本信息、图书借阅的信息和读者的信息 #创建数据库Library
CREATE DATABASE IF NOT EXISTS Library;
USE Library;
#创建实体表
#图书信息表book
CREATE TABLE book(
bookId VARCHAR(50) PRIMARY KEY NOT NULL COMMENT '图书编号',
bookName VARCHAR(255) NOT NULL COMMENT '图书名称',
author VARCHAR(50) COMMENT '作者姓名',
pubComp VARCHAR(50) COMMENT '出版社',
pubDate DATE COMMENT '出版日期',
bookCount INT(4) COMMENT '现存数量',
price DOUBLE COMMENT '单价'
)COMMENT="图书信息表"; #读者信息表reader
CREATE TABLE reader(
readerId VARCHAR(50) PRIMARY KEY NOT NULL COMMENT '读者编号',
readerName VARCHAR(50) NOT NULL COMMENT '读者姓名',
lendNum INT(4) COMMENT '已借书数量',
readerAddress VARCHAR(255) COMMENT '联系地址'
)COMMENT="读者信息表"; #图书借阅表borrow
CREATE TABLE borrow(
readerId VARCHAR(50) NOT NULL COMMENT '读者编号',
bookId VARCHAR(50) NOT NULL COMMENT '图书编号',
lendDate DATETIME DEFAULT NOW() NOT NULL COMMENT '借阅日期',
willDate DATE COMMENT '应归还日期',
returnDate DATE COMMENT '实际归还日期'
)COMMENT="图书借阅表"; #罚款记录表penalty
CREATE TABLE penalty(
readerId VARCHAR(50) NOT NULL COMMENT '读者编号',
bookId VARCHAR(50) NOT NULL COMMENT '图书编号',
pDate DATE NOT NULL COMMENT '罚款日期',
pType INT COMMENT '罚款类型:1-延期,2-损坏,3-丢失',
amount DOUBLE COMMENT '罚款金额'
)COMMENT="罚款记录表"; #删除信息表
USE Library;
DROP TABLE IF EXISTS book;
DROP TABLE IF EXISTS reader;
DROP TABLE IF EXISTS borrow;
DROP TABLE IF EXISTS penalty; #给borrow添加复合主键
ALTER TABLE borrow
ADD CONSTRAINT pk_borrow PRIMARY KEY(readerId,bookId,lendDate); #给penalty添加复合主键
ALTER TABLE penalty
ADD CONSTRAINT pk_penalty PRIMARY KEY(readerId,bookId,pDate); ##删除borrow和penalty的主键
ALTER TABLE borrow
DROP PRIMARY KEY;
ALTER TABLE penalty
DROP PRIMARY KEY;
##删除borrow和penalty的外键
ALTER TABLE borrow
DROP FOREIGN KEY fk_borrow_book;
ALTER TABLE borrow
DROP FOREIGN KEY fk_borrow_reader;
ALTER TABLE penalty
DROP FOREIGN KEY fk_penalty_book;
ALTER TABLE penalty
DROP FOREIGN KEY fk_penalty_reader; #给borrow添加外键
ALTER TABLE borrow
ADD CONSTRAINT fk_borrow_book FOREIGN KEY(bookId)
REFERENCES book(bookId);
ALTER TABLE borrow
ADD CONSTRAINT fk_borrow_reader FOREIGN KEY(readerId)
REFERENCES reader(readerId); #给penalty添加外键
ALTER TABLE penalty
ADD CONSTRAINT fk_penalty_book FOREIGN KEY(bookId)
REFERENCES book(bookId);
ALTER TABLE penalty
ADD CONSTRAINT fk_penalty_reader FOREIGN KEY(readerId)
REFERENCES reader(readerId);