Basic operation and management of Mysql database

1. Basic operation commands commonly used in databases

1.1 View database list information

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| students           |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

1.2 View which tables are included in the database

  • use to specify the database
  • Specify the table name using show
mysql> use students; ## 进入数据库students;
mysql> show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| test               |
| test01             |
| test02             |
| test03             |
| test04             |
| xuesheng           |
+--------------------+
6 rows in set (0.00 sec)

1.3 View the structure (fields) of the data table

  • Enter the specified database
  • describe table name (can also be abbreviated as desc table name)
mysql> describe test;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(10)      | NO   | PRI | NULL    | auto_increment |
| name    | char(20)     | NO   | MUL | NULL    |                |
| score   | decimal(5,2) | YES  |     | NULL    |                |
| address | varchar(20)  | YES  |     | 未知    |                |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

1.4 View the current status;

--------------
mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper

Connection id:		3
Current database:	students     ## 当前所在数据库
Current user:		root@localhost      ## 当前所使用用户及登录终端
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.17 Source distribution
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/usr/local/mysql/mysql.sock
Uptime:			8 min 33 sec

Threads: 1  Questions: 22  Slow queries: 0  Opens: 114  Flush tables: 1  Open tables: 107  Queries per second avg: 0.042
--------------

Two, SQL statement

  • Abbreviation of Structured Query Language, namely structured query module
  • Relational library language
  • Used to maintain and manage the database, including data query, data update, access control, object management and other functions.
  • SQL Classification
    DDL: Data Definition Language
    DML: Data Manipulation Language
    DQL: Data Query Language
    DCL: Data Control Language

2.1 DDL statement

  • Used to create database objects, such as libraries, tables, indexes, etc.

2.1.1 Create a database

mysql> create database student1;

2.1.2 Create data table

  • Basic syntax
    create table table name (field 01 name field 01 type field 01 constraint, field 02 name field 02 type field 02 constraint) storage engine, character set ## Define multiple types separated by commas
create table xuesheng(id int(10) auto_increment,       ## id设置为int型,自增,不能为空
name varchar(15) NOT NULL, ##   ## name设置为字符型,不能为空
score  decimal(5,2)  NOT NULL,        ##  score 设置为浮点型 最大位数为5,小数点后面保留两位,不能为空
address varchar(50) NOT NULL default ‘未知’, ## 设置为字符型,不能为空 默认为未知
PRIMARY KEY(id));   ## 设置主键为id

2.1.3 Delete database and table

  • Delete the specified data table
    drop table database name. Table name;
mysql> drop table students.test04;
Query OK, 0 rows affected (0.00 sec)

  • Delete the specified database
  • Basic format
    drop database database name;
mysql> drop database student1;
Query OK, 0 rows affected (0.01 sec)

2.2 DML statements

  • Used to manage the data in the table
  • Included operations
    insert: insert new data
    update: update the original data
    delete: delete unnecessary data

2.2.1 Insert new data

  • Basic format:
    Method 1:
    INSERT INTO table name (field 1, field 2,...) VALUES (field 1 value, field 2 value...) ## Ordered, the table name and VALUES must be one-to-one correspondence, quantity Also correspond to
    Method 2:
    insert into info values ​​(the value of field 1, the value of field 2, the value of field 3) ## Do not follow the field name, write all the values, you must write each one, no field name, default All fields
    Method three:
    insert into info (name, score, address) values ​​('赵六', 99, suzhou), ('tianqi', 60, default) ## Multi-line insert
mysql> select * from xuesheng;
+----+----------+-------+---------+
| id | name     | score | address |
+----+----------+-------+---------+
|  1 | zhangsan |    90 | nanjing |
|  2 | lisi     |    88 | chengdu |
+----+----------+-------+---------+
2 rows in set (0.00 sec)
- 方式一插入数据:
mysql> insert into xuesheng(name,score,address) values('wangwu',86,'shanghai');
Query OK, 1 row affected (0.01 sec)
- 方式二插入数据:
mysql> insert into xuesheng values(4,'zhaoliu',73,'beijing');
Query OK, 1 row affected (0.00 sec)
方式三插入数据:
mysql> insert into xuesheng(name,score,address) values('tianqi',99,'hangzhou'),('laoba',92,'guangzhou');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from xuesheng;
+----+----------+-------+-----------+
| id | name     | score | address   |
+----+----------+-------+-----------+
|  1 | zhangsan |    90 | nanjing   |
|  2 | lisi     |    88 | chengdu   |
|  3 | wangwu   |    86 | shanghai  |
|  4 | zhaoliu  |    73 | beijing   |
|  5 | tianqi   |    99 | hangzhou  |
|  6 | laoba    |    92 | guangzhou |
+----+----------+-------+-----------+
6 rows in set (0.00 sec)

2.2.2 Modify the data records in the update table

  • Basic format
    UPDATE table name SET field name 1 = value 1 WHERE conditional expression
