MySQL数据库中视图与索引

视图:
视图本质就是对查询的封装,定义视图,建议以v_开头
格式:create view 视图名称 as select语句;

例:创建视图,查询学生对应的成绩信息

create view v_stu_score_course as
select
stu.*,cs.courseNo,cs.name courseName,sc.score
from
students stu
inner join scores sc on stu.studentNo = sc.studentNo
inner join courses cs on cs.courseNo = sc.courseNo

查看视图:查看表会将所有的视图也列出来
show tables;

删除视图
drop view 视图名称;
例:
drop view v_stu_score_course;

使用:视图的用途就是查询
select * from v_stu_score_course;

索引:
primary key
unique
key (字段名)

查看索引:
– show index from 表名;

创建索引:
方式一:建表时创建索引

create table create_index(
id int primary key,
name varchar(10) unique,
age int,
key (age)
);

方式二:对于已经存在的表,添加索引
如果指定字段是字符串,需要指定长度,建议长度与定义字段时的长度一致
字段类型如果不是字符串,可以不填写长度部分
create index 索引名称 on 表名(字段名称(长度))
例:
create index age_index on create_index(age);
create index name_index on create_index(name(10));

删除索引:
drop index 索引名称 on 表名;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值