SQL stands for Structured Query Language. SQL commands are the instructions used to communicate with a database to perform tasks, functions, and queries with data.

SQL代表结构化查询语言。 SQL命令是用于与数据库通信以执行任务,功能和数据查询的指令。

SQL commands can be used to search the database and to do other functions like creating tables, adding data to tables, modifying data, and dropping tables.

SQL命令可用于搜索数据库并执行其他功能,例如创建表,向表中添加数据,修改数据和删除表。

Here is a list of basic SQL commands (sometimes called clauses) you should know if you are going to work with SQL.

这是基本SQL命令(有时称为子句)的列表,您应该知道是否要使用SQL。

选择和从 (SELECT and FROM)

The SELECT part of a query determines which columns of the data to show in the results. There are also options you can apply to show data that is not a table column.

查询的SELECT部分确定要在结果中显示数据的哪些列。 您还可以应用其他选项来显示不是表列的数据。

The example below shows three columns SELECTed FROM the “student” table and one calculated column. The database stores the studentID, FirstName, and LastName of the student. We can combine the First and the Last name columns to create the FullName calculated column.

下例显示了FROM “学生”表中SELECT三列和一个计算所得的列。 该数据库存储该学生的studentID,FirstName和LastName。 我们可以组合名字和姓氏列来创建FullName计算列。

SELECT studentID, FirstName, LastName, FirstName + ' ' + LastName AS FullName
FROM student;
+-----------+-------------------+------------+------------------------+
| studentID | FirstName         | LastName   | FullName               |
+-----------+-------------------+------------+------------------------+
|         1 | Monique           | Davis      | Monique Davis          |
|         2 | Teri              | Gutierrez  | Teri Gutierrez         |
|         3 | Spencer           | Pautier    | Spencer Pautier        |
|         4 | Louis             | Ramsey     | Louis Ramsey           |
|         5 | Alvin             | Greene     | Alvin Greene           |
|         6 | Sophie            | Freeman    | Sophie Freeman         |
|         7 | Edgar Frank "Ted" | Codd       | Edgar Frank "Ted" Codd |
|         8 | Donald D.         | Chamberlin | Donald D. Chamberlin   |
|         9 | Raymond F.        | Boyce      | Raymond F. Boyce       |
+-----------+-------------------+------------+------------------------+
9 rows in set (0.00 sec)

创建表 (CREATE TABLE)

CREATE TABLE does just what it sounds like: it creates a table in the database. You can specify the name of the table and the columns that should be in the table.

CREATE TABLE功能听起来很像:它在数据库中创建一个表。 您可以指定表的名称以及应在表中的列。

CREATE TABLE table_name (column_1 datatype,column_2 datatype,column_3 datatype
);

更改表 (ALTER TABLE)

ALTER TABLE changes the structure of a table. Here is how you would add a column to a database:

ALTER TABLE更改ALTER TABLE的结构。 这是将列添加到数据库的方法:

ALTER TABLE table_name
ADD column_name datatype;

检查 (CHECK)

The CHECK constraint is used to limit the value range that can be placed in a column.

CHECK约束用于限制可以放置在列中的值范围。

If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

如果在单个列上定义CHECK约束,则该列仅允许某些值。 如果在表上定义CHECK约束,则可以基于行中其他列中的值来限制某些列中的值。

The following SQL creates a CHECK constraint on the “Age” column when the “Persons” table is created. The CHECK constraint ensures that you can not have any person below 18 years.

创建“人员”表时,以下SQL在“年龄”列上创建CHECK约束。 CHECK约束确保您不能有18岁以下的任何人。

CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int,CHECK (Age>=18)
);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:

若要命名CHECK约束,并在多个列上定义CHECK约束,请使用以下SQL语法:

CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int,City varchar(255),CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);

哪里 (WHERE)

(AND, OR, IN, BETWEEN, and LIKE)

( ANDOR INBETWEENLIKE )

The WHERE clause is used to limit the number of rows returned.

WHERE子句用于限制返回的行数。

As an example, first we will show you a SELECT statement and results without a WHERE statement. Then we will add a WHERE statement that uses all five qualifiers above.

作为示例,首先我们将向您显示SELECT语句和WHERE语句的结果。 然后,我们将添加一个使用上面所有五个限定词的WHERE语句。

