###This article is to supplement the content that is not in the book after learning "MYSQL Must Know and Know". It is strongly recommended that this book is an introduction to MYSQL database! ! ! ! !
to sum up:
use RUNOOB; The command is used to select the database RUNOOB.
The set names utf8; command is used to set the character set used. set names utf8 specifies the encoding rule for transferring characters between the client and the server as UTF8.
set char set'gbk'; Due to personal setting problems, it is impossible to display and insert Chinese. It is estimated that this sentence should be added.
SELECT * FROM Websites; Read the information in the data table Websites.
Note: SQL is not case sensitive: SELECT and select are the same.
Some of the most important SQL commands
SELECT-extract data from the database
UPDATE-update data in the database
DELETE-delete data from the database
INSERT INTO-insert new data into the database
CREATE DATABASE-create a new database
ALTER DATABASE-modify the database
CREATE TABLE- Create new table
ALTER TABLE-change (change) database table
DROP TABLE-delete table
CREATE INDEX-create index (search key)
DROP INDEX-delete index
Search with regular expressions
Regular expressions are special strings (character sets) used to match text
REGEXP
The following statement retrieves all rows where the column prod_name contains the text 12:
select prod_name
from Products
where prod_name REGEXP ‘12’
ORDER BY prod_name;
Note: Different from LIKE:REGEXP '12' refers to a string containing 12, while LIKE '12' refers to prod_name=12;
What follows REGEXP is treated as a regular expression (a regular expression that matches the text body 12).
Regular expression matching in MySQL is not case sensitive (that is, both uppercase and lowercase match). To distinguish between upper and lower case, you can use the BINARY keyword, such as WHERE prod_name REGEXP BINARY'JetPack .000'.
| Usage
To search for one of two strings (either this string or the other string), use |
The regular expression [123] Ton . [123] Define a set of characters, which means to match 1 or 2 or 3,
[] is another form of OR statement. In fact, the regular expression [123]Ton is short for [1|2|3]Ton
But pay attention to the difference between the following procedures
This is not the expected output. Two requested rows were retrieved, but three other rows were retrieved. The reason for this is that MySQL assumes that you mean '1' or '2' or '3 ton'. Unless the character | is enclosed in a set, it will be applied to the entire string.
Match range
[0-9] Match numeric characters within 0-9
[az] matches any alphabetic character
In order to match special characters, \\ must be used as the leader . \\- means search-, \\. means search.
To make it easier to work, you can use a predefined character set called a character class . Table 9-2 lists the character classes and their meanings.
Table 9-2 Character class
[Class] Description
[:alnum:] any letters and numbers (same as [a-zA-Z0-9])
[:alpha:] Any character (same as [a-zA-Z])
[:blank:] Space and Tab (same as [\\t])
[:cntrl:] ASCII control characters (ASCII 0 to 31 and 127)
[:digit:] Any number (same as [0-9])
[:graph:] Same as [:print:], but does not include spaces
[:lower:] Any lowercase letter (same as [az])
[:print:] Any printable character
[:punct:] Any character that is neither in [:alnum:] nor in [:cntrl:]
[:space:] Any blank characters including spaces (same as [\\f\\n\\r\\t\\v])
[:upper:] Any uppercase letter (same as [AZ])
[:xdigit:] Any hexadecimal number (same as [a-fA-F0-9])
\\( Matches (, [0-9] matches any number (1 and 5 in this example), sticks? matches sticks and sticks (? after s makes s optional, because ? Matches 0 of any character before it Or 1 occurrence), \\) matches). No? It would be very difficult to match sticks and sticks.
As mentioned earlier, [:digit:] matches any digit, so it is a set of digits. {4} exactly requires the character (any number) in front of it to appear 4 times, so [[:digit:]]{4} matches any 4 digits connected together.
The above example can also be written as follows:
All the examples so far match text anywhere in a string. In order to match the text at a specific location, you need to use the locators listed in Table 9-4 .
What if you want to find all products that start with a number (including numbers that start with a decimal point) ? A simple search for [0-9\\.] (or [[:digit:]\\.]) will not work, because it will find a match anywhere in the text. The solution is to use the ^ locator, as shown below:
^ The beginning of the match string. Therefore, ^[0-9\\.] only matches. Or any number when it is the first character in the string. Without ^, 4 more individual rows (those with numbers in the middle) must be retrieved.
The difference between LIKE and REGEXP is that LIKE matches the entire string and REGEXP matches substrings. Using locators, by starting each expression with ^ and ending each expression with $, you can make REGEXP the same as LIKE.
Common date and time processing functions
Function Description
AddDate() add a date (day, week, etc.)
AddTime() add a time (hour, minute, etc.)
CurDate() returns the current date
CurTime() returns the current time
Date() returns the date part of the date and time
DateDiff() calculates the difference between two dates
Date_Add() highly flexible date calculation function
Date_Format() returns a formatted date or time string
Day() returns the day part of a date
DayOfWeek() For a date, returns the corresponding day of the week
Hour() returns the hour part of a time
Minute() returns the minute part of a time
Month() returns the month part of a date
Now() returns the current date and time
Second() returns the second part of a time
Time() returns the time part of a date and time
Year() returns the year part of a date
The stored order_date value is 2005-09-01 11:30:05, then WHERE order_date = '2005-09-01' fails.
For this, the Date() function must be used . Date(order_date) instructs MySQL to extract only the date part of the column, which is more reliable
The SELECT statement is