SQL (Structured Query Language)是具有数据操纵和数据定义等多种功能的数据库语言,这种语言具有交互性特点,能为用户提供极大的便利,数据库管理系统应充分利用SQL语言提高计算机应用系统的工作质量与效率。
SQL基本语法
SQL基础语法分为四类:
- DDL(Data Definition Language):数据定义语言,常用操作有create创建,drop删除,alter更改等
- DML(Data Manipulation Language):数据操作语言,常用操作有insert插入,delete删除,update更改等
- DQL(Data Query Language):数据查询语言,常用操作有select
- DCL(Data Control Language):数据控制语言,常用操作有commit提交,rollback回滚等
DDL
creat
创建,用于创建表,视图等。
以创建表为例,creat table table_name(字段)
在数据库的表中,每一行称为记录,每一列称为字段
create table t_customer( -- 创建字段
cust_id int, -- 创建字段语法:标识符 数据类型
cust_name varchar(20), -- varchar指可变字符串类型,不超过括号内长度
cust_gender char(1), -- char固定字符串类型
cust_age int,
cust_birthday date, -- data 日期数据类型
cust_total decimal(10, 2), -- dacimal 实型类型,表示十位数字保留两位小数
cust_note varchar(60)
);
drop
删除
以删除表为例,drop table table_name
drop table t_customer; -- 删除表(无法恢复)
alter
修改
以对表中字段操作为例,用法为alter table table_name 操作
alter table t_customer
add cust_level varchar(10); -- 添加新的字段
alter table t_customer
drop cust_level; -- 删除字段
alter table t_customer
change cust_gender cust_sex varchar(4); -- 修改表中字段名称和类型
alter table t_customer
modify column cust_sex char(2); -- 修改表中字段长度
alter table t_customer
rename t_cust; -- 修改表的名称
DML
insert
对表中内容进行增加
insert into table_name values(添加内容)
括号中的添加内容如果不规定要添加的字段,则根据添加内容类型自动填充,没有填充到的字段内容为NULL。
insert into t_customer -- 插入全部字段数据
values(1, '张三', 'M', 19, date('2023-01-01'), 5632.12, '无');
insert into t_customer -- 插入指定字段数据
(cust_id, cust_name)
values(2, '李四');
insert into table_name values(),(),()...; --批量插入记录
delete
,对记录进行删除
delete from table_name where 条件
,如果不添加条件会删除表中所有记录
delete -- 删除表中全部记录
from t_customer;
delete -- 删除表中指定记录
from t_customer
where cust_id = 1;
update
更改表中记录内容
用法为update table_name set 更改 where 更改位置判断
,不对更改位置进行指定时会修改该字段全部内容。
update t_customer -- 修改指定字段全部数据
set cust_note = '无';
update t_customer -- 修改指定记录中字段数据
set cust_gender = 'F'
where cust_id = 1;
DQL
select
查询数据
select * from t_customer; -- 查询表中所有数据
DCL
commit
提交
rollback
回滚
查询语句
别名
为了查询出的结果集更易读,sql提供起别名的语法。
字段名/表名 as "别名"
select cust_name as "name" --给字段cust_name起别名为name
from t_customer;
起别名时as可写可不写,不写时用空格代替。 **“”**在别名是数字、关键字、函数时必须写。
起别名的方法也可以用在表的克隆,如下命令完成了将t_customer克隆为t_student:
create table t_student as
select cust_id stu_id, cust_name stu_name, cust_age stu_age, cust_gender stu_gender
from t_customer;
DQL查询操作
常用的关键字有from,where,group by,having,select,order by。
group by分组
分组的作用是将几条相同记录合并为一条记录。
select cust_age, cust_gender -- 输出结果时将相同的年龄和性别记录合并展示
from t_customer
group by cust_age, cust_gender;
要点:
- 可以按一个字段或者多个字段进行分组
- 可以使用一些汇总函数 max min sum count avg
- 使用分组后,select只能分组列、汇总列
- 没使用分组的情况下使用汇总函数会对整张表做一个分组
having过滤
过滤作用是对分组后的数据进行过滤,输出满足条件的不重复记录。
select cust_age
from t_customer
group by cust_age
having cust_age >= 25 -- 输出大于25且不重复的年龄有哪些
order by排序
作用是对一个字段或者多个字段进行排序
select * --*表示查找所有字段
from t_customer
order by cust_age; --对年龄进行排序
select *
from t_customer
order by cust_age desc , cust_id; -- 按年龄降序进行排序,年龄相等时按id升序
要点:
- order by默认为升序。加desc可以调整为降序
- 排序可以写字段名,别名,序号(第一个字段为1,第二个字段为2,以此类推)
- 排序后可继续加条件将第一次排序中相同的结果按新加条件再排序
- order by最后执行
select 查询
select now(); -- 时间函数,展示当前时间
select concat('abc', 'def'); -- 字符串拼接函数,展示结果为abcdefg
select pow(2,10); -- 数学函数,展示2的10次方
select distinct cust_age -- 展示t_customer表中不同的年龄
from t_customer;
select (select count(cust_gender)
from t_customer
where cust_gender = 'M') "男性" -- 查询的cust_gender = 'M'总数起别名为男性作为展示的查询结果
,(select count(cust_gender)
from t_customer
where cust_gender = 'F') "女性";
-- 查询的结果作为另一条查询的条件
select cust_name
from t_customer
where cust_age = (select min(cust_age) -- 子查询判断只能有一个结果,否则where判断不合法
from t_customer);
要点:
- 跟*表示查询全部字段
- 可以跟多个列名,列名之间用逗号隔开
- 可以跟常量
- 可以跟表达式
- 可以跟函数,如日期(时间)函数、字符串函数、数学函数
- 可以与distinct连用显示去重结果
- 可以跟子查询:查询结果当一条字段;查询的的结果当另一条查询的条件
case when语句
使用语法:满足哪个条件执行哪个结果,都不满足就执行else。
case when 条件 then 执行结果
when 条件 then 执行结果...
else 执行结果 end;
case when语句常用于行列转换。
select sum(
case when cust_gender = 'M' then 1
else 0
end
) 男性人数,
sum(
case when cust_gender = 'F' then 1
else 0
end
) 女性人数
from t_customer;
where关键字
select *
from t_customer
where cust_age >= 19
and cust_age <= 23; -- 跟关系运算符和逻辑运算符
select *
from t_customer
where cust_age between 19 and 23; -- 跟between..and..进行判断,该语法内部逻辑采用B+树,查找效率更快
# 两种方式表示逻辑或
select *
from t_customer
where cust_age = 20
or cust_age = 23;
select *
from t_customer
where cust_age in(20, 23);
select *
from t_customer
where cust_note is null; -- 判断是空语法is null
select *
from t_customer
where cust_note is not null; -- 判断非空语法is not null
select *
from t_customer
where cust_name like '张%'; -- like为模糊查询关键字,通配符%和_都可以匹配字符。_只能匹配一个字符,%可以匹配0个或多个字符。
要点:
- 可以跟关系运算符 > < = != >= <=
- 可以跟逻辑运算符 and or not
- 可以跟is,is多用于null的判断。is null、is not null
- 可以跟模糊查询like
- 可以跟exists,后面语句多用于子查询的结果集合
- 可以跟>any、<any、>all、<all,后面语句多用于子查询结果的集合
- 变量的赋值 是 :=
- 可以跟between…and…, in
escape
:用于指定转义字符。
如下,想找到含有%的记录。如果使用一般的通配符where like %%%
,系统会认为要寻找的%是通配符。结果展示全部的记录。
select *
from t_customer
where cust_note like '%a%%' escape 'a'; -- 用escape进行转义
exists
:多用于查询结果的判断。
语法:exists(select 1 from..where..)
在from后的查询范围中查找到符合where条件的则返回为真,没有查询到则为假。
下面代码表示。查找并展示a中符合条件的所有字段,条件是a中的cust_id字段要与b中的cust_id相同。
select *
from t_customer a
where exists(select 1
from t_buy b
where a.cust_id = b.cust_id
);
from关键字
select *
from t_customer a,
t_buy b
where a.cust_id = b.cust_id; -- 多表联查查询a表与b表相同id的记录
要点:
- 可以跟表、视图、结果集
- 多个表之间可以用逗号分开
- 使用多表联查时注意加限定条件,不然会造成笛卡尔积
- 其他表中有相同字段时加表名表所属关系
select *
from t_customer a
where a.cust_id in (
select cust_id
from t_buy b
); -- 使用子查询实现与上方多表联查相同功能。在数据库操作优化时应尽量避免子查询
limit关键字
用于限制查询结果的展示
select * from t_stu limit 2 -- 展示前四行记录
select * from t_stu limit 2, 4 -- 从第三条记录开始,一共展示四条
select * from t_stu limit 4 offset 3 -- 跳过前三条记录从第四条开始
表的合并
左连接
以左表为基准做一个全量显示,符合就显示,不符合置空。
语法:左表名 left jion 右表名 on 连接条件
select *
from t_customer a
left join t_buy b on a.cust_id = b.cust_id
where b.cust_id is not null ;
内连接
用于返回满足条件的所有记录,在执行查询时如果没有指定任何连接操作,则此查询默认为内连接。
select *
from t_customer a
inner join t_buy b on a.cust_id = b.cust_id
where b.cust_id is not null ;
联合
union
:把两个结果集合并为一个结果集(竖向),并将合并后相同的结果集去重
union all
:把两个结果集合并,但不去重
ifnull(a, b)
:函数,查询结果不为空展示a值,为空展示b值
约束
用于确保数据库中的数据满足业务规则。
主键和外键约束
主键
表中一列多个字段的组合,用于作为表中某一记录的唯一标识。
主键特点:
- 主键与记录一一对应,主键不为空
- 主键分为单字段主键和多字段联合主键
创建主键方式:
- 创建表时添加主键
creat table t_user(
use_id int primary key,
use_name varchar(20)
)
- 为表新增主键约束
alter table table_name add constraint constraint_name primary key (field_name);
删除主键方式:
alter table table_name drop primary key constraint_name;
外键
一个表中的外键用于指向另一个表中的主键。
外键特点:
- 可以是一个字段或多个字段,一个表可以有有一个或多个外键
- 外键可以为空,如果不为空必须等于另一个表主键值
- 外键必须关联主表主键且类型一致
- 外键用于保证数据的一致性、完整性
创建外键方式:
- 创建表时添加
creat table t_score(
stu_id varchar(10) foreign key references t_stu (stu_id),
score_id int
)
- 为表新增外键约束
alter table foreign_table
add constraint constraint_name foreign key (field_name)
references primary_table (primary_key_field)
在删除主表记录时,如果有外表关联主表则要先在外表删除关联记录才能在主表删除记录
删除外键方式:
alter table table_name drop foreign key constraint_name;
非空约束
非空约束字段的值不能为空,对于使用非空约束的字段,如果用户在添加数据时没有指定值,会出现报错。
一般在创建表时添加非空约束。
create table t_users(
user_id varchar(20) not null,
user_name varchar(20)
)
唯一约束
要求该字段值唯一,允许为空但只能有一个空值。用于确保一个字段不会重复。
create table t_users(
user_id varchar(20) unique,
user_name varchar(20)
)
默认约束
指定某一字段的默认值(缺省值)
create table t_users(
user_id varchar(20),
user_name varchar(20),
user_age int default -1
)