这是有声音的视频,请检查播放器或者声音输出设备。
这次的学习内容:索引index(快速查询) 与 视图view(安全,方便查找)
===================================================
create database xue_xiao character set utf8 collate utf8_general_ci;
use xue_xiao;
create table ban_ji(
id int(20) not null primary key,
ban_ming varchar(15) not null
);
insert into ban_ji values(1,'一年级(1)班'),(2,'一年级(2)班'),(3,'一年级(3)班');
create table xue_sheng(
id int(20) auto_increment not null primary key,
xing_ming varchar(50) not null,
yu_wen int,
shu_xue int,
bj_id int not null,
CONSTRAINT fk_xue_sheng FOREIGN KEY(bj_id) REFERENCES ban_ji(id)
);
insert into xue_sheng(xing_ming,yu_wen,shu_xue,bj_id) values
('张三',90,70,1),('李四',80,85,2),('李小红',80,75,1),('陈小明',75,80,3);
================================================
视图它是一个逻辑表,并不存储在物理硬盘上 , 方便查询
考虑一个例子,要查找学生姓名和他们的总分
select xing_ming, yu_wen+shu_xue from xue_sheng;
创建视图
create view v_xue_sheng as select xing_ming ,yu_wen+shu_xue from xue_sheng;
访问视图的方法和访问表类似 , 但是不能插入,删除,更新数据
select * from v_xue_sheng;
修改视图
alter view v_xue_sheng as select xing_ming as 姓名,yu_wen+shu_xue as 总分 from xue_sheng;
select * from v_xue_sheng;
+-----------+--------+
| 姓名 | 总分 |
+-----------+--------+
| 张三 | 160 |
| 李四 | 165 |
| 李小红 | 155 |
| 陈小明 | 155 |
+-----------+--------+
4 rows in set (0.00 sec)
删除视图
drop view v_xue_sheng;
--------------------------------------------
再看这样的一个例子, 我们要查找2张表的数据
select xing_ming,ban_ming from xue_sheng x,ban_ji b where x.bj_id=b.id;
建立一个视图方便以后查询
create view v_xue_sheng as
select xing_ming,ban_ming from xue_sheng x,ban_ji b where x.bj_id=b.id;
select * from v_xue_sheng;
+-----------+-----------------+
| xing_ming | ban_ming |
+-----------+-----------------+
| 张三 | 一年级(1)班 |
| 李四 | 一年级(2)班 |
| 李小红 | 一年级(1)班 |
| 陈小明 | 一年级(3)班 |
+-----------+-----------------+
查看当前数据库的表和视图
show tables;
=======================================================
索引 index - 用来快速查找具有特定值的记录,如果没有索引,执行查询时候必须从第一条记录开始
扫描整个表的记录,直到符合要求的记录。
如果有了索引mysql无需扫描任何记录即可顺序找到目标记录的位置。
简单说来, 索引就是提高查找数据速度,数据量越多,效果越明显。
创建索引
create index idx_xing_ming on xue_sheng(xing_ming);
创建唯一索引(主键是一种唯一索引)
create unique index idx_xing_ming on xue_sheng(xing_ming);
删除索引
drop index idx_xing_ming on xue_sheng;
另外的一种创建和删除方式
alter table xue_sheng add index idx_xing_ming(xing_ming);
alter table xue_sheng add unique idx_xing_ming(xing_ming);
alter table xue_sheng drop index idx_xing_ming;
视频就到这里结束。88
01月 13th, 2012 at 05:18:43 #Kameryn
A prvocoatvie insight! Just what we need!
[回复]
01月 13th, 2012 at 19:34:50 #umaynn
7rxjIW iuzsphmibohe
[回复]
01月 15th, 2012 at 19:40:18 #qhjboygy
nNXV0Z hromjnlupafu
[回复]