zoukankan      html  css  js  c++  java
  • 数据库 索引

    数据库索引的作用:

    设置了合适的索引加快查询速度,降低数据库的排序成本

    索引的分类:

    1)普通索引:基本的索引类型,而且没有唯一性之类的限制,

    2)唯一性索引:与普通索引基本相同,区别在于:索引列的所有值都只能出现一次:即必须唯一,但可以为空:

    3)主键:是一种特殊的唯一索引,必须指定"PRIMAPY KEY",具有唯一性不能为空(列:身份证号)

    4)全文索引:mysql3版本支持 ,在VARCHAR  TEXT创建,

    5)单列索引与多列索引: 索引可以是单列上创建的索引,也可以是多列上创建大的索引,

    索引的创建依据:

    1.表的主键,外键必须有索引,

    2.数量超过三百行的表因该有索引

    案列:

    创建库并支持中文字符集

    MariaDB [(none)]> create database imployee_salary default charset utf8;
    Query OK, 1 row affected (0.00 sec)

    MariaDB [(none)]> use imployee_salary
    Database changed

    在数据库里  创建数据表

    MariaDB [imployee_salary]> create table IT_salary(岗位类别 char(20) not null,姓名 char(20) not null, 年龄 int, 员工ID int not null,学历 char(6),年限 int,薪资 int not null, primary key(员工ID));
    Query OK, 0 rows affected (0.01 sec)

     在数据表里添加数据:

    MariaDB [imployee_salary]> insert into IT_salary(岗位类别,姓名,年龄,员工ID,学历,年限,薪资) values('.¡件驱动工程师','孙空无',27,011,'本科',3,4800);
    Query OK, 1 row affected (0.00 sec)

    MariaDB [imployee_salary]> insert into IT_salary(岗位类别,姓名,年龄,员工ID,学历,年限,薪资) values('windowds工程师','蓝领',19,021,'本科',2,3500);
    Query OK, 1 row affected (0.00 sec)

    MariaDB [imployee_salary]> insert into IT_salary(岗位类别,姓名,年龄,员工ID,学历,年限,薪资) values('linux工程师','姜文',32,013,'本科',8,15000);
    Query OK, 1 row affected (0.00 sec)

    MariaDB [imployee_salary]> insert into IT_salary(岗位类别,姓名,年龄,员工ID,学历,年限,薪资) values('java工程师','关原',38,014,'大专',10,16000);
    Query OK, 1 row affected (0.00 sec)

    MariaDB [imployee_salary]> insert into IT_salary(岗位类别,姓名,年龄,员工ID,学历,年限,薪资) values('网络工程师','罗仲昆',29,015,'大专',9,16500);
    Query OK, 1 row affected (0.00 sec)

     查看建立的表格:

    MariaDB [imployee_salary]> select * from IT_salary;
    +-----------------------+-----------+--------+----------+--------+--------+--------+
    | 岗位类别 | 姓名 | 年龄 | 员工ID | 学历 | 年限 | 薪资 |
    +-----------------------+-----------+--------+----------+--------+--------+--------+
    | 硬件驱动工程师 | 孙空无 | 27 | 11 | 本科 | 3 | 4800 |
    | linux工程师 | 姜文 | 32 | 13 | 本科 | 8 | 15000 |
    | java工程师 | 关原 | 38 | 14 | 大专 | 10 | 16000 |
    | 网络工程师 | 罗仲昆 | 29 | 15 | 大专 | 9 | 16500 |
    | windowds工程师 | 蓝领 | 19 | 21 | 本科 | 2 | 3500 |
    +-----------------------+-----------+--------+----------+--------+--------+--------+
    5 rows in set (0.00 sec)

    创建索引:

    格式:CREATE INDX<索引的名字> ON 表名(字段);

    创建普通索引:

    MariaDB [imployee_salary]> create index salary_index on IT_salary(薪资);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    查看索引:两种查看方式

    MariaDB [imployee_salary]> show index from IT_salary G

    MariaDB [imployee_salary]> show keys from IT_salary G

    创建唯一性索引:

    MariaDB [imployee_salary]> create index salary_unlpue__index on IT_salary(姓名);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    查看:

    MariaDB [imployee_salary]> show keys from IT_salary G

    创建主键:

    格式:CREATE TABLE 表名(|。。。),PRIMAPY KEY(字段);

        ALTER RABLE 表名 ADD PRIMAPY KEY(字段);

    两种方法:

    create table IT_salary(岗位类别 char(20) not null,姓名 char(20) not null, 年龄 int, 员工ID int not null,学历 char(6),年限 int,薪资 int not null, primary key(员工ID));
    Query OK, 0 rows affected (0.01 sec)

    MariaDB [imployee_salary]> alter table IT_salary add primary key(员工ID);

    删除索引:

    三种方法:

    MariaDB [imployee_salary]> drop index salary_index on IT_salary;
    Query OK, 0 rows affected (0.00 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    MariaDB [imployee_salary]> alter table IT_salary drop index salary_unlpue__index;
    Query OK, 0 rows affected (0.00 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    MariaDB [imployee_salary]> alter table IT_salary drop primary key;
    Query OK, 5 rows affected (0.00 sec)
    Records: 5 Duplicates: 0 Warnings: 0

  • 相关阅读:
    SQL Server优化(4)-聚集索引的重要性和如何选择聚集索引
    青岛开发区公交集团公交线路一览表
    WINDOWS下文件夹简介
    2008 SQL Server优化(2)-改善SQL语句
    【麦课在线教育mycourse】利用Js快速刷完新生安全教育课程
    Linux重启网卡的方法
    Trees
    计算机操作系统之设备管理
    linux目录结构及主要内容
    Linux mysql命令安装允许远程连接
  • 原文地址:https://www.cnblogs.com/liu1584712/p/11683793.html
Copyright © 2011-2022 走看看