SELECT studentID, FullName, sat_score, rcd_updated FROM student;
+-----------+------------------------+-----------+---------------------+
| studentID | FullName               | sat_score | rcd_updated         |
+-----------+------------------------+-----------+---------------------+
|         1 | Monique Davis          |       400 | 2017-08-16 15:34:50 |
|         2 | Teri Gutierrez         |       800 | 2017-08-16 15:34:50 |
|         3 | Spencer Pautier        |      1000 | 2017-08-16 15:34:50 |
|         4 | Louis Ramsey           |      1200 | 2017-08-16 15:34:50 |
|         5 | Alvin Greene           |      1200 | 2017-08-16 15:34:50 |
|         6 | Sophie Freeman         |      1200 | 2017-08-16 15:34:50 |
|         7 | Edgar Frank "Ted" Codd |      2400 | 2017-08-16 15:35:33 |
|         8 | Donald D. Chamberlin   |      2400 | 2017-08-16 15:35:33 |
|         9 | Raymond F. Boyce       |      2400 | 2017-08-16 15:35:33 |
+-----------+------------------------+-----------+---------------------+
9 rows in set (0.00 sec)

Now, we'll repeat the SELECT query but we'll limit the rows returned using a WHERE statement.

现在,我们将重复执行SELECT查询,但是将限制使用WHERE语句返回的行。

STUDENT studentID, FullName, sat_score, recordUpdated
FROM student
WHERE (studentID BETWEEN 1 AND 5 OR studentID = 8)ANDsat_score NOT IN (1000, 1400);
+-----------+----------------------+-----------+---------------------+
| studentID | FullName             | sat_score | rcd_updated         |
+-----------+----------------------+-----------+---------------------+
|         1 | Monique Davis        |       400 | 2017-08-16 15:34:50 |
|         2 | Teri Gutierrez       |       800 | 2017-08-16 15:34:50 |
|         4 | Louis Ramsey         |      1200 | 2017-08-16 15:34:50 |
|         5 | Alvin Greene         |      1200 | 2017-08-16 15:34:50 |
|         8 | Donald D. Chamberlin |      2400 | 2017-08-16 15:35:33 |
+-----------+----------------------+-----------+---------------------+
5 rows in set (0.00 sec)

更新 (UPDATE)

To update a record in a table you use the UPDATE statement.

要更新表中的记录,请使用UPDATE语句。

Use the WHERE condition to specify which records you want to update. It is possible to update one or more columns at a time. The syntax is:

使用WHERE条件指定要更新的记录。 可以一次更新一个或多个列。 语法为:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Here is an example updating the Name of the record with Id 4:

这是一个使用ID 4更新记录名称的示例:

UPDATE Person
SET Name = “Elton John”
WHERE Id = 4;

You can also update columns in a table by using values from other tables. Use the JOIN clause to get data from multiple tables. The syntax is:

您还可以通过使用其他表中的值来更新表中的列。 使用JOIN子句从多个表中获取数据。 语法为:

UPDATE table_name1
SET table_name1.column1 = table_name2.columnAtable_name1.column2 = table_name2.columnB
FROM table_name1
JOIN table_name2 ON table_name1.ForeignKey = table_name2.Key

Here is an example updating Manager of all records:

这是所有记录的更新管理器的示例:

UPDATE Person
SET Person.Manager = Department.Manager
FROM Person
JOIN Department ON Person.DepartmentID = Department.ID

通过...分组 (GROUP BY)

GROUP BY allows you to combine rows and aggregate data.

GROUP BY使您可以合并行并汇总数据。

Here is the syntax of GROUP BY:

这是GROUP BY的语法:

SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name;

拥有 (HAVING)

HAVING allows you to filter the data aggregated by the GROUP BY clause so that the user gets a limited set of records to view.

HAVING允许您过滤GROUP BY子句聚合的数据,以便用户获得一组有限的记录以供查看。

Here is the syntax of HAVING:

这是HAVING的语法:

SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > value;

AVG() (AVG())

“Average” is used to calculate the average of a numeric column from the set of rows returned by a SQL statement.

“平均值”用于从SQL语句返回的行集中计算数字列的平均值。

Here is the syntax for using the function:

这是使用该函数的语法:

SELECT groupingField, AVG(num_field)
FROM table1
GROUP BY groupingField

