提交 0953c5e5 编写于 作者: J jackfrued

更新了数据库部分的文档和代码

上级 c6666f45
......@@ -51,15 +51,15 @@ insert into tb_emp values
(3588, '朱九真', '会计', 5566, 2500, null, 10);
-- 查询薪资最高的员工姓名和工资
-- 查询月薪最高的员工姓名和工资
-- 查询员工的姓名和年薪((月薪+补贴)*12)
-- 查询员工的姓名和年薪((月薪+补贴)*13)
-- 查询有员工的部门的编号和人数
-- 查询所有部门的名称和人数
-- 查询薪资最高的员工(Boss除外)的姓名和工资
-- 查询月薪最高的员工(Boss除外)的姓名和工资
-- 查询薪水超过平均薪水的员工的姓名和工资
......@@ -69,24 +69,4 @@ insert into tb_emp values
-- 查询主管的姓名和职位
-- 查询薪资排名4~6名的员工姓名和工资
-- use hrs;
-- drop procedure if exists sp_avg_sal_by_dept;
-- create procedure sp_avg_sal_by_dept(dno integer, out avg_sal float)
-- begin
-- select avg(sal) into avg_sal from tb_emp where dno=dno;
-- end;
-- call sp_avg_sal_by_dept(10, @avgSal);
-- select @avgSal;
-- 查询月薪排名4~6名的员工姓名和工资
drop database if exists bank;
create database bank default charset utf8;
use bank;
create table tb_account
(
accid char(8) primary key,
accowner varchar(20) not null,
accbalance float not null default 0
);
insert into tb_account values (11223344, '王大锤', 1000);
insert into tb_account values (22334455, '李小龙', 1000);
\ No newline at end of file
drop database if exists booksys;
create database booksys default charset utf8;
use booksys;
create table tb_book
(
bookid integer not null,
isbn char(13) not null,
bname varchar(100) not null,
price decimal(8,2) not null,
author varchar(100) not null,
publisher varchar(50) not null,
pubdate date,
intro varchar(500),
lended bit default 0,
counter integer default 0,
primary key (bookid)
);
create table tb_reader
(
readerid integer not null,
rname varchar(20) not null,
gender bit not null,
tel char(11) not null,
birth date,
regdate date not null,
available bit default 1,
primary key (readerid)
);
create table tb_record
(
recordid integer not null auto_increment,
bid integer not null,
rid integer not null,
lenddate datetime not null,
backdate datetime,
pulishment decimal(6,2),
primary key (recordid)
);
alter table tb_record add constraint fk_record_bid foreign key (bid) references tb_book (bookid) on update cascade;
alter table tb_record add constraint fk_record_rid foreign key (rid) references tb_reader (readerid) on update cascade;
\ No newline at end of file
drop database if exists fangtx;
create database fangtx default charset utf8 collate utf8_bin;
use fangtx;
/* 创建用户表 */
create table `tb_user`
(
`userid` int auto_increment comment '编号',
`username` varchar(20) not null comment '用户名',
`password` char(32) not null comment '用户口令',
`realname` varchar(20) not null comment '真实姓名',
`sex` bool default 1 comment '性别',
`tel` varchar(20) not null comment '手机号',
`email` varchar(255) default '' comment '邮箱',
`regdate` datetime default now() comment '注册日期',
`point` int default 0 comment '积分',
`lastvisit` datetime default now() comment '最后访问时间',
`is_authenticated` bit default 0 comment '是否认证',
primary key (`userid`)
);
/* 创建地区表 */
create table `tb_district`
(
`distid` int not null comment '编号',
`pid` int comment '父级行政单位',
`name` varchar(255) not null comment '名称',
`ishot` bool default 0 comment '是否为热门城市',
`intro` varchar(255) default '' comment '介绍',
primary key (distid)
);
/* 创建经理人表 */
create table `tb_agent`
(
`agentid` int not null auto_increment comment '编号',
`name` varchar(255) not null comment '姓名',
`tel` varchar(20) not null comment '电话',
`servstar` int not null default 0 comment '满意度星级',
`realstar` int not null default 0 comment '真实度星级',
`profstar` int not null default 0 comment '专业度星级',
`certificated` bool not null default 0 comment '是否持有专业认证',
primary key (`agentid`)
);
/* 创建用户登录日志表 */
create table `tb_login_log`
(
`logid` bigint auto_increment comment '编号',
`userid` int not null comment '用户',
`ipaddr` varchar(255) not null comment 'IP地址',
`logdate` datetime default now() comment '登录时间日期',
`devcode` varchar(255) default '' comment '设备代码',
primary key (`logid`)
);
/* 创建楼盘表 */
create table `tb_estate`
(
`estateid` int not null auto_increment comment '编号',
`distid` int not null comment '所在三级行政区域',
`name` varchar(255) not null comment '名称',
`hot` int default 0 comment '热度',
`intro` varchar(511) default '' comment '介绍',
primary key (`estateid`)
);
/* 创建经理人楼盘中间表 */
create table `tb_agent_estate`
(
`agent_estate_id` int not null auto_increment comment '编号',
`agentid` int not null comment '经理人',
`estateid` int not null comment '楼盘',
primary key (`agent_estate_id`)
);
/* 创建户型表 */
create table `tb_house_type`
(
`typeid` int comment '编号',
`name` varchar(255) not null comment '名称',
primary key (`typeid`)
);
/* 创建房源信息表 */
create table `tb_house_info`
(
`houseid` int not null auto_increment comment '编号',
`title` varchar(50) not null comment '标题',
`area` int not null comment '面积',
`floor` int not null comment '楼层',
`totalfloor` int not null comment '总楼层',
`direction` varchar(10) not null comment '朝向',
`price` int not null comment '价格',
`priceunit` varchar(10) not null comment '价格单位',
`detail` varchar(511) default '' comment '详情',
`mainphoto` varchar(255) not null comment '主图',
`pubdate` date not null comment '发布日期',
`street` varchar(255) not null comment '街道',
`hassubway` bool default 0 comment '是否有地铁',
`isshared` bool default 0 comment '是否支持合租',
`hasagentfees` bool default 0 comment '是否有中介费',
`typeid` int not null comment '户型',
`userid` int not null comment '发布用户',
`distid2` int not null comment '所在二级行政区域',
`distid3` int not null comment '所在三级行政区域',
`estateid` int comment '楼盘',
`agentid` int comment '经理人',
primary key (`houseid`)
);
/* 创建房源照片表 */
create table `tb_house_photo`
(
`photoid` int not null auto_increment comment '编号',
`houseid` int not null comment '房源',
`path` varchar(255) not null comment '资源路径',
primary key (`photoid`)
);
/* 创建标签表 */
create table `tb_tag`
(
`tagid` int auto_increment comment '编号',
`content` varchar(20) not null comment '内容',
primary key (`tagid`)
);
/* 创建房源标签中间表 */
create table `tb_house_tag`
(
`house_tag_id` int auto_increment comment '编号',
`houseid` int not null comment '房源',
`tagid` int not null comment '标签',
primary key (`house_tag_id`)
);
/* 创建用户浏览历史记录表 */
create table `tb_record`
(
`recordid` bigint auto_increment comment '编号',
`userid` int not null comment '用户',
`houseid` int not null comment '房源',
`recorddate` datetime not null comment '浏览时间日期',
primary key (`recordid`)
);
/* 创建用户令牌表 */
create table `tb_user_token`
(
`tokenid` int auto_increment comment '编号',
`token` char(32) not null comment '令牌',
`userid` int not null comment '用户',
primary key (`tokenid`)
);
/* 创建角色表 */
create table `tb_role`
(
`roleid` int auto_increment comment '编号',
`rolename` varchar(255) not null comment '角色名',
primary key (`roleid`)
);
/* 创建权限表 */
create table `tb_privilege`
(
`privid` int auto_increment comment '编号',
`method` varchar(15) not null comment '请求方法',
`url` varchar(1024) not null comment '资源的URL',
PRIMARY KEY (`privid`)
);
/* 创建用户角色中间表 */
create table `tb_user_role`
(
`urid` int auto_increment comment '编号',
`userid` int not null comment '用户',
`roleid` int not null comment '角色',
primary key (`urid`)
);
/* 创建角色权限中间表 */
create table `tb_role_privilege`
(
`rpid` int auto_increment comment '编号',
`roleid` int not null comment '角色',
`privid` int not null comment '权限',
primary key (`rpid`)
);
create unique index `uni_idx_agent_estate` on `tb_agent_estate` (`agentid`, `estateid`);
create unique index `uni_idx_record` on `tb_record` (`userid`, `houseid`);
create unique index `uni_idx_userid` on `tb_user_token` (`userid`);
create unique index `uni_idx_username` on `tb_user` (`username`);
create unique index `uni_idx_tel` on `tb_user` (`tel`);
create unique index `uni_idx_email` on `tb_user` (`email`);
create unique index `uni_idx_house_tag` on `tb_house_tag` (`houseid`, `tagid`);
alter table `tb_agent_estate` add constraint `fk_agent_estate_agentid` foreign key (`agentid`) references `tb_agent` (`agentid`);
alter table `tb_agent_estate` add constraint `fk_agent_estate_estateid` foreign key (`estateid`) references `tb_estate` (`estateid`);
alter table `tb_district` add constraint `fk_district_pid` foreign key (`pid`) references `tb_district` (`distid`);
alter table `tb_estate` add constraint `fk_estate_distid` foreign key (`distid`) references `tb_district` (`distid`);
alter table `tb_house_info` add constraint `fk_house_info_agentid` foreign key (`agentid`) references tb_agent (`agentid`);
alter table `tb_house_info` add constraint `fk_house_info_distid2` foreign key (`distid2`) references tb_district (`distid`);
alter table `tb_house_info` add constraint `fk_house_info_distid3` foreign key (`distid3`) references tb_district (`distid`);
alter table `tb_house_info` add constraint `fk_house_info_estateid` foreign key (`estateid`) references tb_estate (`estateid`);
alter table `tb_house_info` add constraint `fk_house_info_typeid` foreign key (`typeid`) references tb_house_type (`typeid`);
alter table `tb_house_info` add constraint `fk_house_info_userid` foreign key (`userid`) references tb_user (`userid`);
alter table `tb_house_photo` add constraint `fk_house_photo_houseid` foreign key (`houseid`) references `tb_house_info` (`houseid`);
alter table `tb_house_tag` add constraint `fk_house_tag_houseid` foreign key (`houseid`) references `tb_house_info` (`houseid`);
alter table `tb_house_tag` add constraint `fk_house_tag_tagid` foreign key (`tagid`) references `tb_tag` (`tagid`);
alter table `tb_login_log` add constraint `fk_login_log_userid` foreign key (`userid`) references `tb_user` (`userid`);
alter table `tb_record` add constraint `fk_record_houseid` foreign key (`houseid`) references `tb_house_info` (`houseid`);
alter table `tb_record` add constraint `fk_record_userid` foreign key (`userid`) references `tb_user` (`userid`);
alter table `tb_user_token` add constraint `fk_token_userid` foreign key (`userid`) references `tb_user` (`userid`);
alter table `tb_user_role` add constraint `uni_user_role` unique (`userid`, `roleid`);
alter table `tb_role_privilege` add constraint `uni_role_priv` unique (`roleid`, `privid`);
alter table `tb_role_privilege` add constraint `fk_role_privilege_privid` foreign key (`privid`) references `tb_privilege` (`privid`);
alter table `tb_role_privilege` add constraint `fk_role_privilege_roleid` foreign key (`roleid`) references `tb_role` (`roleid`);
alter table `tb_user_role` add constraint `fk_user_role_roleid` foreign key (`roleid`) references `tb_role` (`roleid`);
alter table `tb_user_role` add constraint `fk_user_role_userid` foreign key (`userid`) references `tb_user` (`userid`);
此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -350,16 +350,14 @@
select stuname from tb_student where stuaddr is not null;
-- 查询学生选课的所有日期(去重)
select distinct scdate from tb_score;
select distinct seldate from tb_record;
-- 查询学生的家庭住址(去重)
select distinct stuaddr from tb_student where stuaddr is not null;
-- 查询男学生的姓名和生日按年龄从大到小排列(排序)
-- asc (ascending) - 升序(从小到大)/ desc (descending) - 降序(从大到小)
select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stusex=1 order by 年龄 desc;
select stuname as 姓名, datediff(curdate(), stubirth) div 365 as 年龄 from tb_student where stusex=1 order by 年龄 desc;
-- 聚合函数:max / min / count / sum / avg
-- 查询年龄最大的学生的出生日期(聚合函数)
select min(stubirth) from tb_student;
......@@ -370,51 +368,42 @@
select stusex, count(*) from tb_student group by stusex;
-- 查询课程编号为1111的课程的平均成绩(筛选和聚合函数)
select avg(scmark) from tb_score where couid=1111;
select avg(score) from tb_record where cid=1111;
-- 查询学号为1001的学生所有课程的平均分(筛选和聚合函数)
select avg(scmark) from tb_score where stuid=1001;
select avg(score) from tb_record where sid=1001;
-- 查询每个学生的学号和平均成绩(分组和聚合函数)
select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid;
select sid as 学号, avg(score) as 平均分 from tb_record group by sid;
-- 查询平均成绩大于等于90分的学生的学号和平均成绩
-- 分组以前的筛选使用where子句 / 分组以后的筛选使用having子句
select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid having 平均分>=90;
select sid as 学号, avg(score) as 平均分 from tb_record group by sid having 平均分>=90;
-- 查询年龄最大的学生的姓名(子查询/嵌套的查询)
select stuname from tb_student where stubirth=(
select min(stubirth) from tb_student
);
select stuname from tb_student where stubirth=( select min(stubirth) from tb_student );
-- 查询年龄最大的学生姓名和年龄(子查询+运算)
select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stubirth=(
select min(stubirth) from tb_student
);
select stuname as 姓名, datediff(curdate(), stubirth) div 365 as 年龄 from tb_student where stubirth=( select min(stubirth) from tb_student );
-- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
select stuname from tb_student where stuid in (
select stuid from tb_score group by stuid having count(stuid)>2
)
select stuname from tb_student where stuid in ( select stuid from tb_record group by stuid having count(stuid)>2 );
-- 查询学生姓名、课程名称以及成绩(连接查询)
select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid and scmark is not null;
select stuname, couname, score from tb_student t1, tb_course t2, tb_record t3 where stuid=sid and couid=cid and score is not null;
-- 查询学生姓名、课程名称以及成绩按成绩从高到低查询第11-15条记录(内连接+分页)
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 10;
select stuname, couname, score from tb_student inner join tb_record on stuid=sid inner join tb_course on couid=cid where score is not null order by score desc limit 5 offset 10;
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 10, 5;
select stuname, couname, score from tb_student inner join tb_record on stuid=sid inner join tb_course on couid=cid where score is not null order by score desc limit 10, 5;
-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
select stuname, avgmark from tb_student t1, (select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 where t1.stuid=t2.stuid;
select stuname, avgmark from tb_student, ( select sid, avg(score) as avgmark from tb_record group by sid ) temp where stuid=sid;
select stuname, avgmark from tb_student t1 inner join
(select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 on t1.stuid=t2.stuid;
select stuname, avgmark from tb_student inner join ( select sid, avg(score) as avgmark from tb_record group by sid ) temp on stuid=sid;
-- 内连接(inner join)- 只有满足连接条件的记录才会被查出来
-- 外连接(outer join)- 左外连接(left outer join) / 右外连接(right outer join) / 全外连接
-- 查询每个学生的姓名和选课数量(左外连接和子查询)
select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid;
select stuname, ifnull(total, 0) from tb_student left outer join ( select sid, count(sid) as total from tb_record group by sid ) temp on stuid=sid;
```
4. DCL
......
## 日志和调试
在项目开发阶段,显示足够的调试信息以辅助开发人员调试代码还是非常必要的.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册