SQL全名是结构化查询语言(Structured Query Language),是用于数据库中的标准数据查询语言。在不同的数据库中基本互通。
SQL语言按照功能可分为五种:
DDL语句(数据定义语言),Create、Alter、Drop、Declare、Truncate
DQL语句(数据查询语言),Select
DML语句(数据操作语言),Insert、Update、Delete、Merge
DTL语句(事务控制语句),Commit、Rollback、Savepoint
DCL语句(数据控制语言),Grant、Revoke
1、表的创建和删除
创建表:create table 表名(列名1 数据类型1,列名2 数据类型2,...,列名n 数据类型n);
删除表:drop table 表名; 如果存在删除:drop table if exists 表名;
例如:
create table student(name varchar(3),age int(2),sex varchar(2));
drop table if exists student;
2、表内容的增删改查
单行录入:
insert into 表名(指定列名) values(信息);
多行录入:如果不声明列表信息,valuse中的顺序、数量需要和建表时一致
insert into 表名 values(信息1),(信息2),(信息3)...
删除记录:
delete from 表名 where 条件;
整表删除:
truncate table user 快速删除,保留表结构
限制删除条数:
delete from stu limit 2;
级联删除:删除主键表中的数据,由数据库自动删除外建表中的相关数据
cascade级联,一起删 no action 不允许删除 set null置空
修改:
update 表名 set (需要修改的内容) where 条件;
多条更新:
update 表名 set score=score+10 where sex = '男';
嵌套更新:
update 表名 set score = (select score from ...) where 条件;
查询:
select (查询的内容,全部可以用*)from 表名 where 条件;
例如:
insert into student(name,age,sex) values ('张三',20,'男');//插入一条数据
delete from student where name='张三';//删除一条数据
update student set name='李四',age=19 where sex='男';//将男学生的姓名和年龄修改
select * from student where name='李四';//查询名字为李四的学生信息
复制表:通过一个已有的数据表,创建新的数据表
create table stu1 as (select * from stu)
create table stu2 as (select * from stu where 1=2)条件不成立,不插入任何数据
复制数据:将一些数据从表中查询出来,录入到其他表中
insert into 数据表(数据1,数据2...)(select 数据1,数据2... from 数据表 where 条件)
3、常用数据类型
字符型数据
Char,不可变长度的字符。 VarChar,可变长度的字符。
Text,64kb的长文本。 Longtext,最长4gb的长文本。
数值数据类型
tinyint,1字节。整数 bigint,8字节。整数
smallint,2字节。整数 double,8字节。浮点数
int,4字节。整数 decimal(m,d),m字节。m代表整数,d代表小数。
日期类型
Date,天级(yyyy-MM-dd) time,时(hh:mm:ss)
Datetime(yyyy-MM-dd hh:mm:ss)
二进制数据类型
Blob最大长度64kb Longblob最大程度4GB
二进制类型可以存储任何数据,如文字、图形、多媒体等。
4、SQL单表查询
——查询所有记录
select * from student;
——变换查询内容,为列名起别名。
select name 名字,age 年龄,sex 性别 from student;
—— 查询不重复字段。
select distinct age from student;
>大于 | <小于 | =等于 | !=不等于 | >=大于等于 | <=小于等于 |
in属于 | not in 不属于 | between区间 | not between非区间 | like模糊匹配 | not like模糊不匹配 |
——查询年龄大于20的所有信息
select * from student where age>20;
——查询年龄不是18和20岁的学生
select * from student where age not in(18,20);
——查询年龄在18-20岁的学生信息
select * from student where age between 18 and 20;
——查询不姓张的学生信息 注:%代表任意长度,_代表一个长度
select * from student where name not like '张%';
regexp和rlike 匹配正则 | not rlike不匹配正则 | is null 判断空 | is not null 判断非空 |
默认正则表达式不区分大小写。
组合条件查询:与或非——and or not
默认升序排序 order by 降序排序 order by 列名 desc
分组查询group by having需要配合分组使用,筛选分组后的属性
常见的运算查询 + - * /
分页查询limit 条 limit 开始下标,条数
5、多表查询
无条件多表查询是将各表的记录以“笛卡尔”积的方式组合起来。比如A表有7条记录,B表有9条几率,无条件查询就是63条记录。
select A.*,B.* from a表 A, b表 B where A.a=B.a; AB为表的别名
select * from A where A.a=(select a from B where b='aaa'); 嵌套查询
select * from A where A.a in (Select distinct a from B); in包含关系,distinct去重
any 比其中某一个大 all 比其中所有的都大
select * from stu where sex='男' union select * from stu where sex='女' ;将查询结果合并
6、函数查询
6.1、逻辑函数
if(需要判断的内容,真值,假值);
ifnull(非空返回的值,空返回的值);
(Case when score>90 then '优秀' when score>60 then '良好' else '不及格' end)别名
(Case classroom when '一班' then '重点班' when '二班' then '普通版' else '自费班' end)别名
6.2、字符函数
连接字符串concat(str1,str2,...,strn)
字节长度length(str)—一个中文三个长度 char_length(str)—中文长度1
查找字符locate(substr,str)返回str在substr中第一次出现的位置,不存在返回0,开始下标1
locate(substr,str,pos),从pos开始,找str的第一个下标。
左填充lpad(字符串,长度,填充内容) rpad右填充
左截取left(字符串,长度) right右截取
截取substring(字符串,开始位置) substring(字符串,开始位置,长度)
截去前后空格trim()
全部替换replace(str,from,to)将所有的from替换为to
重复repeat(字符串,次数)
反转字符串reverse(str)
插入字符insert(源字符串,开始位置,长度,需要插入的字符串)长度写几个占几个
转小写lower、lcase 转大写upper、ucase
6.3、数字函数
绝对值abs 天花板ceil 地板floor
四舍五入round(num,1)小数位数 round(num,-1)整数0位数
截去truncate(num,1)最后一位小数抹0 truncate(num,-1)一位整数抹0
随机数rand 求模mod(x,y)返回x/y的余数
6.4、日期函数
获取当前系统时间,now、sysdate
日期格式化函数,date_format(日期,%Y年%m月%d日),转字符串
字符串转日期,str_to_date('05/20/2019','%m/%d/%Y'),转日期
日期的运算,date_add(时间,interval 数字 unit)
日期相减,datediff(时间1,时间2)
取得year(date)年 month(date)月 day(date)日 hour(date)时 minute(date)分
second(date)秒 dayOfWeek(date)(1=sun,...,7=sat) weekDay(date)(0=mon,...,6=sun)
last_day(date)返回所在月的最后一天
6.5、多行函数
sum求和 avg平均值 count计数 max最大 min最小 group_concat拼接组内字段
7、外连接
左外连接:select * from A left join B on A.a=B.a;
select * from A,B whre A.a(+)=B.a;(oracle)
右外连接:select * from A right join B on A.a=B.a;
select * from A,B where A.a=B.a(+);(oracle)
内连接:select * from A inner join B on A.a=B.a;
全连接:select * from A left join B on A.a=B.a
union
select * from A right join B on A.a=B.a;
exists存在
select * from stu t exists (select * from Score s where t.sum=s.sum);
注:与in相比,执行顺序不同,效率高。