主键的自动增长:auto_increment
格式:
create table 表名(
id int primary key auto_increment,
name varchar(10),
sal int
)
insert into 表名(name,sal) values('sss',20)
注意:上面这条语句,并没有插入主键的值,主键的值是自动增长,会自动根据上一行的数据自动增加
删除数据(重点)
1.删除数据的介绍
删除表中的数据
2.删除数据的操作
1.删除整张表的数据
格式:delete from 表名
2.有条件的删除数据
格式:delete from 表名 where 条件
格式:delete from 表名 where 条件1 and 条件2 and 条件3 ----多个条件同时满足才删除
格式:delete from 表名 where 条件1 or 条件2 or 条件3 ---只要满足其中一个条件就删除
更新数据(重点)
1.更新数据的命令关键字:update
2.更新操作:
格式一:针对整列的数据进行修改
update 表名 set 列名=值
格式二:针对多列进行修改
update 表名 set 列名1=值1,列名2=值2,...
格式三:有条件的修改
update 表名 set 列名=值 where 条件
格式四:更新的过程可以进行计算
update 表名 set 列名=列名+20 where 条件
查询数据(重点)
1.查询概述
查询是对数据库中已有的数据进行特定的组合,条件,次序进行检索
2.查询的格式介绍
select 列名1,列名2,列名3 from 表名;
3.查询操作
select * from 表名; --*代表所有的列
4.去重复和别名
1.格式:select distinct 列名 from 表名 --distinct去重复的关键字,不能同时作用在多列
2.取别名,给列名取别名,也可以给返回的结果列取别名
1.格式一:select 列名1 '别名1',列名2 '别名2' from 表名
2.格式二:select 列名 as '别名1' from 表名
5.计算列:针对查询的结果计算
select 列名1,列名2+10 from 表名
select 列名1+列名2+列名3 from 表名 --列名的值要数字
6.选择查询
1.选择查询就是有条件的选出一行或几行的数据
2.格式:select 列名 from 表名 where 条件
3.条件:
1.比较:=,>,<,>=,<=,!=,<>
2.逻辑(and 都真为真),(or 有真为真)
3.范围:
1.在某个范围之间查询:between ....and.....
格式:select 列名 from 表名 where 列名 between 最小值 and 最大值
注意:包括最大值和最小值
2.不在某个范围的数据查询:not between ....and.....
格式:select 列名 from 表名 where 列名 not between 最小值 and 最大值
4.搜索条件
1.在列表里的内容进行查询 in --只要匹配到列里任意一个值,都会有数据输出
格式:select 列名 from 表名 where 列名 in(值1,值2,值3...)
2.不要列里的内容 not int
格式:select 列名 from 表名 where 列名 not in(值1,值2,值3...)
3.字符串匹配:(模糊查询 like)
1.在查询过程中,利用指定模式,筛选查询的结果
格式:select 列名 from 表名 where 列名 like '指定模式'
提供了两个通配符:%--代表0个或多个字符 ,_代表1个字符
not like 不要和指定模式匹配的值
格式:select 列名 from 表名 where 列名 not like '指定模式'
5.涉空查询
1.为空的查询 is null
格式:select 列名 from 表名 where 列名 is null;
2.不为空的查询 is not null
格式:select 列名 from 表名 where 列名 is not null;