MySQL数据库基础知识(二)
表结构操作的SQL语句
-
查看当前数据库中的所有表
show tables;
-
创建表
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('男','女','人妖','保密') );
说明:
create table 表名( 字段名称 数据类型 可选的约束条件 ... );
ps : auto_increment 表示自增
查看表结构
dese 表名;
-
修改表-添加字段
alter table 表名 add 列名 类型 约束; 例: alter table students add birthday datetime;
说明:
- modify: 只能修改字段类型或者约束,不能修改字段名
-
修改表-修改字段类型
alter table 表名 modify 列名 类型 约束; 例: alter table students modify birthday date not null;
-
修改表-修改字段名和字段类型
alter table 表名 change 原名 新名 类型及约束; 例: alter table students change birthday birth datetime not null; 说明: change: 既能对字段重命名又能修改字段类型还能修改约束
-
修改表-删除字段
alter table 表名 drop 列名; 例: alter table students drop birthday;
-
查看创表SQL语句
show create table 表名; 例: show create table students;
-
查看创库SQL语句
show create database 数据库名; 例: show create database mytest;
-
删除表
drop table 表名; 例: drop table students;
表数据操作的SQL语句
-
查询数据
-- 1. 查询所有列 select * from 表名; 例: select * from students; -- 2. 查询指定列 select 列1,列2,... from 表名; 例: select id,name from students;
-
添加数据
-- 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);
-
修改数据
update 表名 set 列1=值1,列2=值2... where 条件 例: update students set age = 18, gender = '女' where id = 6;
-
删除数据
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 给字段起一个别名。
-
使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;
-
可以通过 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;
3. 小结
- as 关键字可以给表中字段 或者 表名起别名
- distinct 关键字可以去除重复数据行。
where条件查询
学习目标
- 能够写出模糊查询的SQL语句
1. where条件查询的介绍
使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。
where语句支持的运算符:
- 比较运算符
- 逻辑运算符
- 模糊查询
- 范围查询
- 空判断
where条件查询语法格式如下:
select * from 表名 where 条件;
例:
select * from students where id = 1;
2. 比较运算符查询
- 等于: =
- 大于: >
- 大于等于: >=
- 小于: <
- 小于等于: <=
- 不等于: != 或 <>
例1:查询编号大于3的学生:
select * from students where id > 3;
例2:查询编号不大于4的学生:
select * from students where id <= 4;
例3:查询姓名不是“黄蓉”的学生:
select * from students where name != '黄蓉';
例4:查询没被删除的学生:
select * from students where is_delete=0;
3. 逻辑运算符查询
- and
- or
- not
例1:查询编号大于3的女同学:
select * from students where id > 3 and gender=0;
例2:查询编号小于4或没被删除的学生:
select * from students where id < 4 or is_delete=0;
例3:查询年龄不在10岁到15岁之间的学生:
select * from students where not (age >= 10 and age <= 15);
说明:
- 多个条件判断想要作为一个整体,可以结合‘()’。
4. 模糊查询
- like是模糊查询关键字
- %表示任意多个任意字符
- _表示一个任意字符
例1:查询姓黄的学生:
select * from students where name like '黄%';
例2:查询姓黄并且“名”是一个字的学生:
select * from students where name like '黄_';
例3:查询姓黄或叫靖的学生:
select * from students where name like '黄%' or name like '%靖';
5. 范围查询
- between … and … 表示在一个连续的范围内查询
- 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='男';
例3:查询编号不是3、5、7的人:
select * from students where id not in (3,5,7);
6. 空判断查询
- 判断为空使用: is null
- 判断非空使用: is not null
例1:查询没有填写身高的学生:
select * from students where height is null;
注意:
- 不能使用 where height = null 判断为空
- 不能使用 where height != null 判断非空
- null 不等于 ‘’ 空字符串
7. 小结
- 常见的比较运算符有 >,<,>=,<=,!=
- 逻辑运算符and表示多个条件同时成立则为真,or表示多个条件有一个成立则为真,not表示对条件取反
- like和%结合使用表示任意多个任意字符,like和_结合使用表示一个任意字符
- between-and限制连续性范围 in限制非连续性范围
- 判断为空使用: is null
- 判断非空使用: is not null
排序
排序查询语法:
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
语法说明:
- 先按照列1排序、如果列1的值相同,则按照列2排序,以此类推
- asc 升序,默认按照升序排序
- desc 降序
eg:
select * from students where gender=1 and is_delete=0 order by id desc;
分页查询
目的:为了方便显示那种一页显示不完的查询结果
分页查询语法
select * from 表明 limit start,count
语法说明:
- limit是分页查寻关键字
- start表示开始行索引,默认是0
- count表示查询条数
eg:
select * from students limit 0,3;