一文搞懂MySQL兄弟数据库MariaDB的安装和使用( 三 )


授权完成后,切换到heima用户,再次查看数据库就可以看到刚才授权的mysql数据库了,并且可以操作mysql数据库中user表的内容
MariaDB [(none)]> show databases;+--------------------+| Database|+--------------------+| information_schema || mysql|+--------------------+2 rows in set (0.01 sec)MariaDB [(none)]> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [mysql]> show tables;+-----------------+| Tables_in_mysql |+-----------------+| user|+-----------------+1 row in set (0.00 sec)MariaDB [mysql]> select host,user,password from user where user='heima';+-----------+-------+-------------------------------------------+| host| user| password|+-----------+-------+-------------------------------------------+| localhost | heima | *58613E96F5518C264EA39AA2A57D3DFEB191E343 |+-----------+-------+-------------------------------------------+1 row in set (0.00 sec)MariaDB [mysql]> exitBye[root@mariadb ~]# 3.2.2 移除账户权限当员工离职或其他原因需要移除账户权限时,可以使用root管理员登录,通过revoke语句进行移除
MariaDB [(none)]> revoke select,update,delete,insert on mysql.user from heima@localhost;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> show grants for heima@localhost;+------------+| Grants for heima@localhost|+------------+| GRANT USAGE ON *.* TO 'heima'@'localhost' IDENTIFIED BY PASSWORD '*58613E96F5518C264EA39AA2A57D3DFEB191E343' |+-------------+1 row in set (0.00 sec)MariaDB [(none)]> exit四、MariaDB数据库和表管理4.0 知识储备简单列举几个最基础的命令

  • 创建数据库
CREATE DATABASE 数据库名称 (大小写不敏感,大小写都是可以的)
  • 描述表
DESCRIBE 表单名称
  • 更新表单中的数据
UPDATE 表单名称 SET attribute=新值 WHERE attribute>原始 值
  • 指定使用的数据库
USE 数据库名称
  • 显示当前已有的数据库
SHOW databases
  • 显示当前数据库中的表
SHOW tables
  • 从表单中选中某个记录值
SELECT * FROM 表单名称
  • 从表单中删除某个记录值
DELETE FROM 表单名 WHERE attribute=值
4.1 创建数据库创建一个名为 heima的数据库
MariaDB [(none)]> create database heima;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;+--------------------+| Database|+--------------------+| information_schema || heima|| mysql|| performance_schema |+--------------------+4 rows in set (0.00 sec)MariaDB [(none)]>4.2 创建数据库表切换到刚才创建的heima数据库,在其中创建user表,包含姓名和年龄两个字段
MariaDB [(none)]> use heimaDatabase changedMariaDB [heima]> create table user(name char(15),age int);Query OK, 0 rows affected (0.01 sec)MariaDB [heima]> show tables;+-----------------+| Tables_in_heima |+-----------------+| user|+-----------------+1 row in set (0.00 sec)MariaDB [heima]> describe user;+-------+----------+------+-----+---------+-------+| Field | Type| Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| name| char(15) | YES|| NULL||| age| int(11)| YES|| NULL||+-------+----------+------+-----+---------+-------+2 rows in set (0.00 sec)MariaDB [heima]> 五、MariaDB表数据管理数据库表中数据的查找分为CRUD,也就是通常所说的增、删、改、查 。
5.1 添加数据使用insert into语句向heima数据库的user表中插入数据
MariaDB [heima]> insert into user(name,age) values('heima',18);Query OK, 1 row affected (0.00 sec)MariaDB [heima]> select * from user;+-------+------+| name| age|+-------+------+| heima |18 |+-------+------+1 row in set (0.00 sec)MariaDB [heima]> 5.2 查询数据查询使用select语句,并可以结合where、group by、order by等语句进行综合查询 。
在user表插入一条数据,然后根据年龄查询小于1岁的用户
MariaDB [heima]> insert into user(name,age) values("leo",1);Query OK, 1 row affected (0.00 sec)MariaDB [heima]> select * from user where age<2;+------+------+| name | age|+------+------+| leo|1 |+------+------+1 row in set (0.00 sec)MariaDB [heima]>5.3 修改数据修改数据使用update语句,在user表中将heima用户的年龄修改为19岁
MariaDB [heima]> update user set age=19 where name='heima';Query OK, 1 row affected (0.00 sec)Rows matched: 1Changed: 1Warnings: 0MariaDB [heima]> select * from user;+-------+------+| name| age|+-------+------+| heima |19 |+-------+------+1 row in set (0.00 sec)MariaDB [heima]> 5.4 删除数据删除数据使用delete语句,以下分别演示,删除用户名为leo的用户和删除所有用户


推荐阅读