SQL易忘知识点总结

SQL易忘

sql不区分大小写

sql注释

# 注释内容
-- 注释内容
/* 注释
   内容
	*/

用于返回不重复的值

select distinct * from ... 

环绕文本值(字符串)以单引号为主

order by 默认升序排序(ANS),逆序关键字(DESC)

select * from .. 
order by .. desc;

order by 多序排列时,靠前的优先级高,且可分组

order by a,b,c;

insert into 特殊变量为标注确定值可自动生成,如id

insert into table_name
values (value1,value2...);

使用update语句必须规定where 子句,否则所有的记录都将被更新

update table_name
set column1=value1 , ...
where some_column=some_value;

使用delete 语句如果不加where 子句,所有的行都将被删除,但是表未删除的情况下,

表的结构、属性、索引都将保持不变

delete from table_name
where name='aim_to_delete_name';

select top 或 limit 或 rownum 用于规定选出的记录的数目

根据不同的数据库选用不同的关键字

限制可以是数量(num),也可以是百分比(percent)

like 操作符

select  * from table_name
where name like 'G%';
# "%"为通配符,例中表示以G开头的所有名字

通配符:

  • “%” 匹配任意字符

  • “_” 任意单个字符

  • 正则表达式

select * from table_name
where name REGEXP '^[abc]';
# 选取以a或b或c开头的所有name
# NOT REGEXP表示取反

IN 操作符

select column_name(s)
from table_name
where column_name IN (value1,value2,...);#相当于多个and

between操作符

select * from table_name
where column_name between value1 and value2;# 取反:not between

AS操作符:别名,as可省略,但尽量不省略

别名中有空格的话需使用单引号或双引号括起来

  • 列别名
  • 表别名

JOIN 连接

inner join 关键字在表中存在至少一个匹配时返回行(交集)

(inner 可省略)

select table1.id,table1...... ,table2.id ,.....
from table1
inner join table2
on table1.id=table2.id;

left join 关键字(左表为主,右表没有对应值为null)

左表table1中返回所有行,及时右表中没有匹配,不匹配的返回NULL

right jion 与上对应(右表为主,左表没有对应值为null)

full outer jion(取并集,两个表自己没有对应值的为null)

Union操作符:合并两个或多个select语句的结果

注意:Union每个select语句必须拥有相同数量的列,列也必须拥有相似的数据类型,且select语句中的列的顺序必须相同,才能进行union

select column_name(s) from table1
union
select column_name(s) from table2
where ...
order by ... ;
# select中列名必须对应
# 默认为选取不同的值,如允许选取重复的值需使用 union all

select into 从一个表中复制数据,然后把数据插入到另一个新表中

等价于

cerate table new_table
as 
select * from old_table;
select * 
into new_table
from old_table
where ...;
# 备注:新表将会使用select语句中定义的列名和类型进行创建,也可以使用as子句来应用新的名称

insert int select语句

从一个表复制数据,然后插入到另一个已经存在的表中

insert into table_aim
select * from table_source;
# 只复制目标列到另一个已经存在的表中
insert into table_aim
(column_name(s))
select column_name(s)
from table_source;

create database语句

create table + constraint 约束

create table table_name
(
column1 int,
column2 varchar(255),
...
);
  • not null:不能存放空值
  • unique:每行必须有唯一值
  • primary key:等价于not null + unique 每个表只能有一个primary key约束
  • foreign key:保证一个表中的数据匹配另一个表中的值的参照完整性
  • check:保证列中的值符合指定条件
  • default:规定没有给列赋值时的默认值
not null
create table table_name
(
id int not null;
...
)
# 添加not null 约束
alter table table_name
modify id int not null;

# 删除not null约束
alter table table_name
modify id int null;
unique
# 创建表添加unique约束语法由数据库种类决定

# 不同数据库alter table 时添加、撤销此约束时语法亦有差异
primary key
foreign key
check (附加约束条件)
default(附加缺省内容)

create index

创建索引,在不读取整个表的情况下,可以更快查找数据

create index index_name
on table_name (column_name,.....)
# 不允许使用重复的值:唯一的索引意味着两个行不能拥有相同的索引值
# 使用create unique index ......

drop

drop table table_name
drop database database_name
# 删除索引视数据库决定

# 删除表中数据,而不删除表本身
truncate table table_name

alter table

# 在表中添加列
alter table table_name
add column_name datatype
# 修改表中列的数据类型
alter table table_name
alter column column_name datatype

# 删除表中列
alter table table_name
alter column column_name

auto increment

新纪录插入表中时生成一个唯一的数字

create table table_name
(
id int not null auto_increment,
......
)
# 默认开始值是1,递增1
# 从固定值开始递增如下:
alter table table_name auto_increment=100

视图views

基于sql语句的结果集的可视化的表

create view view_name as
select column_name(s)
from table_name
where condition
  • 视图隐藏了底层的表结构,简化了数据访问操作,客户端不需要指导底层表的结构及其之间的关系
  • 视图提供了一个统一访问数据的接口,即可以允许用户通过视图访问数据的安全机制,而不授予用户直接访问底层表的权限
  • 加强了安全性,只能看到视图所显示的数据
  • 视图可以被嵌套,一个视图中可以嵌套另一个视图

日期 data函数

is null & is not null

select * from table_name
where column_name is not null

各种数据类型

各种函数

group by

用于结合聚合函数,根据列对结果集进行分组

select column_name , arrregate_function(column_name)
from table_name
where column_name operator value
group by column_name

having子句

解决where关键字无法和聚合函数一起使用的问题,可以让我们筛选分组后的各组数据

select column_name,aggrefate_function(column_name)
from table_name
where column_name operator value
group by column_name
having aggregate_function(column_name) operator value;

exists运算符

判断查询子句是否有记录,如果有一天或者多记录则返回存在true,反之返回false

select column_name(s)
from table_name
where exists
(select ......);

以上是本人在菜鸟教程中复习sql基础知识的部分总结笔记,内容比较笼统,更多内容请访问

[菜鸟教程–SQL教程](SQL 教程 | 菜鸟教程 (runoob.com))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给你一颗语法糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值