数据类型
- 数字类型
int bit tinyint - 小数
decimal(5,2) - 可变字符串
varchar(数字) - 固定长度字符串
char(数字) - 枚举
enum(xx, xxx) - 时间
date, datetime
约束
- 主键:
primary key - 非空:
not null - 唯一性:
unique - 默认值:
default
[!tip|style:flat|label:提示]
添加数据的时候,会对数据的数据类型和约束进行验证,如果不满足要求,则会添加失败,保证数据的准确性
数据库操作
- 查看所有数据库:
show databases; - 创建数据库:
create database 数据库名 charset=utf8; - 查看创建的数据库语句:
show create database python; - 使用数据库:
use 数据库名; - 查看当前使用的数据库:
select database(); - 删除数据库:
drop database 数据库名;
数据库表操作
-- 数据准备
create database py charset=utf8;
-- 查看数据库中所有的表(已经选择一个数据库)
show tables;
-- 创建数据表
create table 表名(字段,数据类型 数据约束,字段,数据类型 数据约束);
-- 创建学生表
-- id 主键 id int unsigned primary key not null auto_increment(自动增长)
-- 姓名 name varchar(20) not null
-- 年龄 age tinyint dafault 0
-- 身高 height decimal(5,2)
-- 性别 gender enum('男', '女', '保密')
create table stu(
id int unsigned primary key not null auto_increment,
name varchar(20) not null,age tinyint default 0,height decimal(5,2),gender enum('男', '女', '保密')
);
-- 查看创建表语句
show create table stu;
-- 查看表结构
desc stu;
-- 添加一个字段
alter table 表名 add 字段名 类型 约束;
-- 添加一个 birth 字段,出生日期
alter table stu add birth datetime;
-- 修改字段的名字和类型
alter table 表名 change 原字段名 新的名字 类型 约束;
-- 修改 birth 字段名为 birthday, 类型为date
alter table stu change birth birthday date;
-- 修改字段的类型和约束
alter table 表名 modify 字段 类型 约束:
-- 修改 birthday 的类型为 datetime;
alter table stu modify birthday datetime
-- 删除字段
alter table 表名 drop 字段:
-- 删除 birthday 字段
alter table stu drop birthday;
-- 删除表
drop table 表名;
-- 删除 stu 表
drop table stu;
数据增加和查询操作
-- 增加数据
-- 插入一条数据
insert into 表名 values(值1, 值2, 值3);
-- 插入一条数据
insert into stu values(1, '张三', 20, 180.00, '男');
-- 插入多条数据
insert into 表名 values(值1, 值2, 值3),(值1, 值2, 值3),(值1, 值2, 值3);
-- 插入三条数据
insert into stu values(2, '李四', 20, 180.00, '男'),(3, '王五', 20, 180.00, '男'),(4, '赵六', 20, 180.00, '男');
-- 对于枚举类型来说可以使用数字(下标从 1 开始)代替具体的字符串
insert into stu values(0, '张三丰', 28, 180.00, 1)
-- 插入多条数据
insert into 表名(字段1, 字段2) values(值1, 值2),(值1, 值2),(值1, 值2);
-- 插入三条数据
insert into stu(name, age) values('张三', 20),('李四', 20),('王五', 20);
-- 查询数据
-- 查询所有数据
select * from 表名;
-- 查询所有数据
select * from stu
-- 查询知道的字段
select name, age from stu;
-- 删除
delete from 表名 where 条件;
delete from stu where id=3;
-- 逻辑删除(推荐)本质是修改数据用一个字段的数据值来判断是否被删除
-- 1. 添加字段 is_delete bit default 0
alter table stu add is_delete bit default 0;
-- 2. 删除学生,修改数据
update stu set is_delete=1 where id=4;
-- 查询没有被删除的数据
select * from stu where is_delete=0;
as 和 distinct 关键字
-- 给字段起别名
select name as 姓名, age 年龄 from stu; -- as 可以省略
-- 去重 distinct 后边可以跟多个字段已逗号隔开 不过查询的是组合的数据
select distinct age from stu;
where条件查询
-- > 大于 < 小于 = 等于 <= 大于等于 >= 小于等于 != 不得与或者 <>
-- 查询编号大于等于3的学生
select * from stu where id > 3;
-- 查询编号不大于4的学生
select * from stu where id < 4;
select * from stu where id <= 4;
-- 查询姓名不是 张三丰 的学生
select * from stu where name != '张三丰';
-- 查询没被删除的学生
select * from stu where is_delete=0;
逻辑运算符查询
-- 查询编号大于3的女同学
select * from stu where id>3 and gender='女';
-- 查询编号小于4或没被删除的学生
select * from stu where id<4 or is_delete=0;
-- 查询年龄不在20到25岁之间的学生
elect * from stu where age < 20 or age > 25;
模糊查询
-- 使用关键字 like ,% 任意读个字符,_任意一个字符
-- 查询姓张的学生
select * from stu where name like '张%';
-- 查询姓张并且“名”是一个字的学生 张_
select * from stu where name like '张_';
-- 名字是两个字的学生 ’__‘
select * from stu where name like '__';
-- 查询姓张或者叫三的学生 "张%" or "%三"
select * from stu where name like "张%" or name like "%三";
范围查询
-- betweeen a and b 联系的范围
-- in (a, b, c)在里面的内容
select * from stu where id between 3 and 7;
-- 查询编号不是3至7的学生
select * from stu where id not between 3 and 7;
select * from stu where not id between 3 and 7;
-- 查询编号是4,5,7的学生
select * from stu where id = 4 or id = 5 or id = 7;
select * from stu where id in (4,5,7)
-- 查询编号不是4,5,7的学生
select * from stu where id not in (4,5,7)
判空
-- 查询没有填写升高的学生
-- null,在SQL 中判断一个数据值是不是null,需要使用is ull, is not null
update stu set height=null where id in (2,4); -- 修改数据
select * from stu where height is null;
order by 排序
-- 基本结构
select * from 表名 where 条件判断 order by 字段 [asc | desc]
-- asc 升序, 默认的
-- desc 降序
-- 查询未删除的男生信息,按学号降序
select * from stu where gender='男' order by id desc;
-- 显示所有学生的信息,先按照年龄大小,当年龄相同是 按照升高从高到矮排序
select * from stu order by age desc, height desc;
分页
-- 基本信息
select * from 表名 limit xx; -- 显示多少条信息
-- 查询前3条男生信息
select * from stu where gender='男' limit 3;
-- 年龄最大的三个人
select * from stu order by age desc limit 3;
-- 查询学生表,获取第n页的数据的SQL,每页显示 m 个 最关键的是求第n也对应的开始索引
select * from stu limit (n-1)*m, m;
select * from stu limit 0,3; -- 0可以不写
select * from stu limit 3,3;