视图的创建和修改

  1. -- 视图创建
  2. create view view_student as select * from student;
  3. create view v_student_info(s_id,s_name,d_id,s_age,s_sex,s_height,s_date)
  4. as select id,name,deptid,age,sex,height,login_date from student
  5. -- 视图查看
  6. select * from view_student;
  7. -- 结构查询
  8. desc view_student;
  9. -- 约束查询
  10. show create view view_student;
  11. -- 修改视图
  12. alter view view_student as select id,name,age from student;
  13. -- 删除视图
  14. drop view if exists view_student;

数据库引擎

  1. -- 查看所有数据库引擎
  2. show engines;
  3. -- 默认存储引擎
  4. show variables like 'default_storage_engine%';
  5. -- 临时修改存储引擎
  6. set default_storage_engine = MyISAM;

创建索引

  1. -- 创建普通索引
  2. create table tb_stu_info(
  3. id int,
  4. name char(45),
  5. dept_id int,
  6. age int,
  7. height int,
  8. index(height)
  9. );
  10. -- 创建唯一索引
  11. create table tb_stu_info2(
  12. id int,
  13. name char(45),
  14. dept_id int,
  15. age int,
  16. height int,
  17. unique index(height)
  18. );
  19. -- 删除索引-两种方式
  20. # drop index on
  21. ## alter table drop index
  22. drop index height on tb_stu_info;
  23. alter table tb_stu_info2 drop index height;
  24. -- 修改索引
  25. -- 创建主键索引
  26. create table `table`(
  27. `id` int(11) not null auto_increment,
  28. `title` char(25) not null,
  29. primary key(`id`)
  30. );
  31. -- 创建组合索引
  32. -- 创建全文索引
  33. -- 索引是否有效验证
  34. create table studescribe (
  35. `stu_id` int(11) not null auto_increment,
  36. `name` varchar(50) not null,
  37. `email` varchar(50) not null,
  38. `phone` varchar(11) not null,
  39. `create_date` date default null,
  40. primary key(`stu_id`)
  41. );
  42. insert into studescribe values('1','adin','454545@qq.com','13548789645','1968-03-26');
  43. insert into studescribe values('2','root','564554@qq.com','16545656562','1969-04-13');
  44. insert into studescribe values('3','adin','132656@qq.com','19874551213','1986-08-23');
  45. create index index_name_email on studescribe(email);
  46. create index index_name_phone on studescribe(phone);
  47. -- 使用下面查询sql
  48. # 使用了索引:主键+索引
  49. explain select * from studescribe where stu_id='1' or phone='13548789645' \G;
  50. # 使用了索引:主键+索引
  51. explain select * from studescribe where stu_id='1' or email='454545@qq.com' \G;
  52. # 删除index_name_phone索引名
  53. drop index index_name_phone on studescribe;
  54. # 没有使用了索引:索引+索引
  55. ## 要都是索引或者主键加索引否则就失效
  56. explain select * from studescribe where phone='13548789645' or email='454545@qq.com' \G;
  57. explain select * from studescribe where stu_id='1' or phone='222' or email='454545@qq.com' \G;
  58. # 使用index_name_email索引
  59. explain select * from studescribe where email like '454545@qq.com%' \G;
  60. # 没有使用index_name_email索引,索引失效
  61. explain select * from studescribe where email like '%454545@qq.com' \G;
  62. # 没有使用index_name_email索引,索引失效
  63. explain select * from studescribe where email like '%454545@qq.com%' \G;

执行SQL文件命令

  1. source filepath

查询记录条数

  1. select count(*) from tbname;