SQL 快速参考:简化数据库管理

来源:这里教程网 时间:2026-02-28 18:53:27 作者:

sql 备忘单

本博客全面指导最重要的sql命令和操作。它涵盖了基本查询、连接、子查询、索引和更高级的概念。

目录

    sql 基础知识 数据定义语言(ddl) 数据操作语言(dml) 数据查询语言(dql) 数据控制语言(dcl) 加入 子查询 索引 聚合函数 分组和排序 交易 高级 sql 最佳实践

sql 基础知识

sql 查询的结构

select column1, column2
from table_name
where condition
order by column
limit n;

在 sql 中注释

单行评论: -- 这是一条评论 多行评论
  /* this is a 
     multi-line comment */

数据定义语言(ddl)

创建表

create table table_name (
    column1 datatype [constraints],
    column2 datatype [constraints],
    ...
);

示例:

create table employees (
    id int primary key,
    name varchar(100),
    age int,
    hire_date date
);

修改表格

添加列

alter table table_name
add column_name datatype;

删除一列

alter table table_name
drop column column_name;

修改列

alter table table_name
modify column column_name datatype;

重命名表

alter table old_table_name
rename to new_table_name;

删除一个表

drop table table_name;

创建索引

create index index_name
on table_name (column_name);

删除索引

drop index index_name;

数据操作语言 (dml)

将数据插入表中

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

示例:

insert into employees (id, name, age, hire_date)
values (1, 'john doe', 30, '2022-01-01');

更新表中的数据

update table_name
set column1 = value1, column2 = value2, ...
where condition;

示例:

update employees
set age = 31
where id = 1;

从表中删除数据

delete from table_name
where condition;

示例:

delete from employees
where id = 1;

数据查询语言 (dql)

从表中选择数据

select column1, column2, ...
from table_name
where condition
order by column
limit n;

示例:

select * from employees;
select name, age from employees where age > 30;

通配符

*:选择所有列 %:零个或多个字符的通配符(在 like 子句中) _:仅代表一个字符的通配符(在 like 子句中)

示例:

select * from employees where name like 'j%';

数据控制语言(dcl)

授予权限

grant permission on object to user;

示例:

grant select, insert on employees to 'user1';

撤销权限

revoke permission on object from user;

示例:

revoke select on employees from 'user1';

加入

内连接

当两个表中存在匹配项时返回行。

select columns
from table1
inner join table2
on table1.column = table2.column;

左连接(或左外连接)

返回左表中的所有行以及右表中的匹配行。如果不匹配,右表中的列将显示 null 值。

select columns
from table1
left join table2
on table1.column = table2.column;

右连接(或右外连接)

返回右表中的所有行以及左表中的匹配行。如果不匹配,左表中的列将显示 null 值。

select columns
from table1
right join table2
on table1.column = table2.column;

全外连接

当其中一个表中有匹配项时返回行。

select columns
from table1
full outer join table2
on table1.column = table2.column;

子查询

select 中的子查询

select column1, (select column2 from table2 where condition) as alias
from table1;

where 中的子查询

select column1
from table1
where column2 in (select column2 from table2 where condition);

from 中的子查询

select alias.column1
from (select column1 from table2 where condition) as alias;

索引

创建索引

create index index_name
on table_name (column1, column2);

删除索引

drop index index_name;

唯一索引

确保一列(或一组列)中的所有值都是唯一的。

create unique index index_name
on table_name (column_name);

聚合函数

数数

计算符合特定条件的行数。

select count(*) from table_name where condition;

返回列中值的总和。

select sum(column_name) from table_name;

平均电压

返回列中值的平均值。

select avg(column_name) from table_name;

最小值和最大值

返回列中的最小值和最大值。

select min(column_name), max(column_name) from table_name;

分组和排序

分组依据

将具有相同值的行分组为汇总行。

select column1, count(*)
from table_name
group by column1;

拥有

应用 group by 后过滤组。

select column1, count(*)
from table_name
group by column1
having count(*) > 5;

订购依据

按升序或降序对结果集进行排序。

select column1, column2
from table_name
order by column1 desc;

交易

开始交易

begin transaction;

进行交易

commit;

回滚事务

rollback;

高级sql

案例当

查询中的条件逻辑。

select column1,
       case
           when condition then 'result 1'
           when condition then 'result 2'
           else 'default'
       end as alias
from table_name;

联合和联合全部

union:合并两个或多个查询的结果集(删除重复项)。 union all:合并结果集(保留重复项)。
select column from table1
union
select column from table2;
select column from table1
union all
select column from table2;

最佳实践

尽可能使用 join 而不是子查询以获得更好的性能。 对经常搜索的列建立索引以加快查询速度。 避免 select * 并仅指定您需要的列。 对大型结果集使用 limit 限制返回的行数。 标准化您的数据以避免冗余并提高一致性。 使用where子句而不是在聚合之前过滤数据。 测试查询性能,特别是对于大型数据集。 使用事务来保证数据的一致性,尤其是涉及多个dml语句的操作。

结论

此 sql 备忘单涵盖了使用关系数据库所需的所有基本 sql 命令和技术。无论您是查询、插入、更新还是连接数据,本指南都将帮助您更有效地使用 sql。



          
            
        					

相关推荐