MYSQL索引

本文详细介绍MySQL中索引的创建方法及不同类型,包括全文索引、基本索引、唯一索引和多列索引,并提供了实用的SQL语句示例。此外,还介绍了如何通过函数批量生成数据。

小白作者,只会基础,如果有大佬,可以在评论区讨论

创建索引

1,建表时创建索引

create table index_demo(
id BIGINT(20) UNSIGNED not NULL auto_increment ,
name varchar(10) DEFAULT "",
phone BIGINT(11) DEFAULT 0,
age TINYINT(4) DEFAULT 0,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP null DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key (id)
index id_name(name);		-- 创建索引
)ENGINE=INNODB default CHARSET=utf8;

2,建表后使用alter table创建索引

create table index_demo(
id BIGINT(20) UNSIGNED not NULL auto_increment ,
name varchar(10) DEFAULT "",
phone BIGINT(11) DEFAULT 0,
age TINYINT(4) DEFAULT 0,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP null DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key (id)
)ENGINE=INNODB default CHARSET=utf8;

alter table index_demo add index id_index_demo_name(name);

3,建表后使用create index创建索引

create table index_demo(
id BIGINT(20) UNSIGNED not NULL auto_increment ,
name varchar(10) DEFAULT "",
phone BIGINT(11) DEFAULT 0,
age TINYINT(4) DEFAULT 0,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP null DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key (id)
)ENGINE=INNODB default CHARSET=utf8;

create index id_index_demo_name on index_demo(name);

四种索引类型

fulltext:以词为基础的,MySQL默认的分词是所有非字母和数字的特殊符号都是分词符

create fulltext index id_index_demo_name on index_demo(name);

index:最基本的索引,它没有任何限制

create index id_index_demo_name on index_demo(name);

 unique index:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一

create unique index id_index_demo_name on index_demo(name);

 index index_name on table_name(column,column..):多列索引

create index id_index_demo_name on index_name(name,phone);

 使用以下命令可以查看表的索引

show index from table_name;    -- table_name改成你的表名

使用以下命令可以删除索引

drop index index_name on table_name   

alter table table_name drop index index_name

#index_name替换成索引的名字,table_name替换成表名

注:id_index_demo_name是索引名,前面的id表示索引,index_demo是表名,name是列名

使用函数插入数据

创建函数

DELIMITER $$		-- 创建函数
create function mock_data()		-- 函数名
RETURNS INT		--返回数字
BEGIN		-- 函数开始
	DECLARE num INT DEFAULT 1000000;
	DECLARE i INT DEFAULT 0;
	WHILE i<num Do
		INSERT INTO index_demo(name,phone,age)VALUES(concat("user",i),concat('1',floor(rand()*((9999999999-1000000000)+1000000000))),floor(rand()*100));
		SET i = i+1;
	End	WHILE;
	RETURN i;
END;

# 如果报错:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) 就先运行下面的这条语句

set GLOBAL	log_bin_trust_function_creators=1;	

# set GLOBAL	log_bin_trust_function_creators=0;	-- 使用完成后关闭

运行函数

select mock_data();

查看当前已有函数

show function status

删除函数

drop function mock_data;        -- 注意后面没有括号

删除表操作

truncate table table_name    -- 清空表中所有数据
delete from table_name where      -- 删除符合where条件的数据,可回滚,不删除表的结构
drop table table_name        -- 删除表

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值