视图的创建和修改
-- 视图创建create view view_student as select * from student;create view v_student_info(s_id,s_name,d_id,s_age,s_sex,s_height,s_date)as select id,name,deptid,age,sex,height,login_date from student-- 视图查看select * from view_student;-- 结构查询desc view_student;-- 约束查询show create view view_student;-- 修改视图alter view view_student as select id,name,age from student;-- 删除视图drop view if exists view_student;
数据库引擎
-- 查看所有数据库引擎show engines;-- 默认存储引擎show variables like 'default_storage_engine%';-- 临时修改存储引擎set default_storage_engine = MyISAM;
创建索引
-- 创建普通索引create table tb_stu_info(id int,name char(45),dept_id int,age int,height int,index(height));-- 创建唯一索引create table tb_stu_info2(id int,name char(45),dept_id int,age int,height int,unique index(height));-- 删除索引-两种方式# drop index on## alter table drop indexdrop index height on tb_stu_info;alter table tb_stu_info2 drop index height;-- 修改索引-- 创建主键索引create table `table`(`id` int(11) not null auto_increment,`title` char(25) not null,primary key(`id`));-- 创建组合索引-- 创建全文索引-- 索引是否有效验证create table studescribe (`stu_id` int(11) not null auto_increment,`name` varchar(50) not null,`email` varchar(50) not null,`phone` varchar(11) not null,`create_date` date default null,primary key(`stu_id`));insert into studescribe values('1','adin','454545@qq.com','13548789645','1968-03-26');insert into studescribe values('2','root','564554@qq.com','16545656562','1969-04-13');insert into studescribe values('3','adin','132656@qq.com','19874551213','1986-08-23');create index index_name_email on studescribe(email);create index index_name_phone on studescribe(phone);-- 使用下面查询sql# 使用了索引:主键+索引explain select * from studescribe where stu_id='1' or phone='13548789645' \G;# 使用了索引:主键+索引explain select * from studescribe where stu_id='1' or email='454545@qq.com' \G;# 删除index_name_phone索引名drop index index_name_phone on studescribe;# 没有使用了索引:索引+索引## 要都是索引或者主键加索引否则就失效explain select * from studescribe where phone='13548789645' or email='454545@qq.com' \G;explain select * from studescribe where stu_id='1' or phone='222' or email='454545@qq.com' \G;# 使用index_name_email索引explain select * from studescribe where email like '454545@qq.com%' \G;# 没有使用index_name_email索引,索引失效explain select * from studescribe where email like '%454545@qq.com' \G;# 没有使用index_name_email索引,索引失效explain select * from studescribe where email like '%454545@qq.com%' \G;
执行SQL文件命令
source filepath
查询记录条数
select count(*) from tbname;
