索引,数据导入

1、索引
    1、BTREE
    2、优点 :加快数据的检索速度
    3、缺点
        1、需动态维护,占用系统资源,降低数据维护速度
        2、占用物理存储空间
    4、索引示例
        1、开启运行时间检测 :set profiling=1;
            备注 :show variables like "profiling";
        2、执行查询语句
            select name from t1 where name="lucy99999";
        3、查看执行时间
            show profiles;
        4、给name字段创建索引
            create index name on t1(name);
        5、执行查询语句
            select name from t1 where name="lucy88888";
        6、查看执行时间
            show profiles;
    5、索引类型
        1、普通索引(index)
            1、使用规则
                1、可设置多个字段
                2、约束 :无
                3、KEY标志 :MUL
            2、创建
                1、创建表时
                    index(字段名),
                    index(字段名),
                2、已有表
                    create index 索引名 on 表名(字段名);
            3、查看
                1、desc 表名;
                2、show index from 表名\G;
            4、删除
                drop index 索引名 on 表名;
        2、唯一索引(unique)
            1、使用规则
                1、可设置多个字段
                2、约束 :字段值不允许重复,允许为NULL
                3、KEY标志 :UNI
            2、创建
                1、创建表
                    unique(字段名),
                    unique(字段名)
                2、已有表
                    create unique index 索引名 on 表名(字段名);
            3、查看、删除同普通索引
                Non_unique: 0 --> 唯一索引
                Non_unique: 1 --> 普通索引
        3、主键(primary key)&&自增长属性(auto_increment)
            1、使用规则
                1、只能有一个主键字段
                2、约束 :不允许重复,不能为NULL
                3、KEY标志 :PRI
                4、通常设置记录编号字段 id 为主键,唯一锁定一条记录
            2、创建
                1、创建表时
                    id int primary key auto_increment,
                2、已有表
                    alter table 表名 add primary key(字段名);
            3、删除
                1、删除自增长属性
                    alter table 表名 modify id int;
                2、删除主键
                    alter table 表名 drop primary key;
            
                    主键:primary key  
                        添加、删除 :add/drop primary key..

                    自增长:auto_increment
                        添加、删除 :modify id int;
            4、指定自增长属性起始值
                1、创建表
                    create table 表名(
                    id int primary key auto_increment,
                    ... ...
                    )auto_increment=10000;
                2、已有表
                    alter table 表名 auto_increment=10000;
        4、外键(foreign key)
2、数据导入
    1、把文件系统内容导入到数据的表中
    2、命令格式
        load data infile "文件名"
        into table 表名
        fileds terminated by "分隔符"
        lines terminated by "\n"
    3、将scoretable.csv文件导入到db3库下的score表中
        1、先把scoretable.csv文件拷贝到数据库的默认搜索路径中
            1、查看搜索路径方法
                show variables like "secure_file_priv";
            2、执行复制命令
                sudo cp /home/tarena/scoretable.csv /var/lib/mysql-files/
        2、创建库、表(utf8字符集)
            1、create database db3 character set utf8;
            2、use db3;
            3、
                create table score(
                id int,
                name varchar(20),
                score decimal(5,2),
                phone char(11),
                class char(7)
                )character set utf8;
        3、执行导入语句
            load data infile "/var/lib/mysql-files/scoretable.csv"
            into table score
            fields terminated by ","
            lines terminated by "\n";

            注意:
                1、库、表必须都为utf8编码
                2、路径必须写绝对路径 /var/lib/mysql-files/..."
        

猜你喜欢

转载自blog.csdn.net/weixin_42584821/article/details/81805701