MySQL常用命令

1.登录数据库

mysql -uroot -p<密码>

2.修改密码(三种方法)

1. mysql> set password for 'root'@'localhost' = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> alter user user() identified by '123456';
Query OK, 0 rows affected (0.01 sec)
3. mysql> update mysql.user set password=password('654321') where user='root'; 
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

Unknown column ‘password’ in 'field list’
(报这种错误的话是新的mysql数据库下已经没有password这个字段了,将等号之前password字段改成了authentication_string) 改完之后记得刷新权限

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

退出数据库(三种方法)

mysql> \q
mysql> exit
mysql> quit

3.数据库操作

● 查看当前用户

mysql> select user();

±---------------+
| user() |
±---------------+
| root@localhost |
±---------------+
1 row in set (0.00 sec)

● 查看当前使用的数据库

mysql> select database();

±-----------+
| database() |
±-----------+
| NULL |
±-----------+
1 row in set (0.00 sec)

● 创建库db1

mysql> create database db1;

Query OK, 1 row affected (0.00 sec)

● 查询库

mysql> show databases;

±-------------------+
| Database |
±-------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| sys |
| test |
±-------------------+
6 rows in set (0.00 sec)

● 切换库

mysql> use db1;

Database changed

● 删除库

mysql> drop database db1;

Query OK, 0 rows affected (0.01 sec)

4.表操作

4.1 创建表
命令:create table <表名> ( <字段名1> <类型1> [,…<字段名n> <类型n>]);
例:创建一个名为employess的表

mysql> CREATE TABLE employees ( number CHAR(7) NOT NULL PRIMARY KEY, name CHAR(8) NOT NULL, sex TINYINT NOT NULL, date DATE NOT NULL, Tel CHAR(11) NOT NULL, home CHAR(20))ENGINE=INNODB;
Query OK, 0 rows affected (0.02 sec)

4.2 更改表名称

mysql> alter table <想修改的表名> rename <修改后的表名>;

mysql> alter table employees rename t1;
Query OK, 0 rows affected (0.04 sec)

4.3 表插入数据(三种方法,效果相同)
向数据表employees中(表中列包括number,name,sex,date,Tel,home)插入如下的一行:
0000000 zhangsan 1 2021-5-15 12345678908 null
(null 表示家庭住址home这个字段为空值,暂不插入内容)

