• 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: select is the same as SELECT SQL 关键字不区分大小写: select 与 SELECT 是相同的
    • 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 DISTINCT statement is used to return olny distinct (different) values. SELECT DISTINCT 语句用于返回不同的值(也可以理解为去重
      • Syntax

        • sql ↓
        	SELECT DISTINCT *column1*,* column2, ...*
        	FROM *table_name*;
      • Count Distinct 统计不同

        • By using the DISTINCT keyword is a function called COUNT, we can return the number of different countries.
      • 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);
    • SQL WHERE Clause

      • Example

        • SELECT * FROM Customers WHERE Country=‘Mexico’;
      • Syntax 语法

        • SELECT column1,* column2, …* FROM table_name WHERE condition;
        • Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE,DELETE, etc.! 注意: WHERE 子句不仅用于 SELECT 语句,还用于 UPDATE 、 DELETE 等!
    • SQL   ORDER BY   Keyword

      • The ORDER BY keyword 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;
    • SQL   AND   Operator

      • The WHERE clause can contain one or many AND operators. 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 AND operator displays a record if all the conditions are TRUE. AND 运算符在所有条件都为 TRUE 时显示记录。
        • The OR operator displays a record if any of the conditions are TRUE. OR 运算符在任何条件为 TRUE 时显示记录。
      • Combining AND and OR

        • You can combine the AND and OR operators. 你可以结合 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%‘;
    • SQL   OR   Operator

      • The WHERE clause can contain one or more OR operators. WHERE 子句可以包含一个或多个 OR 运算符。
      • Syntax 语法

        • sql ↓
        	SELECT *column1*,* column2, ...*
        	FROM *table_name*
        	WHERE *condition1* OR *condition2* OR *condition3 ...*;	
    • SQL   NOT   Operator

      • The NOT operator 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*;
    • SQL   INSERT INTO   Statement

      • The INSERT INTO statement is used to insert new records in a table. INSERT INTO 语句用于在表中插入新记录。
      • INSERT INTO Syntax

        • It is possible to write the INSERT INTO statement in two ways: 可以用两种方式编写 INSERT INTO 语句:
            1. Specify both the column names and the values to be inserted:
            2. 指定列名和要插入的值:
            • sql ↓
            	INSERT INTO *table_name* (*column1*,* column2*,* column3*, ...)
            	VALUES (*value1*,* value2*,* value3*, ...);
            1. 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 INTO syntax would be as follows:
            2. 如果你为表的所有列添加值,则不需要在 SQL 查询中指定列名。但是,请确保值的顺序与表中的列顺序相同。这里 INSERT INTO 语法如下:
            • sql ↓
            	INSERT INTO table_name
            	VALUES (value1, value2, value3, ...);
    • The IS NOT Null Operator#

      • The IS NOT NULL operator 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;
    • SQL   UPDATE   Statement

      • The SQL UPDATE Statement

        • The UPDATE statement is used to modify the existing records in a table. UPDATE 语句用于修改表中的现有记录。
      • UPDATE Syntax

        • UPDATE table_name SET column1 = value1,* column2 = value2*, … WHERE condition;
      • Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHEREclause specifies which record(s) that should be updated. If you omit the WHERE clause, 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(数据定义语言)