数据库表的约束和索引

表的约束

*   primary key 主键约束
    *   单字段主键 :字段名  数据类型  primary key;
    *   eg: id int primary key;
    *   多字段主键 : primary key(字段名1,字段名2);
    *   eg: primary key(id,name);
*   foreign key 外键约束
    *   字段名 数据类型  foreign key;
    *   ed: name varchar(20) foreign key;
*   not null   非空约束
    *   字段名 数据类型  not null;
    *   eg:sex varchar(20) not null; 
*   unique   唯一性约束
    *   字段名 数据类型 unique
    *   eg:QQ varchar(20) unique;
*   default  默认值约束
    *   字段名 数据类型 default 默认值;
    *   eg score float default 0;
*   设置表的字段值自动增加
    *   字段名 数据类型 auto_increment;
    *   eg:id int auto_increment;

索引

* 索引:它是对数据库表中一列或多列的值进行排序后的一中结构,作用就是提高表中数据的查询速度
* 创建索引的三种方式
* 创建表的时候创建索引

            create table 表名(
                字段名 数据类型 [完整性约束条件],
                [UNIQUE|FULLTEXT|SPATIAL] INDEX|KEY
                [别名](字段名1[(长度)]) [ASC|DESC])
            );
                eg: create table book2(
                id int,
                name varchar(20),
                index (id));
*   使用create index语句在已经存在的表上创建索引

         create table book2(
                id int,
                name varchar(20),
                UNIQUE index unique_id(id ASC)
                );
*   使用alter table语句在已经存在的表上创建索引
*   
         alter table 表名 add [UNIQUE|FULLTEXT|SPATIAL] INDEX 索引名(字段名[(长度)][(ASC|DESC)])

* 删除索引
*
* alter table 表名 drop index 索引名
* drop index 索引名 on 表名
* 普通索引: 由key和index定义的索引

    *   在t1表中的id字段建立索引
    create table t1(
    id int ,
    name varchar(20),
    index(id)
    );
    *   使用create index语句在已经存在的表上建立索引
        create index index_name on t1(name);
    *   使用alter table语句在已经存在的表上建立索引
        alter table t1 add index index_id(id);

* 唯一性索引 :由unique定义的索引

    *   为表t2中的id字段添加唯一性索引
    create table t2(
    id int not null,
    name varchar(20) not null,
    score float,
    unique index unique_id (id ASC)
    );
    *   使用create index语句在已经存在的表上建立索引
        create unique index unique_name on t2(name);
    *   使用alter table语句在已经存在的表上建立索引
        alter table t2 add unique index unique_id(id);
  • 全文索引 : 由fulltext定义的索引

    *   为表t3中的name字段创建定义全文索引
    create table t3(
    id int,
    name varchar(20) not null,
    score float,
    fulltext index fulltext_name(name)
    ) engine=myisam;
    *   使用create index语句在已经存在的表上建立索引
        create fulltext index fulltext_name on t3(name);
    *   使用alter table语句在已经存在的表上建立索引
        alter table t3 add fulltext index fulltext_name(name);
    
  • 单列索引 :指在表中的单个字段上创建索引,可以是普通索引、唯一性索引或者全文索引

    *   为表t4中的name字段定义单列索引,并且索引的长度为20
    create table t4(
    id int not null,
    name varchar(20),
    score float,
    index signle_name(name(20))
    );
    *   使用create index语句在已经存在的表上建立索引
        create index single_name on t4(name);
    *   使用alter table语句在已经存在的表上建立索引
        alter table t4 add index index_id(name(20));
    
  • 多列索引 :指在表中多个字段上创建索引,只有在查询条件中使用了这些字段中的第一个字段时,该索引才会被使用。例如 在grade表中的id、name和score字段上创建一个多列索引,只有查询条件中使用了id字段时,该索引才会被使用

    *   为表t5中的name和score定义多列索引
    create table t5(
    id int not null,
    name varchar(20) not null,
    score float,
    index multi(name(20),score)
    );
    *   使用create index语句在已经存在的表上建立索引
        create index muitl on t5(name,score);
    *   使用alter table语句在已经存在的表上建立索引
        alter table t5 add index muitl(name(20),score);
    
  • 空间索引 : 由spatial定义的索引。创建空间索引的时候,必须将字段声明为not null

    *   为表t6中的space字段定义空间索引
    create table t6(
    id int,
    space geometry not null,
    spatial index sp(space)
    ) engine=myisam;
    *   使用create index语句在已经存在的表上建立索引
        create spatial index space_name on t6(space);
    *   使用alter table语句在已经存在的表上建立索引
        alter table t6 add spatial index sp(space);
    

猜你喜欢

转载自blog.csdn.net/eternally__/article/details/75012541