-- 创建数据库
CREATE DATABASE mysql
-- 使用数据库
USE mysql
-- 删除数据库
DROP DATABASE mysql
创建表
-- 语法:create table 表名(列名,列的数据类型,
-- 列名,列的数据类型...)
-- 设置主键 唯一不重复的键,通常第一列id为主键列
create table t - student1 (
id int primary key auto_increment,-- 主键 主键自增长
stuname varchar ( 10 ),
phone varchar ( 11 ) unique not null,-- 唯一 非空
sex char ( 2 ) default '男' -- 默认
)
外键约束:
-- 班级表 主表
create table t_class (
c_id int primary key,
c_name varchar ( 20 )
)
-- 学生表 从表
create table t_student (
s_id int primary key auto_increment,-- primary key主键 auto_increment自增
s_name varchar ( 10 ),
phone int unique not null,-- unique 唯一 not null 不为空
sex varchar ( 4 ) default '男',-- default 默认值
classid int references t_class ( c_id ),-- 添加外键
constraint foreign key ( classid ) references t_class ( c_id ) -- 添加外键约束
)
-- 修改表 alter关键词
alter table t_student auto_increment = 500 -- 设置自增长从哪里开始
-- 修改表 添加列
alter table t_student add grade int
-- 修改表 改列名
alter table t_student change grade score float
-- 修改表 删除列
alter table t_student drop column score
-- 删除表
drop table t_student
-- 添加数据 insert into
-- 添加一行
insert into t_student values(null,'zhangsan',123,'男',1)
-- 对应添加
insert into t_student(s_name,phone,sex,classid) values ('lisi',333,'男',1)
--添加多行
insert into t_student
values
(null,'zhaoliu',666,'女',2),
(null,'wangwu',345,'女',1),
(null,'shunqi',443,'男',2)
-- 更新数据 update 表名 set 列名1='值',列名2='值' where 过滤条件
update t_student set s_name='zhangsan2' where s_name='zhangsan'
update t_student set classId=2 where s_name='lisi'
-- 删除表的指定数据
-- delete form 表名 where 过滤条件
delete from t_student where s_name = 'lisi'
-- 删除表格数据
delete from t_class -- 有办法复原
truncate table t_class -- 数据无法复原,效率高
-- 查询语句
-- 查询所有数据
select * from t_student
-- 查询id为2的学生
select *
from t_student
where s_id = 2
-- 查询表中存在的性别
select distinct sex
from t_student
-- 条件筛选 and
select *
from t_student
where s_id = 1 and sex='男' and age<20
-- 范围操作
select *
from t_student
where age>=18 and age<20
select * from t_student where age between 18 and 20
-- 选择
select * from t_student where age=19 or age=20 or age=25
select * from t_student where age in(19,20,25)
select * from t_student where age not in(19,20,25)
-- 模糊查询
select * from t_student where s_name like '小%' -- 以xxx开头
select * from t_student where s_name like '%明' -- 以xxx结尾
select * from t_student where s_name like '%华%' -- 只要包含就可以
-- 查询姓王并且名字长度是三个字的学生
select * from t_student where s_name like '王__'
-- 查询年龄为null的学生
select * from t_student where age is null -- 为空
select * from t_student where age is not null -- 不为空
-- 备份表格
create table t_student_bak select * from t_student -- 包含表格和数据
create table t_student_bak select * from t_student where s_id = 0 -- 只有表格没有数据
-- 排序
select * from t_student order by s_id ASC -- ASC 升序(默认,可不写)
select * from t_student order by s_id DESC -- DESC 降序
-- 当两个人年龄一样时 排序
select * from t_student order by age desc,classid desc
-- 指定返回信息的条数
select * from t_student limit 2 -- 只有一个参数表示返回两条数据
select * from t_student limit 2,2
-- 对筛选的结果进行计算
select s_name as '姓名',age+10 as '十年之后' from t_student
-- 查询 年龄大于18岁的学生 并且 根据年龄排序(升序) 只展示前三条
select * -- 3,6
from t_student -- 1
where age>18 -- 2
order by age -- 4
limit 0,3 -- 5
-- 聚合函数:count
select count(*) from t_student
select count(age) from t_student -- 计算age列有多少条数据,值为null的列将不被计算进去
-- 求和
select sum(age) from t_student
-- 求平均值:avg
select avg(age) from t_student -- 平均值的计算结果不包含null
select sum(age)/count(*) from t_student -- 这种计算方式可能导致结果相差较大,没有计算null的数据
select avg(ifnull(age,19)) from t_student -- 如果值为空就给一个默认值
-- 最大:max | 最小:min
select max(age),min(age),sum(age),avg(age) from t_student
-- 分组: GROUP BY
select classid ,count(*) from t_student group by classid -- 一旦使用了聚合函数, select 和 from 中间的列就必须是聚合函数 或者 被group by的列
-- 二次筛选
-- 查询男生数量大于1的班级
select classid,count(*)
from t_student
where sex = '男'
group by classid
having(count(*)>1)
-- 显示男生数量大于1的班级并且按照人数多少排序
select classid,count(*)
from t_student
where sex = '男'
group by classid
having(count(*)>1 )
order by count(*) desc
-- 显示男生数量大于1的班级并且按照人数多少排序,只显示前两条
select classid,count(*)
from t_student
where sex = '男'
group by classid
having(count(*)>1 )
order by count(*) desc
limit 0,2