1. The database
- concept
DataBase
(hereinafter referred to asDB
)- A warehouse for storing and managing data
- The characteristics of
- A file system that persistently stores data
- Easy to store and manage data
- The database is operated in a uniform way:
SQL
- Common database software
- MySQL
- Oracle
- SQL Server
- DB2
2. MySQL
- The installation
- slightly
- uninstall
- Find it in the MySQL installation directory
my.ini
file - Open this file and copy the
Datadir = "C: / ProgramData/MySQL/MySQL Server 5.5 / Data/"
(Prepare for step 1) - Uninstall MySQL in your application
- Delete step 2
C:/ProgramData
In the directoryMySQL
The folder can be completely uninstalled
- Find it in the MySQL installation directory
- configuration
- MySQL service start
-
CMD enter services. MSC, open the services window, then start/stop/change the startup mode (automatic/manual), as shown below
-
Open CMD as an administrator:
- The input
net start mysql
: Starts the mysql service - The input
net stop mysql
: Stops the mysql service
- The input
-
- MySQL login
- CMD:
Mysql -uroot -p Password
Mysql -h* IP * -uroot -p password
Mysql --host=* IP * --user=root --password= connect target password
- CMD:
- MySQL exit
- CMD:
exit
quit
- CMD:
- MySQL directory structure
- Configuration file:
my.ini
- MySQL installation directory:
basedir="..."
(in the my.ini file) - MySQL > alter database
Datadir = "C: / ProgramData/MySQL/MySQL Server 5.5 / Data/"
- Database: a folder under the data folder in the MySQL installation directory
- Table: files under the database folder
- Data: Data in a table
- Configuration file:
- MySQL service start
3. SQL
- concept
- Structured Query Language: Structured Query Language
- Defines rules that operate on all relational databases, but each specific database operates differently
- SQL General syntax
- Each SQL statement can be written on one or more lines, ending with a semicolon
- You can use Spaces or indents to make your code more readable
- The SQl statements of the MySQL database are case insensitive, but you are advised to use uppercase keywords
- annotation
- One-line comment:
# Comment content
(MySQL specific) or-- Comment content
- Multi-line comments:
/* Comment the content */
- One-line comment:
- Classification of SQL
DDL
(Data Definition Language) : Data Definition Language- Define database objects: databases, tables, columns, etc
- Key words:
CREATE
.DROP
.ALTER
Etc.
DML
(Data Manipulation Language) : Data Manipulation Language- Add, delete, and modify the data of the tables in the database
- Key words:
INSERT
.DELETE
.UPDATE
Etc.
DQL
(Data Query Language) : Data Query Language- Query data (records) from tables in the database
- Key words:
SELECT
.WHERE
Etc.
DCL
(Data Control Language) : Data Control Language- Define database access permissions, security levels, and create users
- Key words:
GRANT
.REVOKE
Etc.
1. DDL: Operates databases and tables
- CRUD
- C: Creat
- R: Retrieve (query)
- U: Update (modify)
- D: Delete
- Operating database: the CRUD
- C
- Create database:
CREATE database Specifies the database name.
- Check whether the database exists. If the database does not exist, create a database.
CREATE database if not exists Database name;
- Create database and specify character set:
CREATE database Database name character set;
- Create database:
- R
- Query all database names:
SHOW databases;
- SQL > select * from character set; SQL > select * from character set;
SHOW CREATE database Database name;
- Query all database names:
- U
- Alter database character set
ALTER database ALTER database name character set;
- Alter database character set
- D
- Delete database:
DROP database Specifies the database name.
- Check whether the database exists. If the database exists, delete it.
CREATE database if exists Database name;
- Delete database:
- Using a database
- Using a database:
USE Database name;
- Query current database name:
SELECT database();
- Using a database:
- C
- The operating table: the CRUD
- C
- Grammar:
CREATE tableTable name (column name1The data type1And the column name2The data type2. Column name n Data type n);Copy the code
Note: The last column does not need a comma
- The data type
int
: Integer typeDouble (number of digits, reserved after the decimal point)
: decimal type,date
: Indicates the date, which contains only yyyy- MM-DDdatetime
: Indicates the date, including year month day hour minute second YYYY-MM-DD HH: MM :sstimestamp
: Indicates the timestamp type, including year, month, day, hour, minute, second, YYYY-MM-DD HH: MM :ss- If no value is assigned to this field or the value is null, the current system time is automatically assigned by default
Varchar (maximum number of characters)
: String type
- The replication table:
CREATE table name like the name of the table to be copied;
- Grammar:
- R
- Query the names of all tables in a database:
SHOW tables;
- Query the structure of a table:
Desc table name;
- Query the names of all tables in a database:
- U
- Alter table name:
ALTER table name rename to new table name;
- Alter table character set;
ALTER table name character set;
- Add a column:
ALTER table name add column name data type;
- Modify column names and data types
- Modify only the data type:
ALTER table table name modify column name New data type;
- Modify column names and data types:
ALTER table table_name change column name new column name new data type;
- Modify only the data type:
- Delete the columns:
ALTER table table_name DROP table_name;
- Alter table name:
- D
- Delete table:
DROP table table name;
- Check whether the table exists, then delete:
DROP table if exists Specifies the name of the table.
- Delete table:
- C
2. DML: Add, delete and modify the data in the table
- Add data:
Insert into table name (1, 2,... N) values(1, 2... And the value of n);
- Column names and values need to correspond one to one
- If no column name is defined after the table name, values are added to all columns by default:
Insert into values(1, 2,... And the value of n);
- Except for numeric columns, all types need to be enclosed in quotes (single or double quotes)
- Delete the data:
Delete from table name where condition;
- If no WHERE condition is added, all data in the table is deleted
- Delete all data from the table
Delete from table name;
This operation is not recommended. If n records exist, n records are deleted, which is inefficientTruncate TABLE name;
You are advised to delete the table and then create an empty table
- Modify the data:
Update table_name set table_name 1= 1, table_name 2= 2,... , column name n= value n where condition;
- Without the WHERE condition, the data in each row of the table is modified
3. DQL: queries records in the table
- grammar
selectField listfromThe table listwhereCondition listgroup byThe grouping fieldhavingList of conditions after groupingorder bySorting based onlimitPaging limitedCopy the code
- Based on the query
- Multiple field queries
- Query multiple fields:
Select 1, 2... From the name of the table;
- Query all fields:
Select * from table_name;
- Query multiple fields:
- The query result is deduplicated
- Add before the field that needs to be reweighted
distinct
- Add before the field that needs to be reweighted
- Computed columns
- You can use four operations to compute the values of some columns of numeric data type
- You can use
Ifnull (expression 1, expression 2)
Replace null data in expression 1 with expression 2
- names
as
, can also be omitted
- Multiple field queries
- Conditions of the query
- The WHERE clause is followed by a condition
- The operator
>, >=, <, <=, =, <> (! =)
between... and...
(including left and right boundaries)In (set)
like
: fuzzy query- A placeholder
_
: A single arbitrary character%
: Multiple arbitrary characters
- A placeholder
is null
And (&)
Or (| |)
Not (!)
- Sorting query:
Order by 1, order by 2, order by 2...
- The sorting way
asc
: Ascending (default)desc
: descending
- Only if sorting by 1 is the same, will we judge sorting by 2
- The sorting way
- Aggregation function: Vertical calculation of a column of data as a whole
Count (count field)
: Count the number- Fields that are not empty are generally selected for counting, such as primary keys
- Count (*) : Counts as long as at least one field in the row is not empty
max
: Calculate the maximum valuemin
: Calculates the minimum valuesum
Calculation and:avg
: Calculate the average value
- Aggregate functions are evaluated without consideration of NULL
- The solution
- Select fields whose values are not null for calculation
- use
Ifnull (expression 1, expression 2)
Replace null data in expression 1 with expression 2
- The solution
- Grouping query:
Group by;
- It is meaningless to query a single field after grouping. You are advised to query a group field or aggregate function after grouping
- Where having
where
: Conditions are specified before grouping. If conditions are not met, no group is performedhaving
If the conditions are not met, the group will not be queried- Aggregate functions cannot be used after WHERE and can be used after HAVING because WHERE is not yet grouped
- Sample code:
SELECT sex , AVG(math),COUNT(id) count FROM student WHERE math > 70 GROUP BY sex HAVING count > 2;
- Paging query:
Limit Indicates the number of queries per page
- Start index = (current page number -1) * number of pages to display per page
- Limit is a clause unique to MySQL
- Multi-table query
- Inner join query
- Implicit inline join: Use the WHERE condition to eliminate unwanted data
Select field list FROM table name list WHERE Condition list
- Explicit inline connection: Use the ON condition to eliminate unwanted data
- ‘select * from table 1 [inner] join table 2 on condition list
- Write the list of names first, then write the list of conditions, and finally write the list of fields
- Implicit inline join: Use the WHERE condition to eliminate unwanted data
- External join query
- Left outer connection:
Select * from 表名1 left [outer] join 表名2 on 表名
- The query data is the intersection of all the data in the left table and the part that meets the criteria
- Right outer connection:
Select * from 表名1 right [outer] join 表名2 on 表名
- The data to be queried is all the data in the right table and the intersection part that meets the conditions
- Left outer connection:
- The subquery
- Concept: nested queries within a query are called subqueries
- classification
- The result of a subquery is a single row, single column
- Subqueries are conditional using the operators (>,>=,<,<=,=,! =) judgment
- The subquery result is multiple rows and one column
- Subqueries are judged by the in operator as conditions
- Subquery results are multiple rows and columns
- The subquery participates in the query as a virtual table
- The result of a subquery is a single row, single column
- Inner join query
4. DCL: Manage users and rights
- Manage users
- Query users:
USE mysql; SELECT * FROM USER; Copy the code
- Create a user:
CREATE USER 'username '@' username' IDENTIFIED BY 'password ';
- Delete a user:
DROP USER 'username '@' username ';
- Changing a user password:
UPDATE USER SET PASSWORD = PASSWORD(' new PASSWORD ') WHERE USER = 'username ';
SET PASSWORD FOR 'username '@' username' = PASSWORD(' new PASSWORD ');
- Query users:
- Rights management
- Query permission:
SHOW GRANTS on 'userid '@' host name ';
- Grant permissions:
Grant permission list on database name. Alter table name to 'username '@' hostname ';
- Grant all privileges on any table in any database:
GRANT ALL ON *.* TO 'username '@' hostname ';
- Grant all privileges on any table in any database:
- Revoking permission:
Revoke Permission list on database name. The name of the table from
The user name.
The host name;
- Query permission: