SQL基础知识V2——SQL语句快速参考

SQL专栏

SQL数据库基础知识汇总

SQL数据库高级知识汇总

以下是一些可以当工具书使用的参考语句,都是固定写法,需要的时候可以拿出来参考一下,一些不懂的可以暂时先跳过,紫色代码均是SQL语句的关键字~

AND / OR 并且/或

SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition

ALTER TABLE 修改表

ALTER TABLE table_name
ADD column_name datatype
--或
ALTER TABLE table_name
DROP COLUMN column_name

AS (alias) 重命名

--对列名进行重命名
SELECT column_name AS column_alias
FROM table_name
--对表名进行重命名
SELECT column_name
FROM table_name  AS table_alias

BETWEEN...AND... 在某个区间


SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

CREATE DATABASE 创建数据库

CREATE DATABASE database_name

CREATE TABLE 创建表

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)

CREATE INDEX 创建索引

--创建普通索引
CREATE INDEX index_name
ON table_name (column_name)
--创建唯一索引
CREATE UNIQUE INDEX index_name
ON table_name (column_name)

CREATE VIEW 创建视图

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

DELETE 删除

--带条件的删除
DELETE FROM table_name
WHERE some_column=some_value
--不带条件的删除
DELETE FROM table_name

DROP DATABASE 删除数据库


DROP DATABASE database_name

DROP INDEX 删除索引


--SQL Server的写法
DROP INDEX table_name.index_name
--DB2/Oracle的写法
DROP INDEX index_name
--Mysql的写法
ALTER TABLE table_name
DROP INDEX index_name

DROP TABLE 删除表


DROP TABLE table_name

GROUP BY 分组


SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

HAVING 分组过滤条件

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value

IN 在某些固定值中查找

SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)

INSERT INTO 插入表


INSERT INTO table_name
VALUES (value1, value2, value3,....)
--或
INSERT INTO table_name
(column1, column2, column3,...)
VALUES (value1, value2, value3,....)

INNER JOIN 内连接


SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

LEFT JOIN 左连接

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

RIGHT JOIN 右连接

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

FULL JOIN 全连接

SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name

LIKE 模糊匹配

SELECT column_name(s)
FROM table_name
WHERE column_nameLIKE pattern

ORDER BY 排序


SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]

SELECT 查找


SELECT column_name(s)
FROM table_name

*SELECT 查找表的所有字段**

SELECT *
FROM table_name

SELECT DISTINCT 查找去除重复的列

SELECT DISTINCT column_name(s)
FROM table_name

SELECT INTO 查询结果插入表


SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_table_name
--或
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_table_name

SELECT TOP 查询前N条记录

SELECT TOP number|percent column_name(s)
FROM table_name

TRUNCATE TABLE 清空表

TRUNCATE TABLE table_name

UNION 去重并集


SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

UNION ALL 不去重并集

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

UPDATE 更新


UPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value

WHERE 条件过滤


SELECT column_name(s)
FROM table_name
WHERE column_name operator value
上一篇:报错注入


下一篇:GO富集分析示例