Here’s an example using the student table:

这是使用学生表的示例:

SELECT studentID, FullName, AVG(sat_score)
FROM student
GROUP BY studentID, FullName;

如 (AS)

AS allows you to rename a column or table using an alias.

AS允许您使用别名重命名列或表。

SELECT user_only_num1 AS AgeOfServer, (user_only_num1 - warranty_period) AS NonWarrantyPeriod FROM server_table

This results in output as below.

结果如下。

+-------------+------------------------+
| AgeOfServer | NonWarrantyPeriod      |
+-------------+------------------------+
|         36  |                     24 |
|         24  |                     12 |
|         61  |                     49 |
|         12  |                      0 |
|          6  |                     -6 |
|          0  |                    -12 |
|         36  |                     24 |
|         36  |                     24 |
|         24  |                     12 |
+-------------+------------------------+

You can also use AS to assign a name to a table to make it easier to reference in joins.

您还可以使用AS为表分配名称,以使其更易于在联接中引用。

SELECT ord.product, ord.ord_number, ord.price, cust.cust_name, cust.cust_number FROM customer_table AS custJOIN order_table AS ord ON cust.cust_number = ord.cust_number

This results in output as below.

结果如下。

+-------------+------------+-----------+-----------------+--------------+
| product     | ord_number | price     | cust_name       | cust_number  |
+-------------+------------+-----------+-----------------+--------------+
|     RAM     |   12345    |       124 | John Smith      |  20          |
|     CPU     |   12346    |       212 | Mia X           |  22          |
|     USB     |   12347    |        49 | Elise Beth      |  21          |
|     Cable   |   12348    |         0 | Paul Fort       |  19          |
|     Mouse   |   12349    |        66 | Nats Back       |  15          |
|     Laptop  |   12350    |       612 | Mel S           |  36          |
|     Keyboard|   12351    |        24 | George Z        |  95          |
|     Keyboard|   12352    |        24 | Ally B          |  55          |
|     Air     |   12353    |        12 | Maria Trust     |  11          |
+-------------+------------+-----------+-----------------+--------------+

订购 (ORDER BY)

ORDER BY gives us a way to sort the result set by one or more of the items in the SELECT section. Here is an SQL sorting the students by FullName in descending order. The default sort order is ascending (ASC) but to sort in the opposite order (descending) you use DESC.

ORDER BY提供了一种方法,可以按SELECT部分中的一个或多个项目对结果集进行排序。 这是一个按FullName降序对学生进行排序SQL。 默认的排序顺序是升序( ASC ),但要使用相反的顺序(降序),请使用DESC

SELECT studentID, FullName, sat_score
FROM student
ORDER BY FullName DESC;

计数 (COUNT)

COUNT will count the number of rows and return that count as a column in the result set.

COUNT将对行数进行计数,并将该计数作为列返回到结果集中。

Here are examples of what you would use COUNT for:

以下是将COUNT用于以下用途的示例:

  • Counting all rows in a table (no group by required)计算表中的所有行(不需要按组)
  • Counting the totals of subsets of data (requires a Group By section of the statement)计算数据子集的总数(需要语句的“分组依据”部分)

This SQL statement provides a count of all rows. Note that you can give the resulting COUNT column a name using “AS”.

该SQL语句提供所有行的计数。 请注意,您可以使用“ AS”为所得的COUNT列命名。

SELECT count(*) AS studentCount FROM student;

删除 (DELETE)

DELETE is used to delete a record in a table.

DELETE用于删除表中的记录。

Be careful. You can delete all records of the table or just a few. Use the WHERE condition to specify which records you want to delete. The syntax is:

小心。 您可以删除表中的所有记录或仅删除一些记录。 使用WHERE条件指定要删除的记录。 语法为:

DELETE FROM table_name
WHERE condition;

Here is an example deleting from the table Person the record with Id 3:

这是从表Person中删除ID为3的记录的示例:

DELETE FROM Person
WHERE Id = 3;

内部联接 (INNER JOIN)

JOIN, also called Inner Join, selects records that have matching values in two tables.

JOIN (也称为内部联接)选择两个表中具有匹配值的记录。

SELECT * FROM A x JOIN B y ON y.aId = x.Id

左联接 (LEFT JOIN)

