索引

MySQL的索引包括普通索引、唯一性索引(unique index)、全文索引(fulltext index)、单列索引、多列索引和空间索引等。

1.索引的创建

·创建表的时候创建索引

SQL语法:index|key [索引名] (属性名[(长度)] [asc|desc])

create table newTable(
id int not null primary key,
name varchar(20),
age int,
index name_index(name(10))
);

·在已经存在的表上创建索引

SQL语法:create index 索引名 on 表名(属性名[(长度)] [asc|desc]);

create index age_index on newTable(age(10));

·使用alter table来创建索引

SQL语法:alter table table_name add index|key 索引名[(长度)] [asc|desc]);

alter table newTable add index name_index(name(5) desc);

2.查看索引

SQL语法:

show index from table_name[from db_name];

show index from mydb.mytable;

show index from newTable;#查看索引信息

3.删除索引

SQL语法:

drop index index_name on table_name;

alter table table_name drop index index_name;

alter table table_name drop primary key;

drop index name_index on newTable;
alter table newTable drop primary key;

视图

视图是一个虚拟表,其内容由查询定义。

1.视图的创建

SQL语法:create view 视图名[(视图列表)] as 查询语句;

create view student_view2(name,cname,grade)
as select sname,cname,grade
from student s,course c,sc
where s.sno=sc.sno and c.cno=sc.cno;

2.查看视图

describe语句:describe 视图名称;

show table status语句:show table status like '视图名';

show create view语句:show create view '视图名';

查询db1数据库下的student表:

select * from db1 a.view where student='student_view'\G

3.修改视图

create or replace view 视图名[{属性清单}] as select 语句;

create or replace view
student_view(姓名,选修课,成绩)
as select sname,cname,grade
from student s,course c,sc
where s.sno=sc.sno and c.cno=sc.cno;

4.删除视图

SQL语法:drop view [if exists] view_name[,view_name2];

drop view if exists student_view;

5.更新视图数据

update student_view set sno='001',sname='张三’,ssex='男';

MySQL数据库——索引与视图的更多相关文章

  1. MySQL数据库索引的4大类型以及相关的索引创建

    以下的文章主要介绍的是MySQL数据库索引类型,其中包括普通索引,唯一索引,主键索引与主键索引,以及对这些索引的实际应用或是创建有一个详细介绍,以下就是文章的主要内容描述. (1)普通索引 这是最基本 ...

  2. (转)MySql数据库索引原理(总结性)

    本文引用文章如链接: http://www.codinglabs.org/html/theory-of-mysql-index.html#more-100 参考书籍:Mysql技术内幕 本文主要是阐述 ...

  3. 知识点:Mysql 数据库索引优化实战(4)

    知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) 一:插入订单 业务逻辑:插 ...

  4. 为什么MySQL数据库索引选择使用B+树?

    在进一步分析为什么MySQL数据库索引选择使用B+树之前,我相信很多小伙伴对数据结构中的树还是有些许模糊的,因此我们由浅入深一步步探讨树的演进过程,在一步步引出B树以及为什么MySQL数据库索引选择使 ...

  5. MySQL数据库索引之B+树

    一.B+树是什么 B+ 树是一种树型数据结构,通常用于数据库和操作系统的文件系统中.B+ 树的特点是能够保持数据稳定有序,其插入与修改操作拥有较稳定的对数时间复杂度.B+ 树元素自底向上插入,这与二叉 ...

  6. 第二百八十八节,MySQL数据库-索引、limit分页、执行计划、慢日志查询

    MySQL数据库-索引.limit分页.执行计划.慢日志查询 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获 ...

  7. MYSQL数据库索引类型及使用

    MYSQL数据库索引类型包括普通索引,唯一索引,主键索引与组合索引,这里对这些索引的做一些简单描述: (1)普通索引 这是最基本的MySQL数据库索引,它没有任何限制.它有以下几种创建方式: 创建索引 ...

  8. MySQL数据库索引的底层原理(二叉树、平衡二叉树、B-Tree、B+Tree)

    1.MySQL数据库索引的底层原理 https://mp.weixin.qq.com/s/zA9KvCkkte2mTWTcDv7hUg

  9. MySQL数据库索引常见问题

    笔者看过很多数据库相关方面的面试题,但大多数答案都不太准确,因此决定在自己blog进行一个总结. Q1:数据库有哪些索引?优缺点是什么? 1.B树索引:大多数数据库采用的索引(innoDB采用的是b+ ...

随机推荐

  1. React.js初探

    React.js 菜鸟官方解释: React 是一个用于构建用户界面的 JAVASCRIPT 库. React主要用于构建UI,很多人认为 React 是 MVC 中的 V(视图). React 起源 ...

  2. chapter6 数据结构基础之习题 Parentheses Balance

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  3. xcode制作越狱后ipa安装文件

    正常情况下发布测试版给用户需要问到对方设备ID并添加到开发者证书里去感觉有点麻烦,如果是已越狱过的机器可以使用xcode制作ipa文件,并直接用itunes同步进去,这样方便多了. 将运行目标选为iO ...

  4. Ajax 传递json字符串到客户端时报 Internal server error

    架构:struts2+JQuery 需求:就是前台请求后台,后台查询数据库,将数据转换成json格式,使用struts2框架赋值给action内的变量jsonStr,前台通过 response.jso ...

  5. [跨域]js设置document.domain实现跨域

    document.domain用来得到当前网页的域名.比如在地址栏里输入: 代码如下: javascript:alert(document.domain); //www.jb51.net 我们也可以给 ...

  6. P1146 硬币翻转

    题目描述 在桌面上有一排硬币,共N枚,每一枚硬币均为正面朝上.现在要把所有的硬币翻转成反面朝上,规则是每次可翻转任意N-1枚硬币(正面向上的被翻转为反面向上,反之亦然).求一个最短的操作序列(将每次翻 ...

  7. Django中关于MySQL的bug总结

    bug one: You are trying to add a non-nullable field 'height' to person without a default; we can't d ...

  8. 【sqli-labs】 less34 POST- Bypass AddSlashes (POST型绕过addslashes() 函数的宽字节注入)

    还是宽字节注入,POST版本的 uname=1&passwd=1%df' union select 1,2,3# 提交报错 列名不匹配,改一下就好了 uname=1&passwd=1% ...

  9. 备份xx

    https://www.tuicool.com/articles/V3EBzev https://www.tuicool.com/topics/11080087?st=0&lang=1& ...

  10. java mongodb 使用MongoCollection,BasicDBObject 条件查询

    废话不说,上代码 //链接数据库 MongoClient mongoClient = new MongoClient( "172.26.xxx.xxx" , 27017 ); Mo ...