MySQL数据库基本操作命令

数据库基本操作命令

mysql查看数据库结构

查看数据库结构

创建及删除库和表

管理表的记录

登录数据库

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

查看数据库信息

查看数据库中的数据表信息

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

使用库

use 数据库名

mysql> use mysql;
Database changed

show tables

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

显示数据表的结构(字段)

describe [数据库名.]表名

mysql> desc servers;
+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| Server_name | char(64) | NO   | PRI |         |       |
| Host        | char(64) | NO   |     |         |       |
| Db          | char(64) | NO   |     |         |       |
| Username    | char(64) | NO   |     |         |       |
| Password    | char(64) | NO   |     |         |       |
| Port        | int(4)   | NO   |     | 0       |       |
| Socket      | char(64) | NO   |     |         |       |
| Wrapper     | char(64) | NO   |     |         |       |
| Owner       | char(64) | NO   |     |         |       |
+-------------+----------+------+-----+---------+-------+
9 rows in set (0.00 sec)

SQL操作管理命令

SQL语言概述

SQL语言
是Structured Query Language的缩写,及结构化查询语言
是关系型数据库的标准语言
用于维护管理数据库,如数据查询,数据更新,访问控制,对象管理等功能

SQL分类

DDL:数据定义语言
DML:数据操纵语言
DQL:数据查询语言
DCL:数据控制语言

DDL操作命令

创建数据库和表
DDL语句用于创建数据库对象,如库,表,索引等

使用DDL语句新建库、表

DDL语句创建库、表的命令
创建数据库:create database 数据库名

mysql> create database school;
Query OK, 1 row affected (0.01 sec)

创建数据表:create table 表名(字段定义…)

mysql> use school;
Database changed

mysql> create table student (id int(3) not null primary key auto_increment,name varchar(10) not null,score decimal(5,2),address varchar(50) default '未知');
Query OK, 0 rows affected (0.02 sec)

DDL语句删除库,表的命令

删除指定的数据表:drop table [数据库名.]表名

mysql> use school;
Database changed

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)

mysql> drop table student;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.00 sec)

删除指定的数据库:drop database 数据库名

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

mysql> drop database school;
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

DML操作命令

DML语句的作用
DML语句用于对表中的数据进行管理

包括以下操作

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

insert:插入新数据
update:更新原有数据
delete:删除不需要的数据

向数据表中插入新的数据记录命令

mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)

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

mysql> desc student;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(3)       | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10)  | NO   |     | NULL    |                |
| score   | decimal(5,2) | YES  |     | NULL    |                |
| address | varchar(50)  | YES  |     | 未知    |                |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


mysql> insert into student(id,name,score,address) values(1,'zhangsan',99,'beijing');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+----------+-------+---------+
| id | name     | score | address |
+----+----------+-------+---------+
|  1 | zhangsan | 99.00 | beijing |
+----+----------+-------+---------+
1 row in set (0.00 sec)

mysql> insert into student(name,score,address) values('lisi',65,'shanghai');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan | 99.00 | beijing  |
|  2 | lisi     | 65.00 | shanghai |
+----+----------+-------+----------+
2 rows in set (0.00 sec)

mysql> insert into student values(3,'wangwu',98,default);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan | 99.00 | beijing  |
|  2 | lisi     | 65.00 | shanghai |
|  3 | wangwu   | 98.00 | 未知     |
+----+----------+-------+----------+
3 rows in set (0.00 sec)

mysql> insert into student(name,score,address) values('zhaoliu',59,'suzhou'),('lilei',68,'nanjing'),('hanmeimei',99,'shanghai');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;                                                   
+----+-----------+-------+----------+
| id | name      | score | address  |
+----+-----------+-------+----------+
|  1 | zhangsan  | 99.00 | beijing  |
|  2 | lisi      | 65.00 | shanghai |
|  3 | wangwu    | 98.00 | 未知     |
|  4 | zhaoliu   | 59.00 | suzhou   |
|  5 | lilei     | 68.00 | nanjing  |
|  6 | hanmeimei | 99.00 | shanghai |
+----+-----------+-------+----------+
6 rows in set (0.01 sec)

修改,更新数据表中的数据记录的命令

update 表名 set 字段名 1=值1[,字段名2=值2] where条件表达式

mysql> update student set score=66 where name='zhangsan';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+----+-----------+-------+----------+
| id | name      | score | address  |
+----+-----------+-------+----------+
|  1 | zhangsan  | 66.00 | beijing  |
|  2 | lisi      | 65.00 | shanghai |
|  3 | wangwu    | 98.00 | 未知     |
|  4 | zhaoliu   | 59.00 | suzhou   |
|  5 | lilei     | 68.00 | nanjing  |
|  6 | hanmeimei | 99.00 | shanghai |
+----+-----------+-------+----------+
6 rows in set (0.00 sec)

