SQL must know the theory must be

These are theories that some good summary of concepts, review their own use, do not like do not spray, which I read a book summed up, please respect the fruits of labor, thank you

Learn SQL

Table is a structured document, used to store a particular type of data

Data stored in the table is the same one type of data or list

Each table in the database has a name to identify themselves. The name is unique, that no other database table with the same name.

This description is set so-called mode (Schema), the database model may be used to describe specific tables, it can be used to describe (and where the relationship between the tables) the entire database.

Worse, by chance have the same data types have different names in different DBMS. There is no way for this user, it is important to create a table structure to keep in mind these differences.

Retrieving data

Multiple SQL statements must be a semicolon (;) separated. Most DBMS does not need a semicolon after a single SQL statement, but there are DBMS may have to add a semicolon after a single SQL statement. Of course, if you prefer you can always add a semicolon. In fact, even if not necessarily need to, add a semicolon and certainly do no harm.
Tip: SQL statements and case
Please note, SQL statements are not case sensitive, so SELECT and select are the same. Similarly, written Select does not matter. Many developers prefer to use SQL to SQL keywords in uppercase, and lowercase table names and column names, this makes the code easier to read and debug. However, we must recognize that while SQL is not case sensitive, but the table names, column names and values may be different (this depends on the specific DBMS and how to configure).

Most DBMS does not need a semicolon after a single SQL statement, but there are DBMS may have to add a semicolon after a single SQL statement

Of course, if you prefer you can always add a semicolon. In fact, even if not necessarily need to, add a semicolon and certainly do no harm.

SQL statements are not case sensitive, so select the same SELECT

When processing SQL statements, all spaces are ignored

Most SQL developers believe that the SQL statement into multiple lines easier to read and debug.

In general, unless you really need each column of the table, it would be best not to use the * wildcard. Although the use of wildcards allow you to save yourself, do not explicitly list the desired column, but is not required to retrieve columns typically reduces performance and retrieval applications.

How many rows to return using TOP keyword to limit the most

Sort Your Data

The ORDER BY clause takes the name of one or more columns, sort the output accordingly

When specifying an ORDER BY clause, it should ensure that it is the last clause of the SELECT statement. If it is not the last clause, an error message will appear.

Presses several column sorting, simply specify the column names, column names separated by commas between the can (just as when selecting multiple columns).

ORDER BY 2 represents sorted by SELECT list of second row prod_price. ORDER BY 2, 3 means press prod_price, then prod_name sort.

First of all, do not explicitly given column names may cause the wrong sort by column name.

If you want to sort descending on multiple columns, you must specify the DESC keyword for each column.

ASC little use, because the default is ascending (If neither specified nor DESC ASC, ASC is assumed).

Filtering data

Single quotes used to define the string. If the value of the character string that is compared, it is necessary to define quotes

! = And <> are often used interchangeably.

Microsoft Access Support <> does not support! =

BETWEEN matching range all values, including the specified start and end values.

Data filtering, the filter must be verified NULL column containing rows actually present in the data returned.

Advanced data filtering

SQL (like most languages) OR operator prior to treatment, priority AND operator.

Higher than parentheses AND or OR operator evaluation order, so that first filtered DBMS OR condition in parentheses.

WHERE clause any time having AND and OR operators, should explicitly grouping operator parentheses. Do not place undue reliance on default evaluation order, even if it does, as you hoped. Use parentheses to no harm, it can eliminate ambiguity.

IN operator generally performs faster than a group OR operator (in this example above a few legal option, you see the performance difference).

The biggest advantage is that can contain other IN SELECT statements can be more dynamically build the WHERE clause

WHERE clause conditions for the subsequent negative keywords.

Filtered with a wildcard

Wildcard search only for text field (string), non-text data type field can not wildcard search.

If you are using Microsoft Access, require the use * instead of%.

A better solution is to remove the spaces with function.

Wildcard% appear to match anything, but there is an exception, this is NULL. Clause WHERE prod_name LIKE '%' does not match the name of the product line to NULL.

This wildcard characters with the prefix ^ (caret) to negative

In any contact names beginning any characters other than the J and M (in contrast to the previous example):

If you are using Microsoft Access, need to use! ^ Rather than to deny a collection, therefore, use the [! JM] rather than [^ JM].

The wild card is placed at the beginning, it is the slowest search.

Using data processing functions

SOUNDEX is a text string into any algorithm which describes a phonetic representation of the alphanumeric mode.
SOUNDEX is a text string into any algorithm which describes a phonetic representation of the alphanumeric mode. SOUNDEX consider a similar character and syllable pronunciation, makes it possible to compare the string to pronounce comparison instead of letters.

Now try using SOUNDEX () function to search, it matches all the pronunciation is similar to Michael Green Contact name:
Enter ▼

SELECT cust_name, cust_contact
FROM Customers
WHERE SOUNDEX(cust_contact) = SOUNDEX('Michael Green');

Output ▼
CUST_NAME cust_contact


Kids Place Michelle Green

Aggregated data

Note: Only a single column for
the AVG () can be used to determine the average value of a particular column, and the column name must be given as a function parameter. In order to obtain an average value of a plurality of columns, you must use a plurality of the AVG () function.
Description: NULL value
AVG (OK function ignores the column is NULL).

COUNT () function

COUNT () function is counted. Available COUNT () to determine the number of rows in the table or rows that meet specific criteria.
COUNT () function used in two ways:
□ using COUNT (*) for counting the number of rows in a table, regardless of the table column contains a null value (NULL)

The AVG () functions ignore row column value is NULL.

Use COUNT (*) for counting the number of rows in a table, regardless of the table column contains a null value (NULL) or non-null value.

Use COUNT (column) having a row of a particular column value counts NULL value is ignored.

Description: NULL value
if the specified column name, COUNT () function ignores the specified column is empty rows, but if the COUNT () function is used an asterisk (*), are not ignored.

MAX () function

MAX () returns the largest value in the specified column. MAX () requires column names are specified as follows:

If you specify the column name, the COUNT () function ignores the specified column is empty rows, but if the COUNT () function is used an asterisk (*), are not ignored.

MAX () functions ignore row column value is NULL.

MIN () functions ignore row column value is NULL.

The SUM () function ignores row column value is NULL.

If you specify a column name can only be used DISTINCT COUNT (). DISTINCT can not be used COUNT (*). Similarly, DISTINCT column name must be used, or not used to calculate expressions.
Tip: DISTINCT for MIN () and MAX ()
Although DISTINCT can technically be used MIN () and MAX (), but this is actually worthless. Minimum and maximum values in a column only consider whether different values, the result is the same.
Note: Other parameters gathered
addition DISTINCT and ALL parameters presented here, some DBMS support other parameters, such as support for TOP and TOP PERCENT subset of query results calculated. To understand the specific DBMS support which parameters, please refer to the appropriate documentation.

If you specify a column name can only be used DISTINCT COUNT (). DISTINCT can not be used COUNT (*). Similarly, DISTINCT column name must be used, or not used to calculate expression

Published 45 original articles · won praise 6 · views 1993

Guess you like

Origin blog.csdn.net/qq_22583191/article/details/104365641