命令行客户端MySQL的使用
1. 登录和登出数据库
登录数据库:
mysql -uroot -p
登出(退出)数据库:
quit 或 exit 或 ctrl + d
2. 数据库操作的SQL语句
1.查看所有数据库
show databases;
2.创建数据库
create database 数据库名 charset=utf8;
例:
create database python charset=utf8;
3.使用数据库
use 数据库名;
4.查看当前使用的数据库
select database();
5.删除数据库-慎重
drop database 数据库名;
例:
drop database python;
3. 表结构操作的SQL语句
1.查看当前数据库中所有表
show tables;
2.创建表
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) not null,
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男','女','人妖','保密')
);
3.修改表-添加字段
alter table 表名 add 列名 类型 约束;
例:
alter table students add birthday datetime;
4.修改表-修改字段类型
alter table 表名 modify 列名 类型 约束;
例:
alter table students modify birthday date not null;
说明:
modify: 只能修改字段类型或者约束,不能修改字段名
5.修改表-修改字段名和字段类型
alter table 表名 change 原名 新名 类型及约束;
例:
alter table students change birthday birth datetime not null;
说明:
change: 既能对字段重命名又能修改字段类型还能修改约束
6.修改表-删除字段
alter table 表名 drop 列名;
例:
alter table students drop birthday;
7.查看创表SQL语句
show create table 表名;
例:
show create table students;
8.查看创库SQL语句
show create database 数据库名;
例:
show create database mytest;
9.删除表
drop table 表名;
例:
drop table students;
表数据操作的SQL语句
1.查询数据
-- 1. 查询所有列
select * from 表名;
例:
select * from students;
-- 2. 查询指定列
select 列1,列2,... from 表名;
例:
select id,name from students;
2.添加数据
-- 1. 全列插入:值的顺序与表结构字段的顺序完全一一对应
insert into 表名 values (...)
例:
insert into students values(0, 'xx', default, default, '男');
-- 2. 部分列插入:值的顺序与给出的列顺序对应
insert into 表名 (列1,...) values(值1,...)
例:
insert into students(name, age) values('王二小', 15);
-- 3. 全列多行插入
insert into 表名 values(...),(...)...;
例:
insert into students values(0, '张飞', 55, 1.75, '男'),(0, '关羽', 58, 1.85, '男');
-- 4. 部分列多行插入
insert into 表名(列1,...) values(值1,...),(值1,...)...;
例:
insert into students(name, height) values('刘备', 1.75),('曹操', 1.6);
说明:
主键列是自动增长,但是在全列插入时需要占位,通常使用空值(0或者null或者default)
在全列插入时,如果字段列有默认值可以使用 default 来占位,插入后的数据就是之前设置的默认值
3.修改数据
update 表名 set 列1=值1,列2=值2... where 条件
例:
update students set age = 18, gender = '女' where id = 6;
4.删除数据
delete from 表名 where 条件
例:
delete from students where id=5;
问题:
上面的操作称之为物理删除,一旦删除就不容易恢复,我们可以使用逻辑删除的方式来解决这个问题。
-- 添加删除表示字段,0表示未删除 1表示删除
alter table students add isdelete bit default 0;
-- 逻辑删除数据
update students set isdelete = 1 where id = 8;
说明:
逻辑删除,本质就是修改操作
as和distinct关键字
1. as关键字
在使用SQL语句显示结果的时候,往往在屏幕显示的字段名并不具备良好的可读性,此时可以使用 as 给字段起一个别名。
1.使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;
2.可以通过 as 给表起别名
-- 如果是单表查询 可以省略表名
select id, name, gender from students;
-- 表名.字段名
select students.id,students.name,students.gender from students;
-- 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;
2. distinct关键字
distinct可以去除重复数据行。
select distinct 列1,... from 表名;
例: 查询班级中学生的性别
select name, gender from students;
-- 看到了很多重复数据 想要对其中重复数据行进行去重操作可以使用 distinct
select distinct name, gender from students;
where条件查询
使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。
select * from 表名 where 条件;
例:
select * from students where id = 1;
1. 比较运算符查询
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>
例1:查询编号大于3的学生:
select * from students where id > 3;
例2:查询没被删除的学生:
select * from students where is_delete=0;
2.逻辑运算符查询
and
or
not
例1:查询编号大于3的女同学:
select * from students where id > 3 and gender=0;
例2:查询年龄不在10岁到15岁之间的学生:
select * from students where not (age >= 10 and age <= 15);
说明:
多个条件判断想要作为一个整体,可以结合‘()’。
3. 模糊查询
1)like是模糊查询关键字
2)%表示任意多个任意字符
3)_表示一个任意字符
例1:查询姓黄的学生:
select * from students where name like '黄%';
例2:查询姓黄并且“名”是一个字的学生:
select * from students where name like '黄_';
例3:查询姓黄或叫靖的学生:
select * from students where name like '黄%' or name like '%靖';
4. 范围查询
1)between … and … 表示在一个连续的范围内查询
2)in 表示在一个非连续的范围内查询
例1:查询编号为3至8的学生:
select * from students where id between 3 and 8;
例2:查询编号不是3至8的男生:
select * from students where (not id between 3 and 8) and gender='男';
5.空判断查询
1)判断为空使用: is null
2)判断非空使用: is not null
例1:查询没有填写身高的学生:
select * from students where height is null;
注意:
不能使用 where height = null 判断为空
不能使用 where height != null 判断非空
null 不等于 ‘’ 空字符串
排序
排序查询语法:
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
语法说明:
1)先按照列1进行排序,如果列1的值相同时,则按照 列2 排序,以此类推
2)asc从小到大排列,即升序
3)desc从大到小排序,即降序
4)默认按照列值从小到大排序(即asc关键字)
例1:查询未删除男生信息,按学号降序:
select * from students where gender=1 and is_delete=0 order by id desc;
例2:显示所有的学生信息,先按照年龄从大–>小排序,当年龄相同时 按照身高从高–>矮排序:
select * from students order by age desc,height desc;
分页查询
当我们在浏览商城的适合,,由于数据特别多,一页显示不完,一页一页的进行显示,这就是分页查询
select * from 表名 limit start,count
说明:
1)limit是分页查询关键字
2)start表示开始行索引,默认是0
3)count表示查询条数
例1:查询前3行男生信息:
select * from students where gender=1 limit 0,3;
简写
select * from students where gender=1 limit 3;
分页查询案例
已知每页显示m条数据,求第n页显示的数据
提示: 关键是求每页的开始行索引
查询学生表,获取第n页数据的SQL语句:
select * from students limit (n-1)*m,m