Table of contents
Database (MySQL): It is a specific software (a lot of data structures will be used to realize the software of database). The function of this kind of software is to manage data (organization and description). The purpose of management is to use SQL like this "Add, delete, modify and check" in the language.
1. Database classification
1. Relational database:
The data format is required to be standardized and unified, and the data is organized in the form of a table , similar to excel, where the same type of data can be placed in the same table, each row is a record, and each record contains many columns. The type and meaning requirements are unified. Validation of data is easier and more rigorous. eg: Oracle: the king in the database circle (paid), MySql, SqlServer (used by schools)
2. Non-relational database:
The format of the data is not required to be standardized (the format of the data is more flexible), and the organizational data is organized in the form of "document"/"key-value pair". Each record is a "document". In this document All attribute fields do not have to be exactly the same. The data verification is not so strict, but it is more efficient and more suitable for the current distributed system. eg: MongoDB Redis, Hbase.
2. MySQL related foundation
(1) MySQL manages data through the server.
There are several databases on the MySQL server (considered as a logical collection of data).
A database contains several tables, a
table contains several rows (several records),
and a row contains several columns (several fields)
(2) MySQL is the Data stored on "external storage" (including but not limited to: hard disk, U disk, CD, floppy disk).
Reasons: 1. The amount of data to be managed by MySQL is relatively large. 2.
It is expected to have lower costs.
3. It is expected to be able to store persistently (data will not be lost after power failure) . —The mysql client constructs a request—the request is sent to the server through the network—the server receives the request and parses out the commands—the server executes the commands, completes the addition, deletion, checking, and modification operations—packages the results into a response—through the network Send the response back to the client - the client finally displays it on the interface after parsing.
Three, MySQL database basic operation
1. Display database
show databases
2. Create a database
Character set name: The default utf8 in the database cannot represent some expressions (emoji expressions), changing to utf8mb4 is a complete character set.
Validation rules are more precisely: comparison rules
create database [if not exists] 创建的数据名 [create_specification]
create_specification:
character set 指定数据库采用的字符集名
collate 指定数据库字符集的校验规则名
3. Delete the database
drop database [if exists] 数据库名
4. Working with databases
use 数据库名
4. Common data types
1. Numeric type
unsigned has been deprecated
type of data | size | illustrate | Corresponding java value |
---|---|---|---|
bit[(m)] | M specifies the number of bits, the default is 1 | Binary number, M ranges from 1 to 64, and the storage value ranges from 0 to 2^M-1 | Commonly used Boolean corresponds to BIT, at this time the default is 1 bit, that is, only 0 and 1 can be stored |
tinyint | 1 byte | Byte | |
smallint | 2 bytes | Short | |
int | 4 bytes | Integer | |
bigint | 8 bytes | Long | |
float(m,d) | 4 bytes | Single precision, M specifies the length, and D specifies the number of decimal places. loss of precision will occur | Float |
double(m,d) | 8 bytes | Double | |
decimal(m,d) | m/d max +2 | Double precision, M specifies the length, and D indicates the number of decimal places. exact value | BigDecimal |
numeric(m,d) | m/d max +2 | Double precision, M specifies the length, and D indicates the number of decimal places. exact value | BigDecimal |
2. String type
type of data | size | illustrate | Corresponding java value |
---|---|---|---|
varchar(size) | 0-65535 bytes | variable length string | String |
text | 0-65535 bytes | long text data | String |
mediumtext | 0-16 777 215 bytes | medium length text data | String |
blob | 0-65535 bytes | Long text data in binary form | byte[] |
3. Date type
type of data | size | illustrate | Corresponding java value |
---|---|---|---|
datetime | 8 bytes | The range is from 1000 to 9999, no time zone retrieval and conversion will be performed | java.util.Date、 java.sql.Timestamp |
timestamp | 4 bytes | Ranges from 1970 to 2038, automatically retrieves the current time zone and converts it. | java.util.Date、 java.sql.Timestamp |
Five, MySQL table basic operation
1. Create a table
Select the database before creating the table
create table [表名] (列名 列类型,列名 列类型);
2. View table
(1) View table
show tables
(2) View the specified table structure
desc [表名]
NULL: whether to allow the value of this column to be empty (null), similar to the value in EXCLE that is not filled in
Default: the default value of the column, here is null. Special means can be used to modify
Extra: additional constraints
3. Delete the table
drop table [表名]
6. CRUD of MySQL table
1. Add (create)
(1) Single row data + full column insert
insert into [表名] values (列的值,列的值...);
(2) Multi-row data + specified column insertion
The column name corresponds to that in create
insert into [表名] (列名,列名...) values
(列的值,列的值...),
(列的值,列的值...);
Note: The default character set of MySQL is Latin, which does not support Chinese utf8, and the character set needs to be modified.
View the current character set by fuzzy query
show variables like '%character%';
database 数据库
server 服务器
2. Query (Retrieve)
(1) Full column query (check the entire column)
select * from 表名;
(2) Specified column query
Temporary tables are not stored in memory or on disk.
select [列名],[列名]... from [表名];
(3) The query field is an expression
For example, check the total
select [表达式],[表达式]... from [表名];
(4) Alias name for query information
as can be omitted
select [列名],[表达式] as [别名] from [表名];
(5) Deduplication query: distinct
seclect distinct [列名],[表达式]... from [表名];
(6) Sorting: obey by
Suitable for sorting on the hard disk
You can specify to sort by multiple columns, if column 1 is the same, sort by column 2. NULL is considered the minimum value.
Default order: ascending
desc: descending
select [列名],[表达式]...from [表名] obey by [列名1],[列名2] [desc];
(7) Condition query: where
comparison operator
operator | illustrate |
---|---|
>,>=,<,<= | greater than, greater than or equal to, less than, less than or equal to |
= | equals, NULL is not safe, eg NULL = NULL results in NULL |
<=> | equals, NULL safe, eg NULL <=> NULL evaluates to TRUE(1) |
!=,<> | not equal to |
between a0 and a1 | Range matching, [a0, a1], if a0 <= value <= a1, return TRUE (1) |
in(option,…) | Returns TRUE (1) if any of options |
is null | is NULL |
in not null | not NULL |
like | fuzzy match. % represents any number (including 0) of any character; _ represents any one character |
Logical Operators
operator | illustrate |
---|---|
and | Multiple conditions must all be TRUE (1) for the result to be TRUE (1) |
or | Any condition is TRUE(1), the result is TRUE(1) |
not | Condition is TRUE (1), result is FALSE (0) |
Where conditions can use expressions, and aliases cannot be used to set conditions
and has a higher priority than or
fuzzy queries: like
-- %(通配符)匹配任意多个字符(也可以是数字)
-- _匹配的任意一个字符
select [列名] from [表名] where [列名] like '%茗%' --查到的都是中间有茗字的
select [列名] from [表名] where [列名] like '茗_' --查到的是茗啥
Query for NULL: is [not] null
select [列名] from [表名] where [列名] is not null --列名已知
select [列名] from [表名] where [列名] is null --列名未知
(8) Paging query: limit
Some data is too much to consider the capacity of the database server disk IO and the network IO from the database client to the server, so set up paging queries.
select ... from [表名] where obey by limit n;
-- 后两个表达意思一样 s 从第几条数据开始,n选几条结果
select ... from [表名] where obey by limit s,n;
select ... from [表名] where obey by limit n offset s;
eg:每页有3条记录,显示三页
select ... from [表名] where obey by limit 3 offset 0;-- 第一页,从0条数据开始
select ... from [表名] where obey by limit 3 offset 3;-- 第二页,从第3条数据开始
select ... from [表名] where obey by limit 3 offset 6;-- 第三页,从第6条数据开始
3. Modify (Update)
updata [列名],[列名] set [列名]=[列名值],[列名]=[列名值] where 条件
4. Delete
delect from [表名] where 条件;