A LEFT JOIN returns all rows from the left table, and the matched rows from the right table. Rows in the left table will be returned even if there was no match in the right table. The rows from the left table with no match in the right table will have null for right table values.

LEFT JOIN返回左侧表中的所有行,以及右侧表中的匹配行。 即使右表中没有匹配项,也将返回左表中的行。 左表中没有匹配项的行在右表中将为null对于右表值。

SELECT * FROM A x LEFT JOIN B y ON y.aId = x.Id

正确加入 (RIGHT JOIN)

A RIGHT JOIN returns all rows from the right table, and the matched rows from the left table. Opposite of a left join, this will return all rows from the right table even where there is no match in the left table. Rows in the right table that have no match in the left table will have null values for left table columns.

RIGHT JOIN返回右侧表中的所有行,以及左侧表中的匹配行。 与左联接相反,这将返回右表中的所有行,即使左表中没有匹配项也是如此。 右表中与左表不匹配的行的左表列将具有null值。

SELECT * FROM A x RIGHT JOIN B y ON y.aId = x.Id

全外连接 (FULL OUTER JOIN)

A FULL OUTER JOIN returns all rows for which there is a match in either of the tables. So if there are rows in the left table that do not have matches in the right table, those will be included. Also, if there are rows in the right table that do not have matches in the left table, those will be included.

FULL OUTER JOIN返回所有表中都匹配的所有行。 因此,如果左表中的行与右表中的行不匹配,则将这些行包括在内。 另外,如果右表中的行与左表中的行不匹配,则将这些行包括在内。

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName

插 (INSERT)

INSERT is a way to insert data into a table.

INSERT是一种将数据插入表中的方法。

INSERT INTO table_name (column_1, column_2, column_3)
VALUES (value_1, 'value_2', value_3);

喜欢 (LIKE)

LIKE  is used in a WHERE or HAVING (as part of the GROUP BY) to limit the selected rows to the items when a column has a certain pattern of characters contained in it.

LIKEWHEREHAVING (作为GROUP BY一部分)用于在列中包含某些特定字符模式的情况下,将选定的行限制为项目。

This SQL will select students that have FullName starting with “Monique” or ending with “Greene”.

这个SQL将选择具有学生FullName开头“莫妮克”或“格林”的结局。

SELECT studentID, FullName, sat_score, rcd_updated
FROM student
WHERE FullName LIKE 'Monique%' OR FullName LIKE '%Greene';
+-----------+---------------+-----------+---------------------+
| studentID | FullName      | sat_score | rcd_updated         |
+-----------+---------------+-----------+---------------------+
|         1 | Monique Davis |       400 | 2017-08-16 15:34:50 |
|         5 | Alvin Greene  |      1200 | 2017-08-16 15:34:50 |
+-----------+---------------+-----------+---------------------+
2 rows in set (0.00 sec)

You can place NOT before LIKE to exclude the rows with the string pattern instead of selecting them. This SQL excludes records that contain “cer Pau” and “Ted” in the FullName column.

你可以把NOTLIKE与字符串模式排除行,而不是选择他们的。 该SQL排除FullName列中包含“ cer Pau”和“ Ted”的记录。

SELECT studentID, FullName, sat_score, rcd_updated
FROM student
WHERE FullName NOT LIKE '%cer Pau%' AND FullName NOT LIKE '%"Ted"%';
+-----------+----------------------+-----------+---------------------+
| studentID | FullName             | sat_score | rcd_updated         |
+-----------+----------------------+-----------+---------------------+
|         1 | Monique Davis        |       400 | 2017-08-16 15:34:50 |
|         2 | Teri Gutierrez       |       800 | 2017-08-16 15:34:50 |
|         4 | Louis Ramsey         |      1200 | 2017-08-16 15:34:50 |
|         5 | Alvin Greene         |      1200 | 2017-08-16 15:34:50 |
|         6 | Sophie Freeman       |      1200 | 2017-08-16 15:34:50 |
|         8 | Donald D. Chamberlin |      2400 | 2017-08-16 15:35:33 |
|         9 | Raymond F. Boyce     |      2400 | 2017-08-16 15:35:33 |
+-----------+----------------------+-----------+---------------------+
7 rows in set (0.00 sec)

翻译自: https://www.freecodecamp.org/news/basic-sql-commands/

