Mariadb or MySQL 的基本使用
1、常用命令:
1) 登录数据库:
[root@slave ~]# mysql -uroot -p
2)修改用户密码:
方法一:[root@slave ~]# mysqladmin -uroot -predhat password biezzz redhat:旧密码 biezzz:新密码
方法二:MariaDB [(none)]> set password for root@localhost = password('biezzz');
Query OK, 0 rows affected (0.00 sec)
3) 数据库的增删查:
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | form | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.01 sec)
MariaDB [(none)]> create database biezzz; #创建数据库
MariaDB [(none)]> drop database biezzz; #删除数据库
MariaDB [(none)]> use form; #选择数据库
4)表&&表内容的增删改查:
MariaDB [form]> create table myclass(
-> id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> sex char(10) not null,
-> score int(3) not null default 0);
Query OK, 0 rows affected (0.01 sec) #表的创建
MariaDB [form]> drop table myclass; #表的删除
Query OK, 0 rows affected (0.01 sec)
MariaDB [form]> show tables; #表的查询
+----------------+
| Tables_in_form |
+----------------+
| myclass |
+----------------+
1 row in set (0.00 sec)
MariaDB [form]> rename table myclass to Class; #修改表名(慎用)
Query OK, 0 rows affected (0.01 sec)
MariaDB [form]> insert into myclass values(1,'xiaoming','man',80); #表中插入数据
Query OK, 1 row affected (0.01 sec)
MariaDB [form]> select * from myclass; #查询表内容
+----+----------+-----+-------+
| id | name | sex | score |
+----+----------+-----+-------+
| 1 | xiaoming | man | 80 |
+----+----------+-----+-------+
1 row in set (0.00 sec)
MariaDB [form]> update myclass set sex='woman' where id=3; #修改表中数据
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [form]> select * from myclass;
+----+----------+-------+-------+
| id | name | sex | score |
+----+----------+-------+-------+
| 1 | xiaoming | man | 80 |
| 2 | xiaowu | man | 65 |
| 3 | xiaotian | woman | 77 |
+----+----------+-------+-------+
3 rows in set (0.00 sec)
MariaDB [form]> alter table myclass add age int(3) not null; #增加字段
MariaDB [form]> select * from myclass;
+----+----------+-------+-------+-----+
| id | name | sex | score | age |
+----+----------+-------+-------+-----+
| 1 | xiaoming | man | 80 | 0 |
| 2 | xiaowu | man | 65 | 0 |
| 3 | xiaotian | woman | 77 | 0 |
+----+----------+-------+-------+-----+
2、索引
作用;用于快速查找出某一列中特定值的行。如果没有索引,数据库查询数据的时候就遍历整个表,直到找到相关的行,这样就花费很多时间。 索引会将某一列的值按照一定方法进行存储,好让查询字段上信息的时候,快速找到对应的数据,而不用遍历整个列中的所有的值。
索引的存储类型:BTREE,HASH 即用树或者hash值来存储字段。
关于索引的内容,推荐参考:https://www.cnblogs.com/whgk/p/6179612.html
工欲善其事,必先利其器