时间:2021-07-01 10:21:17 帮助过:15人阅读
一、MySQL当中的索引:
	
	数组当中我们见过索引;它的好处就是能够快速的通过下标、索引将一个信息查到;或者说
	能够快速的定位到一个信息;
	
	1.MySQL中的索引是什么?
	
		它是将我们表中具有索引的那个字段,单独的存储到了一张表之中(MyISAM存储引擎),
		当我们再次查询表中的数据时,如果你搜索的条件,是具有索引的那个字段,这会,它
		不再遍历表中的所有信息了,而是去索引表中,快速的定位到你要搜索的那条数据,
		它有一个指针是指向我们数据表中的源信息的,由此一来,就可以让我们能快速的从
		一个具有庞大数量级的数据库中准确的快速的取出某条信息;
		
	2.MySQL中(MyISAM存储引擎)存储表的方式;
	
		1. .frm	数据表的结构
		
		2. .MYD 数据表的数据
		
		3. .MYI 数据表中的索引字段
		
	3.MySQL当中的索引,有哪几种呢?
	
		索引,在我们定义之后,不用刻意的去使用,当我们在查询表中具有索引的字段的时候
		索引会自动生效;
	
		1> 普通索引(index)(MUL代表普通索引)
			特点:没有任何限制,当我们定义了普通索引之后,直接搜索数据即可使用它
			
			① 在创建表的时候,定义一个普通索引
				create tabel test1(
				 id int unsigned not null,
				 name varchar(32) not null,
				 sex enum(‘m‘,‘w‘) not null default ‘w‘,
				 age tinyint not null default 18,
				 index id(id)	索引类型 索引名(字段名)
				);
				
			② 在建表之后,给某个字段添加普通索引
				create index id on test1(id);
				create 索引类型 索引名 on 表名(字段名);
				
			③ 删除一个普通索引的方法
				drop index id on test1;
				drop 索引类型 索引名 on 表名;
		
		2> 唯一索引(unique)(UNI代表唯一索引)
			特点:具有唯一索引的字段,它的值只能出现一次,出现重复的值则会报错!
				  同时,一个表中可以有多个字段添加唯一索引
				  
			① 在建表时创建唯一索引的方法一
				create table test1(
				 id int unsigned not null,
				 name varchar(32) not null,
				 sex enum(‘w‘,‘m‘) not null default ‘m‘,
				 age tinyint not null default 18,
				 unique index name(name)	//索引类型 索引名(字段名)
				);
				
			② 在建表时创建唯一索引的方法二
				create table test1(
				 id int unsigned not null,
				 name varchar(32) not null unique,	//直接给字段添加唯一索引
				 sex enum(‘w‘,‘m‘) not null default ‘w‘,
				 age tinyint not null default 18
				);
				
			③ 在建表之后添加一个唯一索引
				create unique index id on test1(id);
				create 索引类型 索引名 on 表名(字段名);
				
			④ 删除一个表中的唯一索引的方法
				drop index id on test1;
				drop 索引类型 索引名 on 表名;
		
		3> 主键索引(primary key)
			特点:它的唯一索引基本上使用方法以及特性一致,唯一的区别是,唯一索引在
				  一个表中可以多次定义、主键索引只能定义一次,而且主键索引一般我们
				  会添加到id字段当中
				  
			① 建表时创建一个主键索引的方法
				create table test1(
				 id int unsigned not null auto_increment primary key,	//添加主键
				 name varchar(32) not null,
				 sex enum(‘w‘,‘m‘) not null default ‘m‘,
				 age tinyint not null default 18
				);
				
			② 建表之后,添加一个主键索引的方法
				
				1.alter table test1 change id id int unsigned not null auto_increment primary key;
				  alter table 表名  change 字段原名 字段新名 类型 约束条件……;
				
				2.alter table test1 modify id int unsigned not null auto_increment priamry key;
				  alter table 表名  modify 字段名 类型 约束条件……;
				  
			③ 删除主键索引的方法
				
				因为主键索引比较特殊,所以我们在删除主键索引时,必须先来查看表结构,看表中
				具有主键索引的那个字段,是否同时拥有 auto_increment 这个约束条件,如果有,
				先删除 auto_increment 这个约束条件,之后才能删除主键索引
				
				1.先查看表结构,查看是否拥有 auto_increment 关键字 
					desc 表名;
					
				2.如果有 auto_increment 关键字,则需要先删除该关键字
					alter table test1 modify id int unsigned not null;
					alter table 表名 modify 字段名 字段类型 约束条件;
					
				3.删除主键索引
					alter table test1 drop primary key;
					alter table 表名 drop 主键索引;
		
		4> 全文索引
		
