数据库名称可以为【schoolDB】,字符集【utf8】,排序训练【utf8_general_cl】。
1、建表语句——DDL
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
`createDate` datetime DEFAULT NULL COMMENT '创建时间',
`userName` varchar(20) DEFAULT NULL COMMENT '用户名',
`pwd` varchar(36) DEFAULT NULL COMMENT '密码',
`phone` varchar(11) DEFAULT NULL COMMENT '手机号',
`age` tinyint(3) DEFAULT NULL COMMENT '年龄',
`sex` char(2) DEFAULT '男' COMMENT '性别',
`introduce` varchar(255) DEFAULT NULL COMMENT '简介',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2、插入语句——DML
INSERT INTO student VALUES(0,'2024-2-25 10:00:00','刘德华','123654',
'15612345678',62,'男','永远的男神。');
INSERT INTO student VALUES(0,'2024-2-25 10:00:00','刘青云','123154',
'15612345678',65,'男','真正的演员。');
INSERT INTO student VALUES(0,'2024-2-25 10:00:00','周星驰','123624',
'15612345678',61,'男','所有的电影。');
INSERT INTO student VALUES(0,'2024-2-25 10:00:00','张翰','122654',
'15612345678',32,'男','流星雨下。');
INSERT INTO student VALUES(0,'2024-2-25 10:00:00','王祖贤','223654',
'15612345678',27,'女','法典化。');
INSERT INTO student (userName,age,introduce) VALUES('刘亦菲',37,'神仙姐姐');
3、查询语句——DQL
# 5个常用的聚合函数 count(x)计数的 max(x)min(x)最大值最小值 avg(x)求平均数,sum(x)求和
# 计数
SELECT count(*) FROM student WHERE pwd is not null;
# 最大最小值
SELECT max(age) '最大年龄',min(age) '最小年龄' FROM student;
#平均数
SELECT avg(age) '平均年龄' FROM student;
#求和
SELECT sum(age) '总年龄' FROM student;
#平均数对照
SELECT avg(age) '平均年龄',sum(age)/(SELECT count(*) FROM student) 'sum计算平均年龄' FROM student;