SQL语言

数据表操作

创建表

create table 表名{
     字段名 类型 约束,
     字段名 类型 约束,
     .....
}

删除表

格式一:drop table 表名
格式二:drop table if exists 表名  //先检验是否有这个表

数据操作

添加数据

格式一:insert into 表名 values(...)  //括号里填一条数据的所有字段信息
格式二:insert into 表名(字段1,字段2,...) values(值1,值2,...) 

修改数据

update 表名 set 列1=值1,列2=值2... where 条件

删除数据

delete from 表名 where 条件

查询数据

查询所有信息

select * from 表名

查询指定字段

select 列1,列2,... from 表名

消除重复行 distinct

select distinct 列1 from 表名
例:select distinct age from students

条件查询

使用where子句对数据表进行筛选

select * from 表名 where 条件
例:select * from students where age=18

比较运算符:

等于:=,大于:>,大于等于:>=,小于:<,小于等于:<=,不等于:!=或<>

逻辑运算符:

and,or,not

select * from students where age=18 and address='北京' //查询年龄是18岁和地址是北京的学生
select * from students where age=18 or address='北京' //查询年龄是18岁或者地址是北京的学生
select * from students where not age=18 //查询年龄不是18岁的学生

模糊查询:

  • like
  • %表示任意多个字符
  • _表示一个任意字符
select * from students where name like '张%' //查询姓张的学生
select * from students where name like '_徐坤' //查询名字后两个字叫徐坤的学生 

范围查询:

in:表示在一个非连续的范围

select * from students where age in (17,18,20,28)

between...and... :表示在一个连续的范围

select * from students where age between 18 and 20 //查询年龄为18到20岁的学生

空判断:

注意:null跟' '是不同的

判断空 is null

判断不为空 is not null

select * from students where address is null
select * from students where address is not null

排序:

升序:asc(不填默认是升序)

降序:desc

select * from students order by age asc //可以不写asc也行,默认是升序
select * from students order by age desc //降序

 

posted @ 2019-09-15 16:02  哗哗哗  阅读(200)  评论(0编辑  收藏  举报