数据库基础
navicat的使用方法:
chmod +x navicat15-premium-cs.AppImage
./navicat15-premium-cs.AppImage试用期结束:
cd~
rm -r .navicat64
登录数据库
mysql -uroot -p
显示当前时间
select now()
退出数据库
quit/exit/ctr+d
查看所有数据库
show databases;
创建数据库
create database python charset=utf8
使用数据库
use python
查看当前使用的数据库
select database();
删除数据库
drop database python;
查看当前数据库中所有的表
show tables;
查看表结构
desc students;
创建表
create table students(
字段名 字段属性 约束条件,
);
修改表 - 添加字段
alter table students add 列名 类型 约束;
alter table students add birthday datetime;
修改表 - 修改字段类型
alter table student modify 列名 类型 约束;
alter table students modify birthday date not null;
修改表 - 修改字段名和字段类型
alter table student change 原名 新名 类型约束;
alter table students change birthday birth datetime not null;
修改表 - 删除字段
alter table students drop birthday;
查看创建sql语句
show create table students;
删除表
drop table students;
查询数据
// 查询所有数据
select * from students;
添加数据
// 全列插入
insert into student value(0,'sin',18,'boy') // 可以使用default代替,使用的默认值
// 部分插入
insert into student(name,age) values('tan',30);
// 多行插入
insert into student values(0,'python1',20,'girl'),(0,'python2',21,'boy');
// 多行部分插入
insert into student(name,age) values('quangirl1',20),('quangirl2',30)
修改数据
update student set age = 50 where id = 1;
update student set age=18,gender'girl' where id = 3;
删除数据
// 物理删除
delete from student where id = 1;
// 逻辑删除
alter table student add is_del tinyint default 0; // 增加一个标识
update student set is_del=1 where id = 7; // 逻辑上删除了数据(打开标识)
// 查询所有被删除的数据
select * from student where is_del =1;
as关键字 | as关键字可以省略也可以表示别名
select name as n,age as a from student;
distinct筛选重复项 | 用于去除重复的数据行
select distinct gender from student;
where条件查询
// 条件运算符
select * from student where id > 3;
select * from student where id <= 4;
select * from student where name != 'sin';
// 逻辑运算符
select * from student where id > 3 and gender = 'boy';
select * from student where id < 4 or is_del = 0;
select * from student where not (age >= 10 and age <= 15);
between | in查询(范围查询)
// between 查询x and x 之间的数据
select * from student where id between 3 and 6;
// in 查询(xxx)之间的数据
select * from student id in (3,5,7);
模糊查询
// 1.like 有qu开头的所有数据
select * from student where name like 'qu%';
// 2.like 有si和后面只有一位的
select * from student where name like 'si_';
// % 表示多个字符 | _ 表示一个字符 ps:任意字符
排序查询
// 对没有给删除的男生,的id进行降序
select * from student where is_del = 0 and gender = 'boy' order by id desc;
分页查询
// limit 关键字查询 范围
select * from student where='boy' limit 0,3;
// 查询,获取第n页数据的sql语句
select * from student limit (n-1)*m,m;
清屏幕
system clear;
聚合函数(常用)
// 计算个数(聚合函数不对空集计数)
select count(height) from student;
// 查询最大值
select max(id) from student;
// 查询最小值
select min(id) from student;
// 查询总和
select sum(id) from student;
// 查询平均
select avg(height) from student;
// ifnull 对null值的替换
select avg(ifnull(height,0)) from studnet;
分组查询
// 查询多少种类
select distinct gender from student;
// 根据gender字段进行分组,查询每个分组的姓名信息
select gender , group_concat(name) from student group by gender;
// 根据gender字段,查询每组年龄的平均值
select gender , avg(name) from student group by gender;
// 根据gender字段,查询每组的人数
select gender , count(*) from student group by gender;
// 根据gender字段,汇总每组的年龄
select gender , sum(age) from student group by gender;
内连接查询
// 两个表相关的内容一起展示出来(公共数据c_id = id)
select * from student inner join classes on student.c_id = classes.id;
select s.name,c.name from student s inner join classes c on s.c_id = c.id;
// 内连接用inner join .. on ..
左连接查询
//以左表为主根据条件查询右表数据,如果根据条件查询右表数据不存在使用null填充
select * from student s left join classes c on s.c_id = c.id;
右连接查询
// 以右表为主根据条件查询左表数据,如果根据条件查询左表数据不存在使用null填充
select * from student s left join classes c on s.c_id = c.id;
内连接查询
// 同上
select * from area c inner join areas p on c.pid = p.id limit 20;
子查询
// 在一个select语句中,嵌入了另外一个select语句,那么被嵌入的select语句被称为子查询语句
// 查询大于平均年龄的学生
select * from student where age > (select avg(age) from student);
// 查询学生在班的所有班级名字
select name from classes where id in (select cls_ id from student where cls_id is not null);
// 查询年龄最大,身高最高的学生
select * from student where (age,height) = (select max(age),max(height) from student);
外链约束sqls
alter table student add foreign key(c_id) references classes(id);
Q.E.D.