二、存储引擎(了解):
	
	事务处理:有时,当你执行一个操作的时候,断电可能会导致一些不必要的麻烦,就比如
	电子转账操作,如果说此时断电,所有的事务操作都会有一个回滚的效果,恢复到上一次
	断点存储的位置,避免出现其他的问题
	
	1.MyISAM存储引擎 
		对于我们一个表的操作,如果是查询比较频繁的表,我们使用MyISAM的存储引擎来
		进行存储,因为它不支持事务操作
		
	2.InnoDB存储引擎
		因为这种存储引擎它支持事务的操作,对于一个表的增、删、改操作比较频繁,就需要
		我们的表支持事务处理,由此一来,就大大降低了表的查询速度。
		
	3.选择什么样的存储引擎,关键在于你的项目各种功能所需要的表的不同,去选择一个
      更合适的存储引擎
	  
	4.如何来指定一个表的存储引擎:
	
		create table test1(
		 id int unsigned not null auto_increment primary key,
		 name varchar(32) not null unique,
		 sex enum(‘w‘,‘m‘) not null default ‘m‘
		)engine=MyISAM[InnoDB];
		
		
	5.如何来查看一个表的存储引擎
	
		show create table 表名;
		
三、MySQL当中的编码格式:
	1.查看我们能够设置的编码格式:
	
		show character set;
		
	2.在MySQL服务器中的编码类型的4个级别
	
		1> 服务器级
		
		2> 数据库级
		
		3> 数据表级
		
		4> 数据字段级
		
	3.编码级别的一个特性:
	
		它具有一个继承的特性,当我们设置了服务器级别的编码类型之后,我们在该服务器
		下所创建的所有的数据库、数据表、数据字段都是跟随服务器级别的编码类型了
		
	4.如何来设置一个编码类型
	
		1> 设置服务器级别的编码类型
		
			set character_set_server = "utf8";
			
		2> 设置数据库级别的编码类型
		
			① 在创建一个数据库时设置默认的编码类型
				create database test default charset="utf8";
				create database 数据库名 默认编码类型="utf8";
			
			② 修改一个数据库的编码类型
				alter database test default charset="utf8";
				alter database 数据库名 默认编码类型="utf8";
				
		3> 设置数据表级别的编码类型
		
			① 创建一个数据表时设置默认的编码类型
				create table test(
				 id int unsigned not null auto_increment priamry key
				)engine=MyISAM default charset="utf8";
				
			② 修改数据表的编码类型
				alter table test default charset="utf8";
				
		4> 设置数据字段级的编码类型
		
			① 修改一个数据字段级的编码
				alter table test modify name varchar(32) character set "utf8";
				
		5> 设置DOS命令框的编码格式
			set names utf8;
			
四、修改表结构
	1.添加表字段:
		alter table test1 add name varchar(32) not null unique;	//不指定位置,则默认在最后出现
		alter table test1 add name varchar(32) not null unique after id;	//指定在id后添加name字段
		alter table test1 add name varchar(32) not null unique first;		//在表的开头添加name字段
	
	2.修改表字段:
		alter table test1 modify 字段名 字段类型 约束条件……;
		
		alter table test1 change 原字段名 新字段名 字段类型 约束条件……;
	
	3.删除表字段:
		alter table test1 drop 字段名;
	
	4.表的重命名:
		alter table test1 rename test2;
		
	5.删除多个表的操作:
		drop table 表名1,表名2,表名3……;
【数据库】MySQL数据库(三)
标签:结构 主键索引 base char 了解 names null rem 方法