Database Review Summary

Database
1.SQL: Structured Query Language
  MYSQL: It is a database based on client and server;

2.MYSQL tool:
mysql command line program;
MySQL Administrator
MySQL QueryBrowser

3. Use MySQL:
show databases to display all available databases;
use USE selects a database;
show tables displays all tables in the database;
show columns from table_name displays the columns in the table;

4.SQL is case-insensitive;

5.distinct keyword only returns different values, which must be placed directly In front of the column name and cannot be used partially;
  select distinct city_name from city;

6.limit limits the number of rows of the returned result;
Select * from city
Limit 5,4;
means to return four rows of data starting from the fifth row;

7.order by:
The columns used in the Order by clause can be non-displayed columns;
when arranging multiple columns, according to the order of column names, sort by the first column name, and then sort by the second column name in the same column name data ;
The default is ascending, descending DESC, ascending ASC;
Using the combination of order by and limit, you can find the highest or lowest value in the column, sort first, and then output several rows of data, so first order by, then limit;
using the combination of order by and where, order by should be located in where After that, it is common sense to sort the selected data.

8. SQL filtering and application software filtering
Although data can be filtered in SQL and on the client, filtering on the client obviously wastes traffic, increases the complexity of the application software, and increases the response time, so data filtering should be done in SQL as much as possible ;

9. When using the where clause, use single quotes for strings, but not for numeric columns;
Where price between 5 and 8;
Where name is null;
Null is a value with a special type, which is required when filtering. It is specially pointed out;

10. Whenever the conditional combination clause of and and or is used, the correct calculation order must be enclosed in parentheses, although the default is and first and then or;

11.In where clause is similar in function to or, but It is more efficient than or;
Where price in (5, 7)==where price =5 or price =7;
Use parentheses to enclose the desired value;
The biggest advantage of IN is that it can contain other select statements.

12. Use wildcards for filtering
LIKE operator and wildcards '%' (can match multiple characters) and '_' (can only match a single character)
Where name like 'jet%'; (search terms are case sensitive)
wildcards It takes a long time to search, so don't overuse it when you deliberately replace it.
Wildcards cannot be used to match null values.

13. Regular expression: (a special set of characters used to match text)
using a regular expression template is also a common way for websites to check the format of user input data;
Regexp matching is case-insensitive, regexp binary is case-sensitive;
Where name regexp 'an '; (returns all values ​​containing an);
Where name regexp 'a|b|c'; (returns all values ​​containing a or b or c);
Where name regexp'[123] Tom' (returns one of 123 with the value of TOM, which matches one of 123)
Where name regexp'[^123] Tom' (value other than 123)
Regexp[1-9][az] matches values ​​from 1 to 9 or a to z;
Regexp'.' matches any character;
Regexp'\\.' matches the '.' character, and special characters should be marked with \\. \\f form feed \\n new line \\\ matches the backslash itself...
matches a custom character class! ! ! The predefined character set
[:digit:] matches any character.
[] indicates a set;
{n} specifies the number of matches;
? Matches the previous character 0 or 1 times;
+ matches the previous character at least once;
* matches the previous character any number of times;
^ means negation in the set, otherwise means the beginning of the string;
$ means the end of the string ;
do not need to pay attention to the position of the wildcard, the regular expression may internally call the template, so the performance will be lower;
Regular expression matching is a very diverse use

14. Instead of retrieving the data and reformatting it in the client application, we need to retrieve the transformed, computed or formatted data directly from the database; in the database server Completing these operations on the server is much faster than in the client, because the DBMS is designed to complete this kind of processing quickly and efficiently;

15. Create new fields
Most DBMS use + or || to implement concatenation, and MySQL uses Concat() The function is implemented by concatenating multiple strings to form a longer string, and each string is separated by commas.
Select concat(name,'(',ID,')')
RTrim() and LTrim() functions remove spaces on the right or left;
use aliases: A AS B
For column merging of numeric classes, add, subtract, multiply and divide directly;

15 .Date and time processing functions
Where Date(date)='2015-10-19';
Where Time(time)='12:03:10';
It is best to use these two functions to judge the date or time;

16.AVG ()Average function
If there is a where clause, only the average value of the values ​​selected by the where clause is found;
NULL values ​​are ignored;

17.count() The counting function
Count(*) calculates the number of all rows in the table;
Count (column name) Calculate the number of rows in this column, ignoring NULL values;

18. Group data, group by (grouping) and having (filter grouping)
First group by grouping and then use aggregate function on the grouped data, so the aggregate function cannot appear in the group by clause;
Group by appears after where, where filters rows, first selects the data and then groups, and then uses having to filter the group;
The NULL value appears as a group;
the column appearing in the Group by clause must be a retrieval column or a valid expression;
after grouping, remember to sort, group by should be used together with order by, first grouping and then sorting, order by should be in group after by.

19. Two uses of subqueries:
1) Use subqueries for filtering;
2) Use the results of subqueries as columns;

20. By specifying primary and foreign keys in the table definition, referential integrity can be maintained, that is, if the product is in production In the table, the primary key of the supplier table is set as a foreign key, then the value of the supplier column in the product table must appear in the supplier table.

21. When joining two tables, each row of the first table is actually paired with each row of the second table, that is, the Cartesian product of the two tables is obtained, and then the where clause is used to The data that meets the conditions is filtered out.

22. The table can be aliased with AS, but the table alias is only used in query execution. Unlike the column alias, the table alias is not returned to the client.

23. Three connection methods:
1) Equivalent connection/inner connection: Common connection types, first get the Cartesian product, and then select the row that satisfies the condition;
(where Aa=Ba) (from A inner join B on Aa =Ba)
2) Natural join: Two tables are required to have the same columns, and duplicate columns will be removed from the returned results; (natural join)
SELECT *
FROM authors NATURAL JOIN publishers
3) Outer join: All rows are returned in the result, although some rows do not meet the conditions;
(from A outer join B on Aa=Ba)
left outer join, let the left table retain all rows, right outer join Let the table on the right keep all the rows
(from A left outer join B on Aa=Ba ||from A right outer join B on Aa=Ba)

24.UNION union Combine query, similar
to The results are combined into a set, so each select statement must be the same retrieval column;
Union deletes duplicate rows by default, and union all can be used to display all rows if necessary;
Union is between two select statements, and the order by clause appears only once. At the end, sort all the results;

25.MySQL's internal engine, used to manage and process data:
InnoDB: is a reliable transaction processing engine, but does not support full-text search;
MEMORY: functionally equivalent to MyISAM, but Data is stored in memory, which is fast, but is used with temporary tables;
MyISAM: does not support transaction processing, but supports full text search;

26. For table modification, it is generally used to define foreign keys
Alter table orderitems
Add constraint fk_orderitems_orders
Foreign key (order_num) references orders(order_num);

Drop column delete column

27. View A
view is a virtual table that encapsulates SQL statements.
Create view name as... (query statement)
and then use this view to replace the complex query statement;
although the operation of the view is basically the same as the operation of the table, it can be used for nested joins and the like,
but the view is not real Existing, the call to the view is actually a call to the SQL defined under the view, so when multiple views are nested, etc., the performance will be degraded;
Show create view name; used to display the SQL statement that defines the view;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327007640&siteId=291194637