-- 删除表


-- 表不存在时,执行会报错

drop table


-- 表存在时删除,不存在不会报错

drop table if exists


-- 插入一条数据

-- 每一列都有值,按照列的排序顺序填写值

-- 主键列是自动增长,插入时需要占位,通常使用0或者default或者null

-- insert into 表名 values(...)


insert into student values(

0,'李四',18,'一年级四班'

)


-- insert into 表名(字段1,...) values(值1,...)

-- 部分字段设置值,值的顺序与给出的字段的顺序一致

insert into student(sname,class)

values('王五','一年级三班');


-- 批量插入

-- 每个字段都设置值

insert into student values

(0,'赵六',19,'二年级四班'),

(0,'周七',19,'二年级一班'),

(0,'小一',21,'三年级四班')


-- 对应字段设置值

-- insert into 表名(列1,...) values(值1,...),(值1,...)...

insert into student (age,class)values

(19,'二年级四班'),

(19,'二年级一班')


-- 修改数据

-- 格式:update 表名 set

-- where条件,满足时才执行


update student set age = 18,class = '一年级四班';


-- 把id为1的数据中的年龄修改为19

update student set age = 19 where id = 1;


update student set age = 19 where age = 18;


-- 把id为1的数据中的姓名修改为null

update student set sname = null where id = 1;



-- 删除表中数据

-- 格式:delete from 表名 where 条件

delete from student where id = 6;



-- 逻辑删除

-- 1.设计表,给表添加一个字段is_delete(isdel),0代表未删除,1代表删除

-- 2.把所有的数据is_delete改为0

-- 3.要删除某一条数据时,is_delete改为1

-- 4.当要查询数据时,查询is_delete位0的数据

update student set is_delete = 1 where id = 4;


-- 查询


-- 创建数据表

drop table if exists students;

create table students (

studentNo varchar(10) primary key,

name varchar(10),

sex varchar(1),

hometown varchar(20),

age tinyint(4),

class varchar(10),

card varchar(20)

);


-- 准备数据

insert into students values

('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),

('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),

('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),

('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),

('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),

('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),

('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),

('008', '小乔', '女', '河南', '15', '3班', null),

('009', '百里守约', '男', '湖南', '21', '1班', ''),

('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),

('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),

('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655');


-- 查询所有字段

-- select * from

select * from students;



-- 查询指定字段

-- select 列名,列名... from

SELECT name,age from students;


-- 查询每个学生的身份证号信息

select card from students;


-- 起别名  as

-- 别名会展现在结果集上,对原表无影响。


-- 给列起别名


-- select 列名 as 别名,列名... from 表名

select name as '姓名',age as '年龄' from students;


-- as 可以忽略不写

-- select 列名 别名,列名 别名... from 表名

select name '姓名',age  '年龄' from students;


-- 给表起别名

select * from students as s;

select * from students s;

-- 把'.'当成的释义

select s.name,s.age from students as s;



-- distinct 去重

-- 在select后面列前使用distinct可以消除重复的行

-- 注意:distinct只能放在列前面


-- 单一列去重

-- 查询students表中的性别有哪些

select distinct sex from students;


-- 多个列去重 按照每条数据对应列的值的组合情况去重

-- 查询students表中每个班的性别有哪些

select distinct class,sex from students;


-- 条件

-- where子句

-- 使用where子句对表中的数据筛选,符合条件的数据会出现在结果集中

-- select 字段1,字段2... from 表名 where 条件;

-- where后面支持多种运算符,进行条件的处理

-- 比较运算

-- 逻辑运算

-- 模糊查询

-- 范围查询

-- 空判断


-- 比较运算符

-- 等于: =

-- 大于: >

-- 大于等于: >=

-- 小于: <

-- 小于等于: <=

-- 不等于: != 或 <>


-- 查询小乔的年龄

select age from students where name = '小乔';

-- 查询20岁以下的学生

select * from students where age < 20;

-- 查询家乡不在北京的学生

select * from students where hometown <> '北京';

-- 查询年龄大于22岁的学生信息

select * from students where age > 22;

-- 查询女学生信息

select * from students where sex = '女';

-- 查询年龄小于等于25岁的学生姓名

select name from students where age <= 25;

-- 查询年龄大于26岁的学生信息

select * from students where age > 26;

-- 查询大乔的身份证号

select card from students where name = '大乔';

-- 查询年龄小于15岁的学生信息

select * from students where age < 15;

-- 查询学号是'007'的学生的身份证号

select card from students where studentNo = '007';

-- 查询'1班'以外的学生信息

select * from students where class <> '1班';

-- 查询年龄大于20的学生的姓名和性别

select name,sex from students where age > 20;


-- 逻辑运算符

-- and 并且

-- or 或者

-- not 不,非


-- and

-- 查询年龄小于20的女同学

select * from students where age < 20 and sex = '女';

-- 查询1班中年龄大于20岁的男生信息

select * from students where class = '1班' and age > 20 and sex = '男';


-- or

-- 查询女学生或'1班'的学生

select * from students where sex = '女' or class = '1班';

-- 查询年龄大于15或者小于25的学生信息

select * from students where age > 15 or age < 25;



-- not

-- 查询非天津的学生

select * from students where not hometown='天津';


-- 查询河南或河北的学生

select * from students where hometown='河南' or hometown='河北';

-- 查询'1班'的'上海'的学生

select * from students where hometown='上海' and class = '1班';

-- 查询非20岁的学生

select * from students where not age = 20;

-- 查询3班中南京的学生信息或者年龄大于20的学生信息

select * from students where class = '3班' and hometown = '南京' or age > 20;

-- 查询1班中年龄大于20或者2班中年龄小于25的学生信息

select * from students where class = '1班' and age > 20 or class = '2班' and age < 25;



-- 模糊查询

-- like

-- %表示任意多个任意字符

-- _表示一个任意字符


-- 查询3班的学生信息

select * from students where class like '3班';

-- 查询不是3班的学生信息

select * from students where class not like '3班';


-- 查询姓孙的学生

select * from students where name like '孙%';

-- 查询姓孙且名是一个字的学生

select * from students where name like '孙_'

-- 查询姓孙且姓名是三位的学生信息

select * from students where name like '孙__';

-- 查询名字中含有云的学生信息

select * from students where name like '%云%';

-- 查询姓名结尾是乔的学生信息

select * from students where name like '%乔';

-- 查询姓名中第三个字是守的学生信息

select * from students where name like '__守%';

-- 查询名字是四位的学生信息

select * from students where name like '____';

-- 查询年龄大于20且姓孙的女生信息

select * from students where name like '孙%' and age > 20 and sex = '女';



-- 范围查询  

-- in表示在一个非连续的范围内   范围内的取值之间是or的关系


-- 查询家乡是北京或上海或广东的学生

select * from students where hometown in ('北京','上海','广东');

-- 查询年龄不是18或者19或者23的学生信息

select * from students where age not in (18,19,23);


-- between ... and ...表示在一个连续的范围内

-- 注意:小的值需要放在前面


-- 查询年龄为18至20的学生

select * from students where age BETWEEN 18 and 20;

-- 查询年龄不在20至25之间的学生信息

select * from students where age not BETWEEN 20 and 25;



-- 空判断

-- NULL

-- is null 为空

-- is not null 不为空


-- 查询没有填写身份证的学生

select * from students where card is null;


-- 查询填写了身份证的学生

select * from students where card is not null;


-- 查询没有身份证号的学生信息

select * from students where card is null or card = '';