SQL must know must be the first

first lesson

        Primary key: A column or set of columns whose value uniquely identifies each row in a table

        1. No two rows have the same primary key value

        2. Each row must have a primary key value (NULL is not allowed)

        3. The value in the primary key column is not allowed to be modified or updated

        4. The primary key value cannot be reused (if a row is deleted from the table, its primary key cannot be assigned to a new row in the future)


SQL Server is used, and the database relationship diagram used is given before starting the formal exercise:



Second lesson

        This lesson describes how to use the SELECT statement to retrieve one or more columns of data from a table

        The main code is as follows:

select prod_name
from products;
select prod_id, prod_name, prod_price
from products;
select *
from products;
select distinct vend_id
from products;
select top 5 prod_name
from Products;
select prod_name -- this is a comment
from Products;

        1. SQL is not case sensitive

        2. (*) means wildcard, means to return all columns in the table, it is best not to use * wildcard

        3. The DISTINCT keyword instructs the database to only return different values, and it acts on all columns, and cannot be partially used

        4. In SQL Server, use the TOP keyword to limit the maximum number of rows returned

        5. The first row to be retrieved is row 0, not row 1. Pay attention here

        6. Comments can be used with -- or #

Summary: This lesson shows how to use SQL's SELECT statement to retrieve a single table column, multiple table columns, and all table columns. How to return different values, how to comment code. Unfortunately, more complex SQL makes SQL code less portable.


Lesson Three

        This lesson teaches how to use the ORDER BY clause of the SELECT statement to sort the retrieved data as needed

        The main code is as follows:

select prod_name
from products
order by prod_name;
select prod_id, prod_price, prod_name
from products
order by prod_price, prod_name;
select prod_id, prod_price, prod_name
from Products
order by 2, 3;
select prod_id, prod_price, prod_name
from Products
order by prod_price desc, prod_name;

        1. The ORDER BY statement should be the last sentence in the SELECT statement

        2. You can use non-retrievable columns for sorting

        3. When sorting by multiple columns, the sorting order is as specified

        4. You can sort by relative column position

        5. The ORDER BY statement is sorted in ascending order by default. If you want to sort in descending order, use the DESC keyword. The DESC keyword only acts on the column name directly in front of it.

        6. If you need case sensitivity and sort order, ask your database administrator for help

Summary: This lesson shows how to use the ORDER BY clause of the SELECT statement to sort the retrieved data. This clause must be the last clause in the SELECT statement. It can be used to sort data on one or more columns as needed.


lesson Four

        This lesson teaches how to specify search conditions using the WHERE clause of the SELECT statement

        The main code is as follows:

select prod_name, prod_price
from products
where prod_price = 3.49;
select prod_name, prod_price
from Products
where prod_price < 10;
select prod_name, prod_price
from Products
where prod_price <= 10;
select vend_id, prod_name
from Products
where vend_id <> 'DLL01';
select vend_id, prod_name
from Products
where vend_id != 'DLL01';
select prod_name, prod_price
from Products
where prod_price between 5 and 10;
select prod_name
from Products
where prod_price is null;
select cust_name
from Customers
where cust_email is null;

        1. In the SELECT statement, the data is filtered according to the search criteria specified in the WHERE clause

        2. When using the ORDER BY clause and the WHERE clause at the same time, the ORDER BY clause should be placed after the WHERE

        3. Operator compatibility depends on the DBMS

        4. Single quotation marks are used to delimit strings. If the value is compared with a column of type string, single quotation marks are required

        5. When you filter to select all rows that do not contain the specified value, you may want to return rows with NULL values, but this cannot be done. When filtering data, be sure to verify that rows containing NULL in the filtered column do appear in the returned data

Summary: This lesson shows how to use the WHERE clause of the SELECT statement to filter the returned data. Also, how to check for equality, inequality, greater than, less than, range of values, NULL values, etc.


fifth lesson

        This lesson teaches how to combine WHERE clauses to build more powerful and advanced search conditions. Also, you will learn how to use the NOT and IN operators

        The main code is as follows:

select prod_id, prod_price, prod_name
from Products
where vend_id = 'DLL01' and prod_price <= 4;
select prod_name, prod_price
from Products
where vend_id = 'DLL01' or vend_id = 'BRS01';
select prod_name, prod_price
from Products
where (vend_id = 'DLL01' or vend_id = 'BRS01')
	  and prod_price >= 10;
select prod_name, prod_price
from Products
where vend_id in ('DLL01', 'BRS01')
order by prod_name;
select prod_name
from Products
where not vend_id = 'DLL01'
order by prod_name;

        1. When using operators with AND and OR, parentheses should be used to explicitly group, and do not rely on the default evaluation order (AND first, OR)

        2. IN and NOT operators have more advantages, use as many as possible

Summary: This lesson teaches how to combine WHERE clauses with AND and OR operators. It also teaches how to explicitly manage evaluation order, and how to use IN and NOT operators.


Lesson Six

        This lesson explains what wildcards are, how to use them, and how to use the LIKE operator to perform wildcard searches for complex filtering of data

        The main code is as follows:

select prod_id, prod_name
from Products
where prod_name like 'fish%';
select prod_id, prod_name
from Products
where prod_name like '%bean bag%';
select prod_name
from Products
where prod_name like 'f%y';
select prod_id, prod_name
from Products
where prod_name like '__ inch teddy bear';
select prod_id, prod_name
from Products
where prod_name like '%inch teddy bear';
select cust_contact from Customers
where cust_contact like '[jm]%'
order by cust_contact;
select cust_contact
from Customers
where cust_contact like '[^jm]%'
order by cust_contact;
select cust_contact
from Customers
where not cust_contact like '[jm]%'
order by cust_contact;

        1. Wildcards are special characters used to match a part of

        2. The LIKE operator instructs the DBMS that subsequent search patterns use wildcard matching instead of simple equality matching

        3. Card search can only be used for text fields (strings), non-text data type fields cannot use wildcard searches

        4. % means any number of occurrences of any character, representing 0, 1 or more characters at a given position in the search pattern

        5. If the DBMS fills the content of the field with spaces, a statement such as 'f%y' will fail to retrieve the desired result. The simple solution is to add another % sign to the search pattern: 'f%y%' , a better solution is to use a function (see Lesson 8)

        6. Wildcard % cannot match NULL, NULL means does not exist

        7. Wildcard _ only matches a single character at a time, not multiple characters

        8. When using __, be careful to add a space after it, otherwise the space is also a character will be matched and an error will occur    

        9. [jm] matches any character in the brackets, it can only match a single character, this wildcard can be negated with the prefix character ^

        10. Wildcard searches take longer than other searches discussed earlier, don’t overuse wildcards

        11. If you really want to use a wildcard, try not to put it at the beginning of the search pattern, this is the slowest

        12. Pay careful attention to the placement of wildcards

Summary: This lesson introduces what wildcards are, how to use SQL wildcards in the WHERE clause, and also explains that wildcards should be used carefully and not overused



        


        

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325795784&siteId=291194637