数据库表的约束和索引
来源:互联网 发布:淘宝推广教程视频 编辑:程序博客网 时间:2024/05/20 19:15
表的约束
* 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字段定义单列索引,并且索引的长度为20create 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);
阅读全文
0 0
- 数据库表的约束和索引
- 数据库索引和约束
- ORACLE的索引和约束详解数据库
- ORACLE的索引和约束
- oracle的约束和索引
- Oracle的约束和索引
- Oracle的约束和索引
- oracle的约束和索引
- 索引和约束的创建
- MySQL的约束和索引
- 数据库中的索引、键和约束
- ORACLE数据库的数据对象-表、约束、视图、索引、序列
- SQL删除数据库的约束,索引。
- SQL 数据库 学习 010 表和约束的区别、check约束、default约束、unique约束、not null约束
- 数据库索引与约束
- 表的约束与索引
- 查询表的约束、索引
- 保留主表,删除一类表的索引和约束
- ASP.NET Core 中的依赖项管理
- 显示栅格图层和矢量图层的属性表(AE开发)
- 今天跟新AS和NDK开发遇到几个很骚的问题
- Android Wi-Fi MIMO/SISO设置方法(基于高通平台)
- hdu2795-Billboard (线段树求区间最大值)
- 数据库表的约束和索引
- ramdisk.img的生成过程
- macOS系统下搭建React开发环境
- spark--actions算子--first
- 解决Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds.问题
- 自己动手写一个依赖注入框架
- Hibernate(四)一对多映射 补充三:双向多对一关系(学生-->班级)
- 欢迎使用CSDN-markdown编辑器
- spring生成代理对象的过程(AOP切面)