SQL语法整理

Posted Aquarium

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语法整理相关的知识,希望对你有一定的参考价值。

1.CREATE
创建数据库:CREATE DATABASE db_name
创建表:CREATE TABLE table_name (col_name col_type [NOT NULL] [PRIMARY KEY], ...)

2.DROP
删除表:DROP TABLE table_name

3.ALTER
添加列:ALTER TABLE table_name ADD COLUMN col_type
添加主键:ALTER TABLE table_name ADD PRIMARY KEY(col_name)

4.INSERT
插入记录:INSERT INTO table_name (col_name, ...) VALUES (col_value, ...)

5.DELETE
删除记录:DELETE FROM table_name WHERE col_name=col_value
*safe update模式只能通过主键字段更新记录,更新同理

6.UPDATE
更新记录:UPDATE table_name SET col_name=col_value, ... WHERE ...

7.SELECT
查询部分字段:SELECT col_name, ... FROM table_name WHERE ...
查询全部字段:SELECT * FROM table_name WHERE ...

8.OUTER JOIN (表)
table_name1 LEFT/RIGHT OUTER JOIN table_name2
*左联查询把table1的内容全部查出,table2中只查出满足条件的内容,其他为null

9.INNER JOIN (表)
table_name1 INNER JOIN table_name2 ON table_name1.col_name1=table_name2.col_name2
*只查出满足条件的记录

10.ORDER BY
升序:SELECT ... ORDER BY col_name
降序:SELECT ... ORDER BY col_name DESC

11.聚合函数(字段)
和:sum(col_name)
平均:avg(col_name)
最大:max(col_name)
最小:min(col_name)
计数:count(col_name)

12.GROUP BY
某字段同值记录合并:GROUP BY col_name
eg. SELECT sum(col_name2) as new_col_name FROM table_name GROUP BY col_name

























以上是关于SQL语法整理的主要内容,如果未能解决你的问题,请参考以下文章

SQL语法整理

SQL语法整理

SQL语法整理

SQL语法整理

SQL基本语法整理(更新中)

SQL中部分语法整理