Database indexes and tables built

First, the index

1, a clustered index

Usually, when construction of the table will add a primary key for the table, in some relational database, if you do not specify a primary key table when built, the database will refuse to execute the construction of the table statement. In fact, the addition of a table's primary key, and not be called "table." Did not add a primary key table, its data disorderly placed on disk storage, line by line arranged very neatly, with my knowledge of the "table" is very close. If the primary key to the table, then the table is stored on disk structure is transformed by the alignment of the structure became a tree structure, similar to the entire table becomes an index, which is the so-called "clustered index." This is why a table can have only one primary key, a table can have only a "clustered index", because the role of the primary key is to convert the "table" of the data format to "index (balanced tree)" format placement.

The figure is the table (clustered index) with a configuration diagram of a primary key. Wherein all the nodes of the tree (except the bottom) of the data is constructed by data primary key field, is generally designated our primary key id field. The bottom portion of the data is a real table.

If we execute a SQL statement:

select * from table where id = 98;

The first index of the target 98 where the value of the leaf node, then take id equal to 98 through a data line of the leaf nodes. Not here to explain the details of running a balanced tree, but can be seen from the figure, a total of three tree from root to leaf nodes only need to be able to get the results look after three.

Cons: This balanced tree structure must be maintained in a proper state, additions and deletions to the index data will change the balance of the data content of each node in the tree, destroying the tree structure, and therefore, each time the data changes, you must go to re-sort the tree (index ) structure to ensure that it is correct, this will bring no small performance overhead, that is, to bring reason why the side effects of operations outside the index will query.

2, non-clustered index

Non-clustered index and a clustered index, same as a balanced tree data structure as an index. Value of the index tree structure each node from the table index field, if the user name field to the table, plus index, the index value is composed of a name field, when the data changes, the DBMS has been required to maintain the index structure correctness. If we add the index to the table in a plurality of fields, a plurality of separate index structure will then occur, each index (non-clustered index) no correlation between each other. As shown below

 

 

// create a non-clustered index

create index index_birthday on user_info(birthday);

//查询生日在1995年11月1日出生用户的用户名

select user_name from user_info where birthday = '1995-11-01'

这句SQL语句的执行过程如下

首先,通过非聚集索引index_birthday查找birthday等于1995-11-01的所有记录的主键id值。然后,通过得到的主键id值执行聚集索引查找,找到主键id值对应的真实数据(数据行)存储的位置。最后,从得到的真实数据中取得user_name字段的值返回,取得最终的结果

非聚集索引和聚集索引的区别在于,通过聚集索引可以查到需要查找的数据, 而通过非聚集索引可以查到记录对应的主键值 ,再使用主键的值通过聚集索引查找到需要的数据。

3、复合索引

有一种例外可以不使用聚集索引就能查询出所需要的数据, 这种索引称之为「覆盖索引」查询, 也就是复合索引或者多字段索引查询。当为字段建立索引以后,字段中的内容会被同步到索引之中,如果为一个索引指定两个字段,那么这个两个字段的内容都会被同步至索引之中。

先看下面这个SQL语句

//创建复合索引

create index index_birthday_and_user_name on user_info(birthday, user_name);

select user_name from user_info where birthday = '1995-11-01'

这句SQL语句的执行过程:

通过非聚集索引index_birthday_and_user_name查找birthday等于1995-11-01的叶节点的内容,叶节点中除了有user_name表主键id的值以外user_name字段的值也在里面,因此不需要通过主键id值去查找数据行的真实所在,直接取得叶节点中user_name的值返回即可,大大的提高了查询性能。

4、注意点

最左前缀匹配原则,非常重要的原则

create index index_name_email on user(name,email)

- 最左前缀匹配:必须按照从左到右的顺序匹配

select * from user where name='zhangsan'; #可以

select * from user where name='zhangsan'and email='163.com'; #可以

select * from user where email='[email protected]'; #不可以

 

二、建表

1.新建用户

1.1 登录MYSQL:

C:\mysql\mysql-5.6.41-winx64\bin>mysql -u root -p

Enter password: **********

 

1.2 创建用户:

mysql> insert into mysql.user(Host,User,Password) values("localhost","demo",password("1234"));

这样就创建了一个名为:demo 密码为:1234 的用户。

注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。

 

1.3 然后登录一下:

mysql>exit;

C:\mysql\mysql-5.6.41-winx64\bin>mysql -u demo -p

Enter password: **********

2.为用户授权

授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

2.1 登录MYSQL(有ROOT权限),这里以ROOT身份登录:

C:\mysql\mysql-5.6.41-winx64\bin>mysql -u demo -p

Enter password: **********

 

2.2 首先为用户创建一个数据库(testDB):

mysql>create database demoDB;

 

2.3 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):

mysql>grant all privileges on demoDB.* to demo@localhost identified by '1234';

mysql>flush privileges;//刷新系统权限表

格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

 

2.4 如果想指定部分权限给一用户,可以这样来写:

mysql>grant select,update on demoDB.* to demo@localhost identified by '1234';

mysql>flush privileges; //刷新系统权限表

 

2.5 授权test用户拥有所有数据库的某些权限:  

mysql>grant select,delete,update,create,drop on *.* to demo@"%" identified by "1234";

//test用户对所有数据库都有select,delete,update,create,drop 权限。

//@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)

//对localhost授权:加上一句grant all privileges on demoDB.* to demo@localhost identified by '1234';即可。

3. 删除用户

C:\mysql\mysql-5.6.41-winx64\bin>mysql -u demo -p

Enter password: **********

mysql>Delete FROM user Where User='demo' and Host='localhost';

mysql>flush privileges;

mysql>drop database demoDB; //删除用户的数据库

删除账户及权限:>drop user 用户名@'%';

        >drop user 用户名@ localhost;

4. 修改指定用户密码

C:\mysql\mysql-5.6.41-winx64\bin>mysql -u demo -p

Enter password: **********

mysql>update mysql.user set password=password('新密码') where User="demo" and Host="localhost";

mysql>flush privileges;

5. 列出所有数据库

mysql>show database;

6. 切换数据库

mysql>use '数据库名';

7. 列出所有表

mysql>show tables;

8. 显示数据表结构

mysql>describe 表名;

9. 删除数据库和数据表

mysql>drop database 数据库名;

mysql>drop table 数据表名;

Guess you like

Origin www.cnblogs.com/supiaopiao/p/10948478.html