table of Contents
One, the basic operation of the database
1.1 View the database structure-1
■ View database information
SHOW DATABASES
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| myadm |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
1.2 View database structure-2
■ View table information in the database
USE 数据库名
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
......
31 rows in set (0.00 sec)
1.3 View the database structure -3
■ Display the structure (fields) of the data table
DESCRIBE [数据库名.]表名
###也可以使用“desc user;”
mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
......
Two, create and manage databases and tables
2.1 Create database and table
■ DDL statements can be used to create database objects, such as libraries, tables, indexes, etc.
■ Use DDL statements to create new libraries and tables
- Create database
CREATE DATABASE 数据库名
- Create data table
CREATE TABLE 表名(字段定义......)
mysql> create database auth;
Query OK, 1 row affected (0.00 sec)
mysql> use auth;
Database changed
mysql>
mysql> create table users (user_name CHAR(16) NOT NULL,user_passwd CHAR(48) DEFAULT'',PRIMARY KEY (user_name));
Query OK, 0 rows affected (0.01 sec)
2.2 Data in the management table-1
■ DML statement is used to manage the data in the table
■ Include operations
- INSERT: Insert new data
- UPDATE: update the original data
- DELETE: Delete unnecessary data
2.3 Data in Management Table-2
■ Want to insert a new data record in the data table
UPDATE 表名 SET 字段名1=值1[,字段名2=值2]WHERE 条件表达式
mysql> update auth.users set user_passwd=password('123456') where user_name='lisi';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 1
mysql> flush privileges; ### 刷新下
mysql> update mysql.user set authentication_string=PASSWORD('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges; ### 刷新下
2.3 Data in Management Table-3
■ Delete the specified data record in the data table
DELETE FROM 表名 WHERE 条件表达式
mysql> delete from auth.users where user_name='lisi';
Query OK, 0 rows affected (0.01 sec)
■ A statement without where conditions means to delete all records in the table
mysql> delete from auth.users;
Query OK, 0 rows affected (0.00 sec)
###不带where子句时需谨慎操作
2.4 Data in Management Table-4
■ DQL is a data query statement, only SELECT
■ It is used to find data records that meet the conditions from the data table
■ You can query without specifying conditions
SELECT 字段名1,字段名2.....FROM 表名
mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| wangwu | *E710DC2512FCF6F18FE0D652B53290DCB11F3334 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
2.5 Data in the management table-5
SELECT 字段名1,字段名2......FROM 表名 WHERE 条件表达式
mysql> select user_name,user_passwd from kk.KK where user_name='lisi';
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
2.5 Advanced Database Operation-1
■ Empty table
- DELETE FROM tablename
- TRUNCATE TABLE tablename
mysql> truncate table tmp;
2.6 Advanced Database Operation-2
■ Temporary table
- Temporarily created table, used to save some temporary data, will not exist for a long time
在这里插入代码片
2.7 Advanced Database Operation-3
■ Clone table
- LIKE method
mysql> create table test like mytmp;
mysql> insert into test select * from mytmp;
- SHOW CREATE TABLE method
mysql> show create table mytmp\G
mysql> create table test(......);
mysql> insert into test select * from mytmp;
Three, database user authorization
3.1 Database user authorization-1
■ DCL statement to set user permissions (when the user does not exist, create a new user)
GRANT 权限列表 ON 数据库名 TO 用户名@来源地址 [IDENTIFIED BY '密码']
mysql> GRANT select ON auth.* TO 'xiaoqi'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
3.2 Database user authorization-2
■ View user permissions
SHOW GRANTS FOR 用户名@来源地址
mysql> SHOW GRANTS FOR 'lisi'@'20.0.0.6';
■ Revoke user permissions
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址
mysql> REVOKE all ON auth.* FROM 'xiaoqi'@'localhost';