- SQL_Quick_Start
-
Introduction to SQL
- SQL is a standard language for accessing and manipulating databases. SQL 是一种用于访问和操作数据库的标准语言。
-
RDBMS 关系数据库管理系统
- RDBMS strands for Relational Database Management System
-
SQL Syntax
-
Keep in Mind That… 牢记以下几点…
- SQL keywrods are NOT case sensitive:
selectis the same asSELECTSQL 关键字不区分大小写:select与SELECT是相同的
- SQL keywrods are NOT case sensitive:
-
-
Some of The Most Important SQL Commands
SELECT- extracts data from a database 从数据库中提取UPDATE- updates data in a database 更新数据库中的数据DELETE- deletes data from a database 从数据库中删除INSERT INTO- inserts new data into a database 将新数据插入数据库CREATE DATABASE- creates a new database 创建新数据库ALTER DATABASE- modifies a database 修改数据库CREATE TABLE- creates a new table 创建新表ALTER TABLE- modifies a table 修改表DROP TABLE- deletes a table 删除一个表CREATE INDEX- creates an index (search key) 创建一个索引(搜索键)DROP INDEX- deletes an index 删除一个索引
-
SQL SELECT Statement
-
SQL SELECCT
- sql ↓
SELECT *column1*, *column2,...* FROM *table_name*; -
Select ALL columns
- sql ↓
SELECT *` FROM *table_name*;
-
-
SQL SELECT DISTINCT Statement
- The
SELECT DISTINCTstatement is used to return olny distinct (different) values.SELECT DISTINCT语句用于返回不同的值(也可以理解为去重) -
Syntax
- sql ↓
SELECT DISTINCT *column1*,* column2, ...* FROM *table_name*; -
Count Distinct 统计不同
- By using the
DISTINCTkeyword is a function calledCOUNT, we can return the number of different countries.
- By using the
-
Example
- SELECT COUNT(DISTINCT Country) FROM Customers;
- **Note: The **COUNT(DISTINCT column_name) is not supported in Microsoft Access databases. 注意:COUNT(DISTINCT column_name) 在 Microsoft Access 数据库中不受支持。
- Here is a workaround for MS Access: 这里是一个针对 MS Access 的解决方案:
- SELECT Count(*) AS DistinctCountries FROM (SELECT DISTINCT Country FROM Customers);
- The
-
SQL WHERE Clause
-
Example
- SELECT * FROM Customers WHERE Country=‘Mexico’;
-
Syntax 语法
- SELECT column1,* column2, …* FROM table_name WHERE condition;
- Note: The
WHEREclause is not only used inSELECTstatements, it is also used inUPDATE,DELETE, etc.! 注意:WHERE子句不仅用于SELECT语句,还用于UPDATE、DELETE等!
-
-
SQL ORDER BY Keyword
- The
ORDER BYkeyword is used to sort the result-set in ascending or descending order.ORDER BY关键字用于按升序或降序对结果集进行排序。 -
Syntax 语法
- SELECT column1,* column2, …* FROM table_name ORDER BY *column1, column2, … *ASC|DESC;
- The
-
SQL AND Operator
-
The
WHEREclause can contain one or manyANDoperators.WHERE子句可以包含一个或多个AND运算符。 -
Example 示例
Select all customers from Spain that starts with the letter ‘G’: 从西班牙选择所有以字母”G”开头的客户:
SELECT * FROM Customers WHERE Country = ‘Spain’ AND CustomerName LIKE ‘G%‘;
-
Syntax 语法
- SELECT column1,* column2, …* FROM table_name WHERE condition1 AND condition2 AND condition3 …;
-
AND vs OR
- The
ANDoperator displays a record if all the conditions are TRUE.AND运算符在所有条件都为 TRUE 时显示记录。 - The
ORoperator displays a record if any of the conditions are TRUE.OR运算符在任何条件为 TRUE 时显示记录。
- The
-
Combining AND and OR
- You can combine the
ANDandORoperators. 你可以结合AND和OR运算符。 - The following SQL statement selects all customers from Spain that starts with a “G” or an “R”. 以下 SQL 语句从西班牙选择所有以”G”或”R”开头的客户。
- Select all Spanish customers that starts with either “G” or “R”:
选择所有以”G”或”R”开头的西班牙客户:
- SELECT * FROM Customers WHERE Country = ‘Spain’ AND (CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%’);
- Select all customers that either:
选择所有符合以下任一条件的客户:
are from Spain and starts with either “G”, or
来自西班牙且姓名以”G”开头,或
starts with the letter “R”:
姓名以字母”R”开头:
- SELECT * FROM Customers WHERE Country = ‘Spain’ AND CustomerName LIKE ‘G%’ OR CustomerName LIKE ‘R%‘;
- You can combine the
-
-
SQL OR Operator
- The
WHEREclause can contain one or moreORoperators.WHERE子句可以包含一个或多个OR运算符。 -
Syntax 语法
- sql ↓
SELECT *column1*,* column2, ...* FROM *table_name* WHERE *condition1* OR *condition2* OR *condition3 ...*;
- The
-
SQL NOT Operator
- The
NOToperator is used in combination with other operators to give the opposite result, also called the negative result.NOT运算符与其他运算符结合使用,以给出相反的结果,也称为负结果。 -
Syntax
- sql ↓
SELECT *column1*,* column2, ...* FROM *table_name* WHERE NOT *condition*;
- The
-
SQL INSERT INTO Statement
- The
INSERT INTOstatement is used to insert new records in a table.INSERT INTO语句用于在表中插入新记录。 -
INSERT INTO Syntax
- It is possible to write the
INSERT INTOstatement in two ways: 可以用两种方式编写INSERT INTO语句:-
- Specify both the column names and the values to be inserted:
- 指定列名和要插入的值:
- sql ↓
INSERT INTO *table_name* (*column1*,* column2*,* column3*, ...) VALUES (*value1*,* value2*,* value3*, ...); -
- If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the
INSERT INTOsyntax would be as follows: - 如果你为表的所有列添加值,则不需要在 SQL 查询中指定列名。但是,请确保值的顺序与表中的列顺序相同。这里
INSERT INTO语法如下:
- sql ↓
INSERT INTO table_name VALUES (value1, value2, value3, ...); - If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the
-
- It is possible to write the
- The
-
The IS NOT Null Operator#
- The
IS NOT NULLoperator is used to test for non-empty values (NOT NULL values).IS NOT NULL运算符用于测试非空值(NOT NULL 值)。 -
Example
- SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL;
- The
-
SQL UPDATE Statement
-
The SQL UPDATE Statement
- The
UPDATEstatement is used to modify the existing records in a table.UPDATE语句用于修改表中的现有记录。
- The
-
UPDATE Syntax
- UPDATE table_name SET column1 = value1,* column2 = value2*, … WHERE condition;
- Note: Be careful when updating records in a table! Notice the
WHEREclause in theUPDATEstatement. TheWHEREclause specifies which record(s) that should be updated. If you omit theWHEREclause, all records in the table will be updated! id:: 6850cf22-4e2b-4fe7-9e53-eef10457eaeb 注意:在更新表中的记录时要小心!注意UPDATE语句中的WHERE子句。WHERE子句指定了要更新的记录。如果你省略了WHERE子句,表中的所有记录都将被更新! -
Example
- UPDATE Customers SET ContactName = ‘Alfred Schmidt’, City= ‘Frankfurt’ WHERE CustomerID = 1;
-
-
SQL 分为两个部分: DML(数据操作语言) & DDL(数据定义语言)