?
-- 创建数据库
create database uclass default charset "UTF8";
-- 指定数据库
use uclass;
-- 创建数据表
create table user(
id int primary key auto_increment comment '用户编号',
username varchar(50) not null comment '账号',
password varchar(50) not null comment '密码',
nickname varchar(100) comment '昵称',
age int default 18 comment '年龄',
gender char(5) default '男' comment '性别'
);
create table courseType(
id int primary key auto_increment comment '课程编号',
name varchar(50) not null comment '类型名称',
intro text comment '课程类型描述'
);
create table course(
id int primary key auto_increment comment '课程编号',
name varchar(50) comment '课程名称',
price double comment '课程售价',
sales int comment '销售量',
ctypes int comment '所属类型'
);
-- 测试数据
insert into user(username, password, nickname)
values('tom', '123', '汤姆'),
('jerry', '123', '杰瑞'),
('shuke', '123', '舒克'),
('beita', '123', '贝塔'),
('peiqi', '123', '佩奇'),
('qiaozhi', '123', '乔治');
insert into courseType(name)
values('前端'),('JAVA WEB'),('人工智能');
insert into course(name, price, sales, ctypes)
values('HTML/css', 1000, 20000, 1),
('JavaScript', 1400, 20000, 1),
('NodeJS', 2000, 10000, 1),
('Express', 2200, 20000, 1),
('VueJS', 4000, 20000, 1),
('React', 3000, 20000, 1),
('小程序', 800, 20000, 1),
('Java SE', 1000, 20000, 2),
('Java EE', 1000, 20000, 2),
('JSP/Servlet', 1200, 20000, 2),
('Mybatis', 3000, 20000, 2),
('Spring', 4000, 20000, 2),
('SpringMVC', 3000, 20000, 2),
('Hibernate', 1200, 20000, 2),
('Struts2.x', 500, 20000, 2),
('SpringBoot', 4500, 20000, 2),
('SpringCloud', 8000, 20000, 2),
('Nacos', 6000, 20000, 2),
('Zookeeper', 600, 20000, 2),
('Dubbo', 1600, 20000, 2),
('Python', 1000, 20000, 3),
('Django', 1200, 20000, 3),
('Flask', 1000, 20000, 3),
('数据分析', 1500, 20000, 3),
('数据爬虫', 1500, 20000, 3),
('深度学习', 6000, 20000, 3),
('机器学习', 6000, 20000, 3);
?-- 全表查询:查询一张表中的所有数据
select * from user;
-- 用户注册:判断某个账号是否已经被使用
select * from user where username = 'tom';
-- 用户登录:判断账号密码是否正确
select * from user where username = "tom" and password = "123";
-- 查询前端的所有课程
select * from courseType where name = "前端";
select * from course where ctypes = 1;
-- 改造,关联查询
select * from courseType ct, course c
where ct.id = c.ctypes and ct.name = "前端";
-- 改造,连接查询
-- 左连接
select * from courseType ct left join course c
on ct.id = c.ctypes where ct.name = "前端";
-- 右连接
select * from courseType ct right join course c
on ct.id = c.ctypes where ct.name = "前端";
-- 查询课程售价小于1000 的所有课程
select * from course where price < 1000;
-- 查询课程售价大于1500 小于4000的课程
select * from course where price > 1500 and price < 4000;
-- 查询课程售价在2000~4000之间的java课程
select * from courseType where name = "JAVA WEB"
select * from course where ctypes = 2 and price > 2000 and price < 4000;
-- 改造:关联查询
select * from courseType, course
where courseType.id = course.ctypes
and courseType.name = "JAVA WEB"
and course.price > 2000
and course.price < 4000;
-- 不知道具体课程名称,查询智能相关的课程
-- 模糊查询:%匹配0~n个任意字符,_匹配单个字符
select * from courseType where name like '%智能%';
-- 查询部分课程信息
select * from course where name = "NodeJS" or name = "Express";
-- 改造:in查询
select * from course where name in ('NodeJS', 'Express');
-- 查询课程销售量在10000~20000的课程
select * from course where sales >= 10000 and sales <= 20000;
-- 改造:between..and
select * from course where sales between 10000 and 20000;