前言
数据库名称可以为【schoolID8】,字符集【utf8】,排序规则【utf8_general_ci】。
1、建表语句——DDL
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createDate` datetime DEFAULT NULL,
`userName` varchar(20) DEFAULT NULL,
`pwd` varchar(36) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`age` tinyint(3) DEFAULT NULL,
`sex` char(2) DEFAULT NULL,
`introduce` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2、插入语句——DML
insert into student values(
0,'2024-02-25 10:00:00','刘德华','123456','15612345678','62','男','永远的男神。');
insert into student values(
0,'2024-02-25 10:00:00','刘青云','123456','15612345678','65','男','真正的好演员。');
insert into student values(
0,'2024-02-25 10:00:00','周星驰','123456','15612345678','61','男','所有的电影都没有对爱情轻视。');
insert into student values(
0,'2024-02-25 10:00:00','张翰','123456','15612345678','32','男','流星雨。');
insert into student values(
0,'2024-02-25 10:00:00','王祖贤','123456','15612345678','27','女','女神。');
insert into student (userName,age,introduce) values ('刘亦菲',37,'神仙姐姐');
3、基础查询语句——DQL
#1、基础查询
select * from student;
#2、分列匿名以及筛选数据查询
select userName as '姓名',age 年龄 ,sex'性别',introduce'简介'
from student
where pwd is not null;
#3、去重查询
select distinct sex '性别类型'from student;
#4、排序查询
select userName as '姓名',age 年龄,introduce'简介'from student ORDER BY age desc;
#5、分页查询·limit这是个重载函数
#1个参数的limit用法是显示多少条信息
select * from student LIMIT 2;
#2个参数,参数1:从低第N条开始查询,N的起始坐标为0条。参数2:查询条数
select * from student limit 4,2;
查询语句
select * from `student` where userName like '张_';
select * from `student` where userName like '刘%';
select * from `student` where pwd is null;
select * from `student` where pwd is not null;
select * from `student` where age between 30 and 40;
select * from `student` where createDate between '2024-02-21 00:00:00' and '2024-02-23 00:00:00';
#
select * from `student` where userName in('张翰','刘德华','刘亦菲');