mysql> insert into employees
    -> values ('0000000','zhangsan',1,'2021-5-15','12345678908',null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into employees(number,name,sex,date,Tel)
    -> values ('0000001','lisi',0,'2021-5-15','12345678907');
Query OK, 1 row affected (0.00 sec)
mysql> insert into employees
    -> set number='0000002',name='wangwu',sex=1,date='2021-5-15',Tel='12345678906';
Query OK, 1 row affected (0.00 sec)

插入多行数据
例:insert into t1(id,name) values(1,‘php’),(2,‘java’),(3,‘python’);
4.4 查询表
查询数据库中所有表

show tables

±--------------+
| Tables_in_db1 |
±--------------+
| employees |
±--------------+
查询表中所有数据

mysql> select * from employees;

±--------±---------±----±-----------±------------±-----+
| number | name | sex | date | Tel | home |
±--------±---------±----±-----------±------------±-----+
| 0000000 | s7 | 1 | 2021-05-15 | 12345678908 | NULL |
| 0000001 | s7 | 0 | 2021-05-15 | 12345678907 | NULL |
| 0000002 | s7 | 1 | 2021-05-15 | 12345678906 | NULL |
±--------±---------±----±-----------±------------±-----+
查询表中某列信息

mysql> decs <表名> <列名>

查询表中的某人各别信息

select <列名>,<列名>
from employees
where <列名>='信息'

例:查询数据库db1的数据表employees中number为0000001的name、sex、date。
mysql> select name,sex,date
-> from employees
-> where number=‘0000001’;
±-----±----±-----------+
| name | sex | date |
±-----±----±-----------+
| lisi | 0 | 2021-05-15 |
±-----±----±-----------+
假如查询表中员工姓“张”的一些信息则修改where子句即可
例:where 员工姓名 like ‘张%’;
查员工编号第三位数字为3的即为:where 员工编号 like ‘_3%’;
4.5 更新表数据

mysql> update employees
    -> set name='zhouba'
    -> where number='0000002';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

如果employees数据表中有多条记录,不添加where子句,则将所记录行name都变成zhouba
更新完查询
mysql> select * from employees;
±--------±---------±----±-----------±------------±-----+
| number | name | sex | date | Tel | home |
±--------±---------±----±-----------±------------±-----+
| 0000000 | s7 | 1 | 2021-05-15 | 12345678908 | NULL |
| 0000001 | s7 | 0 | 2021-05-15 | 12345678907 | NULL |
| 0000002 | zhouba | 1 | 2021-05-15 | 12345678906 | NULL | |
±--------±---------±----±-----------±------------±-----+
4.6 删除数据表(两种方法)

mysql> delete from <表名>

mysql> delete from employess;
Query OK, 1 row affected (0.00 sec)

mysql> truncate table <表名>

mysql> truncate table employesa;
Query OK, 0 rows affected (0.03 sec)

4.7 清空表

mysql> delete from employees;

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

清空某一行数据

mysql> delete from employees number='0000000';

4.8 显示表结构

mysql> describe employees;

±-------±-----------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±-------±-----------±-----±----±--------±------+
| number | char(7) | NO | PRI | NULL | |
| name | char(8) | NO | | NULL | |
| sex | tinyint(4) | NO | | NULL | |
| date | date | NO | | NULL | |
| Tel | char(11) | NO | | NULL | |
| home | char(20) | YES | | NULL | |
±-------±-----------±-----±----±--------±------+
6 rows in set (0.01 sec)
4.9 修改表数据
增加一列

mysql> alter table <表名> add <列名> <类型>;

例:在数据库db1中的employees表中增加cloumn列(text为类型)
mysql> alter table employees add cloumn text;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除某列

mysql> alter table  <表名> drop <列名> ;

mysql> alter table employees drop cloumn;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

5.索引

(1)普通索引(INDEX):允许重复和空值
(2)唯一索引(UNIQUE):不允许出现重复。可以有多个唯一索引。唯一可为null;唯一约束放在一 个或者多个列上,这些列或列的组合必须有唯一的;
(3)主键索引(PRIMARY KEY):非空,唯一
(4)复合索引:用户可以在多个列上建立索引,这种索引叫做复合索引(组合索引)
(5)全文索引(FULLTEXT):可重复和空值,只能在char,varchar,text类型创建
where match(列名)against (‘查找内容’);
(6)空间索引:对空间数据类型的列建立的索引
聚合索引(可以通过索引找到需要的数据)
非聚合索引(可以查到记录对应的主键值,再使用主键的值通过索引找到需要的数据)
创建索引:
create 【索引类型】index 索引名 on 表名 (创建索引的列);
或者创建表时之间在列后面加上索引类型。
或者修改表alter table 表名 add index 索引名 (索引列);
删除索引:drop index 索引名;
查看索引:show index from 表名;

索引命令参考于:链接

6.权限

权限相关

授予操作权限(将test数据库中所有表的select和insert权限授予test用户):

grant select,insert on test.* to 'test'@'localhost' identified by '123'

查看账号权限:

show grants for 'test'@'localhost'

收回操作权限:

revoke insert on test.* from 'test'@'localhost'

授予所有数据库的所有权限:

grant all privileges on *.* to 'test'@'localhost'

授予所有数据库的所有权限(包括grant):

grant all privileges on *.* to 'test'@'localhost' with grant option

授予SUPER PROCESS FILE权限(系统权限不能指定数据库):

grant super,process,file on *.* to 'test'@'localhost'

只授予登录权限:

grant usage on *.* to 'test'@'localhost'

权限命令参考于:链接

7.数据库备份

7.1 备份库

mysqldump -uroot -p  <库名> > /<存放路径>/<库名>.sql

[root@aslb ~]# mysqldump --set-gtid-purged=OFF -uroot -p db1 > /tmp/db1.sql
Enter password:

加了–set-gtid-purged=OFF时,在会记录binlog日志,如果不加,不记录binlog日志,所以在我们做主从用了gtid时,用mysqldump备份时就要加–set-gtid-purged=OFF,否则你在主上导入恢复了数据,主没有了binlog日志,同步则不会被同步。

7.2 恢复库

mysql -uroot -p <库名> < /<存放路径>/<库名>.sql

[root@aslb ~]# mysql -uroot -p db1 < /tmp/db1.sql
Enter password:

备份的是数据而不是库,恢复时恢复的是数据不是库,如果不小心将库删除,则新建一个库即可

7.3 备份表

mysqldump -uroot -p <库名> <表名> > /<存放路径>/<表名>.sql

[root@aslb ~]# mysqldump --set-gtid-purged=OFF -uroot -p db1 employees > /tmp/employees.sql
Enter password:

7.4 恢复表

mysql -uroot -p <库名>  < /<存放路径>/<表名>.sql

[root@aslb ~]# mysql -uroot -p db1 < /tmp/employees.sql
Enter password:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值