1.DDL
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
`createDate` datetime DEFAULT NULL,
`userName` varchar(20) DEFAULT NULL,
`pwd` varchar(36) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`age` tinyint(3) unsigned DEFAULT NULL,
`sex` char(2) DEFAULT '男',
`introduce` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/2401_83963218/article/details/137113259
2、插入学生信息DML
insert into student values(0,'2024-02-25 10:10:10','赵灵儿','123',
'15612345678',16,'女','逍遥哥哥,你终于找到我了。');
insert into student values(0,'2024-02-25 10:10:10','王语嫣','123',
'15612345678',17,'女','慕容复,我和你不共戴天。');
insert into student values(0,'2024-02-25 10:10:10','龙姑娘','123',
'15612345678',22,'女','我想过过过儿过过的日子。');
insert into student values(0,'2024-02-25 10:10:10','杨过','123',
'15612345678',18,'男','一遇杨过误终身。');
insert into student values(0,'2024-02-25 10:10:10','杨逍','123',
'15612345678',27,'男','杨过跟程英的大儿子。');
insert into student (userName,age,introduce)values('黄衣女子',26,'杨过与龙姑娘的大女儿。');
3、基础查询测试DQL
# 基础查询
select * from student;
# 筛选列(匿名)与筛选数据行
select userName as 姓名,age '年龄',sex '性别',introduce '简介'
from student
where pwd is not null;
# 分页 limit·有两个重载函数常用,
# 1、一个参数为显示条数
# 2、一个参数为从那条开始查询(下标从0开始计算),零一参数为显示条数
select * from student limit 3,2;# 从4(0-3)开始查询查2条。
# 排序,order by根据数值类型进行排序会比较准确。
select * from student order by age desc limit 3;
# 去重语句distinct
select distinct sex '性别类型' from student;
#常用的五个聚合函数 count(x)计数器,max(x)最大值min(x)最小值,avg(x)平均数,sum(x)求和
select count(*)from student where pwd is not null;
#max 与min
select max(age)'最大年龄',min(age)'最小年龄'from student;
#平均年龄
select avg(age) '平均年龄'from student;
#年龄总和
select sum(age)'年龄总和'from student;
#利用sum计算平均数
select avg(age) '平均年龄',sum(age)/(select count(*)from student )'sum的平均年龄'from student;