在数据表中删除指定的数据记录命令

delete from 表名 where 条件表达式
不带where条件的语句表示删除表中所有记录(高危操作)

mysql> delete from student where socre > 80;
ERROR 1054 (42S22): Unknown column 'socre' in 'where clause'
mysql> mysql> delete from student where score > 80;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from student;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan | 66.00 | beijing  |
|  2 | lisi     | 65.00 | shanghai |
|  4 | zhaoliu  | 59.00 | suzhou   |
|  5 | lilei    | 68.00 | nanjing  |
+----+----------+-------+----------+
4 rows in set (0.00 sec)

查看表结构命令

mysql> desc student;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(3)       | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10)  | NO   |     | NULL    |                |
| score   | decimal(5,2) | YES  |     | NULL    |                |
| address | varchar(50)  | YES  |     | 未知    |                |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

修改用户登录mysql的密码

忘记密码
[root@localhost ~]# vim /etc/my.cnf
skip-grant-tables
[root@localhost ~]# mysql -u root -p
Enter password:      //不用输入密码,直接回车登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> update mysql.user set authentication_string=password('123456') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> quit
Bye

[root@localhost ~]# systemctl restart mysqld

在这里插入图片描述

DQL操作命令

DQL语句的作用
DQL是数据查询语句,只有一条:select
用于从数据表中查找符合条件的数据记录
不指定条件查询命令
select字段名1,字段名2…from表名

指定条件查询的命令
select字段名1,字段名2…from表名 WHERE 条件表达式

mysql> select * from student where score > 80;
+----+-----------+-------+----------+
| id | name      | score | address  |
+----+-----------+-------+----------+
|  3 | wangwu    | 98.00 | 未知     |
|  6 | hanmeimei | 99.00 | shanghai |
+----+-----------+-------+----------+
2 rows in set (0.00 sec)

mysql> create table tmp as select * from student where score > 80;
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| tmp              |
+------------------+
2 rows in set (0.00 sec)

3 rows in set (0.00 sec)

DCL操作命令

DCL语句的作用
设置或查看用户的权限,或者创建用户
设置用户权限的命令
若用户已存在,则更改用户密码
若用户不存在,则新建用户
grant 权限列表 on 数据库名.表名 to 用户名@来源地址 identified by ‘密码′

mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

查看用户权限的命令

show grants for 用户名@来源地址
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> show grants for 'root'@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.01 sec)

撤销用户权限的命令
revoke 权限列表 on 数据库名.表名 from 用户名@来源地址

mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> revoke all on *.* from 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants;
+--------------------------------------------------------------+
| Grants for root@localhost                                    |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'localhost' WITH GRANT OPTION   |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)

数据库高级操作
清空表

delete from tablename

truncate table tablename
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| tmp              |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from tmp;
+----+-----------+-------+----------+
| id | name      | score | address  |
+----+-----------+-------+----------+
|  3 | wangwu    | 98.00 | 未知     |
|  6 | hanmeimei | 99.00 | shanghai |
+----+-----------+-------+----------+
2 rows in set (0.00 sec)

mysql> delete from tmp;
Query OK, 2 rows affected (0.00 sec)

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

临时表

临时建立的表,用于保存一些临时数据,不会长期存在(连接断开,临时表被删除)

mysql> create temporary table temp_info (id int(4) not null  auto_increment,name varchar(10) not null,hobbby varchar(10) not null,primary key(id))engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> desc temp_info;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(4)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10) | NO   |     | NULL    |                |
| hobby | varchar(10) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> show tables;			'没有看到临时表,因为是放在内存当中'
+------------------+
| Tables_in_school |
+------------------+
| student          |
| tmp              |
+------------------+
2 rows in set (0.00 sec)

克隆表

直接克隆

mysql> create table kelong as select * from info;	'克隆表结构和数据'
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| kelong           |
| tmp              |
+------------------+
3 rows in set (0.00 sec)
like方法

从表完整复制结构生成新表,再导入数据

mysql> create table k like info;		'相比较like不能克隆表中的数据'
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| k                |
| kelong           |
| tmp              |
+------------------+
4 rows in set (0.00 sec)

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

mysql> insert into k select * from info;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from k;
+----+----------+-------+----------+
| id | name     | score | address  |
+----+----------+-------+----------+
|  1 | zhangsan | 66.00 | nanjing  |
|  3 | wangwu   | 77.00 | hangzhou |
+----+----------+-------+----------+
2 rows in set (0.00 sec)

show create table方法

mysql> show create table student\G;
*************************** 1. row ***************************
       Table: student
Create Table: CREATE TABLE `student` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `score` decimal(5,2) DEFAULT NULL,
  `address` varchar(50) DEFAULT '未知',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified

猜你喜欢

转载自blog.csdn.net/weixin_46355881/article/details/108173501