mysql> update xuesheng set name='xiaoba',score=87.5 where id=6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from xuesheng;
+----+----------+-------+-----------+
| id | name     | score | address   |
+----+----------+-------+-----------+
|  1 | zhangsan |    90 | nanjing   |
|  2 | lisi     |    88 | chengdu   |
|  3 | wangwu   |    86 | shanghai  |
|  4 | zhaoliu  |    73 | beijing   |
|  5 | tianqi   |    99 | hangzhou  |
|  6 | xiaoba   |    88 | guangzhou |
+----+----------+-------+-----------+
6 rows in set (0.00 sec)

2.2.3 Delete data records in the table

  • Basic format
    delete from table name where conditional expression## Without where, delete all records in the table
mysql> delete from xuesheng where id=6;
Query OK, 1 row affected (0.00 sec)

mysql> select * from xuesheng;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan |    90 | nanjing  |
|  2 | lisi     |    88 | chengdu  |
|  3 | wangwu   |    86 | shanghai |
|  4 | zhaoliu  |    73 | beijing  |
|  5 | tianqi   |    99 | hangzhou |
+----+----------+-------+----------+
5 rows in set (0.00 sec)

2.3 DQL statement

  • Is a data query statement, the command is only SELECT
  • Used to find data records that meet the specified conditions from the data table
  • No conditions can be specified when querying
  • Basic format
    select field name 1, field name 2 …… from table name

mysql> select name,score from xuesheng;
+----------+-------+
| name     | score |
+----------+-------+
| zhangsan |    90 |
| lisi     |    88 |
| wangwu   |    86 |
| zhaoliu  |    73 |
| tianqi   |    99 |
+----------+-------+
5 rows in set (0.00 sec)

2.4 DCL statement

  • Is to set or view user permissions, or create users

2.4.1 Set user permissions

  • Basic format
    GRANT permission list ON database name. Table name TO user name@source address [IDENTIFIED by'password']
grant  all on *.* to 'jerry'@'localhost'  identified  by 'abc123' ## 给予jerry用户本地登录对于所有数据库和表 
#所有的权限
Query OK, 0 rows affected, 1 warning (0.00 sec)

2.4.2 View user permissions

  • The basic format
    show grants for username@source address;
mysql> show grants for 'jerry'@'localhost';
+----------------------------------------------------+
| Grants for jerry@localhost                         |
+----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'jerry'@'localhost' |
+----------------------------------------------------+
1 row in set (0.00 sec)

2.4.3 Revoke user permissions

  • The basic format
    revoke permission list on database name. table name from username@source address;
mysql> revoke  all on  *.* from  'jerry'@'localhost' ;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'jerry'@'localhost';
+-------------------------------------------+
| Grants for jerry@localhost                |
+-------------------------------------------+
| GRANT USAGE ON *.* TO 'jerry'@'localhost' |
+-------------------------------------------+
1 row in set (0.00 sec)

Three, data table advanced operations

2.1 Temporary table

  • Temporarily created table, used to save some temporary data, will not exist for a long time (in memory, it will not exist after shutdown and restart)
  • Basic format
    create temporary table table name (field 01 name field 01 type field 01 constraint, field 02 name field 02 type field 02 constraint) storage engine, character set
create  temporary TABLE mytmp1 (
       id int(10) NOT NULL AUTO_INCREMENT,
       NAME varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
       level  int(10) NOT NULL,
       PRIMARY KEY (id)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.2 Clone table

  • Method one:
    create table clone the generated table table name as select * from the table name of the cloned table; (in one go) ## Can not clone the entire table after the select attribute
  • Method 2: Two
    steps:
    create table test like mytmp; (copy a table structure, generate a new table mytmp is copied)
    insert into test select * from mytmp; (import the data in the original table into the new table)
- 方式一:
mysql> create table tmp as select * from xuesheng;
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from tmp;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan |    90 | nanjing  |
|  2 | lisi     |    88 | chengdu  |
|  3 | wangwu   |    86 | shanghai |
|  4 | zhaoliu  |    73 | beijing  |
|  5 | tianqi   |    99 | hangzhou |
+----+----------+-------+----------+
5 rows in set (0.00 sec)

- 方式二:
 mysql> create table tem1 like xuesheng;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tem1 select * from  xuesheng;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from tem1;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan |    90 | nanjing  |
|  2 | lisi     |    88 | chengdu  |
|  3 | wangwu   |    86 | shanghai |
|  4 | zhaoliu  |    73 | beijing  |
|  5 | tianqi   |    99 | hangzhou |
+----+----------+-------+----------+
5 rows in set (0.00 sec)

2.3 Clear the table

  • Basic format
    truncate table table name##
    delete from
    the difference between table name drop and truncate drop directly delete the table, the table is gone, truncate is the empty table, the table structure is still there, the data is not there
mysql> truncate table tem1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from tem1;
Empty set (0.00 sec)

  • Use drop
mysql> drop table students.tmp;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from tmp;
ERROR 1146 (42S02): Table 'students.tmp' doesn't exist

Guess you like

Origin blog.csdn.net/weixin_47219725/article/details/108092483