MySQL开发技巧 - 索引
第1关:索引
编程要求
在右侧编辑器补充代码,要求如下:
① 在创建student
表结构时为stu_id
字段创建名为pk_student
的主键索引;
② 在age
字段上建立名为idx_age
的普通索引;
③ 在classes
字段上添加名为uniq_classes
的唯一索引,并以降序排列;
④ 在name
、sex
和grade
三个字段上建立名为idx_group
的组合索引。
use School;
#请在此处添加实现代码
########## Begin ##########
#1.创建名为pk_student的主键索引
create table student(
stu_id int not null,
name varchar(25) not null,
age int not null,
sex char(2) not null,
classes int not null,
grade int not null,
primary key(stu_id)
);
#2.创建名为idx_age的普通索引
create index idx_age on student(age);
#3.创建名为uniq_classes的唯一索引
create unique index uniq_classes on student(classes);
#4.创建名为idx_group的组合索引
create index idx_group on student(name,sex,grade);
########## End ##########
MySQL开发技巧 - 视图
第1关:视图
编程要求
根据提示,在右侧编辑器补充代码,根据提供的表完成视图的创建,具体要求如下:
student
表数据结构:
stu_id | name | math | chinese |
---|---|---|---|
1 | Tom | 80 | 78 |
2 | Jack | 70 | 80 |
3 | Lucy | 97 | 95 |
stu_info
表数据结构:
stu_id | classes | city |
---|---|---|
1 | 1633 | 长沙 |
2 | 1632 | 重庆 |
3 | 1633 | 成都 |
-
在
student
表上创建视图stu_view
,查询math
、chinese
字段并定义一个math+chinese
成绩之和字段,值为两者之和; -
在
student
表和stu_info
表上,创建stu_classes
视图,查询出stu_id
、姓名和班级,查询条件为两表中的stu_id
字段相同。
use School;
#请在此处添加实现代码
########## Begin ##########
-- 在student表上创建视图stu_view,查询math、chinese字段并定义一个math+chinese成绩之和字段,值为两者之和;
-- 在student表和stu_info表上,创建stu_classes视图,查询出stu_id、姓名和班级,查询条件为两表中的stu_id字段相同。
#1.创建单表视图
create view stu_view as select math,chinese,math+chinese from student;
#2.创建多表视图
CREATE VIEW stu_classes AS
SELECT s.stu_id, s.name, info.classes
FROM student s INNER JOIN stu_info info
ON s.stu_id = info.stu_id;
########## End ##########
索引(teachingdb数据库)
第1关:索引
编程要求
在右侧编辑器补充代码,要求如下:
- 为 student 表按姓名升序建立索引,索引名为 idx_sname。
use teachingdb;
/****请在此编写代码,操作完毕之后点击评测******/
/**********Begin**********/
create index idx_sname on student(sname asc);
/**********End**********/
第2关:删除索引-练习
编程要求
根据提示,在右侧编辑器补充代码,删除索引 idx_sname。
use teachingdb;
/****请在此编写代码,操作完毕之后点击评测******/
/**********Begin**********/
drop index idx_sname on student;
/**********End**********/
SQL视图(teachingdb数据库)
第1关:创建视图
编程要求
根据提示,在右侧编辑器补充代码:
- 建立计算机系的学生的视图 student_cs。 student表结构
use teachingdb;
/****请在此编写代码,操作完毕之后点击评测******/
/**********Begin**********/
create view student_cs as select sno,sname,sdept,sclass,ssex,birthday,totalcredit from student where sdept like '计算机';
/**********End**********/
第2关:创建视图-练习一
编程要求
根据提示,在右侧编辑器补充代码, 创建由学号和平均成绩两个字段的视图 v_grade_avg 成绩表score结构如下:
use teachingdb;
/****请在此编写代码,操作完毕之后点击评测******/
/**********Begin**********/
create view v_grade_avg as select sno '学号',avg(grade) '平均成绩' from score group by sno;
/**********End**********/
MySQL-索引和视图
第1关:建立索引
任务描述
任务: 第一题 在jdxx数据表根据省份sf、城市cs、区县qx和名称name建立主索引zsy 第二题 在jdxx数据表根据名称name建立普通索引namesy
use province;
#代码开始
alter table jdxx add PRIMARY key (sf,cs,qx,name);
create index namesy on jdxx(name);
#代码结束
show index in jdxx\g;
第2关:建立视图并更新视图
任务描述
本关任务: 建立视图csxx,显示jdxx数据表的cs长沙市的区县qx、名称mc。 根据视图csxx,将开福区qx的西湖街道name修改为月湖街道
use province;
#代码开始
CREATE VIEW csxx AS
SELECT qx, name
FROM jdxx
WHERE cs = '长沙市' and qx IS NOT NULL;
UPDATE csxx
SET name = '月湖街道'
WHERE qx = '开福区' and name = '西湖街道';
#代码结束
select * from csxx;
第3关:建立基于多表的视图
任务描述
本关任务:建立基于多表的视图
任务要求
根据jdxx数据表和qxyzbm数据表 建立视图csbm 视图中有长沙市的区县qx、名称name和区县编码qxbm字段
use province
#代码开始
create view csbm as
select jdxx.qx,jdxx.name,qxbm
from jdxx join qxyzbm
on jdxx.qx=qxyzbm.qx
and jdxx.cs='长沙市';
#代码结束
select * from csbm;