基本SQL命令-您应该知道的数据库查询和语句列表相关推荐

  1. 【转】SQL SERVER函数无法执行对数据库的修改语句

    当在函数中使用INSERT语句时出现以下错误: 消息 443,级别 16,状态 15,过程 MyTestFunction,第 9 行 在函数内的 'INSERT' 中对带副作用的或依赖于时间的运算符的 ...

  2. 数据库查询常用语句语法

    select xx from 表格 where 内容="" 检查一个项目是否在列表中 可以用in 列表,用(' ' , ' ' ,' ')单引号分隔开 名字不在里面 name no ...

  3. MySQL数据库 第0章:每章SQL命令总结

    MySQL数据库 第0章:每章SQL命令总结 MySQL数据库 第1章:数据库入门 MySQL数据库 第2章:数据库基本操作 MySQL数据库 第3章:数据类型与约束 MySQL数据库 第4章:数据库 ...

  4. mysql数据库查询工具''_数据库查询工具

    多肉数据查询系统是一款非常实用的查询多肉植物小工具,软件提供了多肉植物的习性.照片.如何培养等等功能,对于喜欢在家里养些花花草草的用户会有很大的帮助,操作也很简单,基本一看就会.接下来非凡软件站小编就 ...

  5. iBatis.Net(C#)数据库查询

    引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html  摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...

  6. oracle查询并更新数据库,oracle数据库查询和更新

    package sqltest; import java.sql.*; import parameter.BaseCanShu; public class PublicDbOracle { stati ...

  7. java方法里执行两条sql_Java如何在数据库上执行多个SQL命令?

    在Java编程中,如何同时在数据库上执行多个SQL命令?假定数据库名称是:testdb,其中有两张表:employee和dept,employee表中有4条记录,dept表中有2条记录. 创建数据库表 ...

  8. Navicat 使用sql命令建数据库和表详细过程_2

    /** *@author blovedr *功能: Navicat 使用sql命令建数据库和表详细过程_2 *日期: 2018年7月24日   10:39 *注释: 学习数据库MySQL的点点记录, ...

  9. SQL:使用备份向导、SQL命令、导出数据三种方式对已建立的数据库进行备份

    SQL:使用备份向导.SQL命令.导出数据三种方式对已建立的数据库进行备份 目录 题目要求 解题步骤 题目要求 使用备份向导备份数据库 "DEMO_4_2_2_1"到考生文件夹&q ...

最新文章

  1. 她是985高校的90后博导!常被认成“师姐”,喜欢减压式教学
  2. Jira停售Server版政策客观解读——如何最小化风险?
  3. C语言之父,UNIX之父丹尼斯·里奇去世 享年70岁
  4. “打头办”不在百度,在民间
  5. 不要把时间画在抽奖上。。。去学习吧。。。
  6. ASP.NET页面对象模型:Page类介绍
  7. ajax框架dwr开发
  8. 模块(sys/os/序列化模块)
  9. 【Flink】kafka FlinkKafkaException send data to Kafka old epoch newer producer same transactionalId
  10. java名称服务_java – UnknownHostException:名称或服务未知
  11. java中使用switch case报错case expressions must be constant expressions
  12. Android内存优化11 内存泄漏常见情况2 线程持久化
  13. 拉结尔6月21日服务器维护,《拉结尔》6月11日更新公告
  14. python调用java完整教程
  15. 详尽的msf——meterpreter——渗透测试教程
  16. NPY and girls (HDU - 5145,莫队算法 + 乘法逆元)
  17. php后门文件,php后门屌炸天
  18. 性能测试入门指南 (慎入: 6000 字长文)
  19. php面试题之三——PHP语言基础(基础部分)
  20. 如何使用物联网低代码平台进行设备调试?

热门文章

  1. 【C++ Priemr | 15】虚函数表剖析(二)
  2. 设计模式(3):抽象工厂模式(Abstract Factory Pattern)
  3. 目前最全的《Android面试题及解析》!面试真题解析
  4. sqlserver游标概念与实例全面解说
  5. Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)
  6. Struts1和Struts2的区别和对比(完整版)(转)
  7. 安装设置Android Studio Win7安装
  8. 一个学者科研的感受,推荐一下(小木虫)
  9. 【求助】windows server 2003 64位下面网卡IP总是默认为动态分派IP
  10. NHibernate 做个小项目来试一下吧 一