学无止境·MySQL⑦(索引和视图)

该文详细展示了在MySQL中如何进行数据库和表的创建,包括商品表和栏目表。接着,对商品表进行了字段增删和索引操作,如添加唯一性索引和普通索引,并演示了删除索引的方法。同时,文章还介绍了视图的创建,如创建stu_info视图来查询学生信息,以及如何删除视图。
摘要由CSDN通过智能技术生成

索引练习

1、建立一个utf8编码的数据库test1

create database test1 character set utf8;

在这里插入图片描述
在这里插入图片描述

2、建立商品表goods和栏目表category

按如下表结构创建表:存储引擎engine myisam 字符集charset utf8

	mysql> desc goods;
	+------------+-------------+------+-----+---------+----------------+
	| Field      | Type        | Null | Key | Default | Extra          |
	+------------+-------------+------+-----+---------+----------------+
	| goods_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
	| goods_name | varchar(20) | NO   |     |         |                |
	| cat_id     | int(11)     | NO   |     | 0       |                |
	| brand_id   | int(11)     | NO   |     | 0       |                |
	| goods_sn   | char(12)    | NO   |     |         |                |
	| shop_price | float(6,2)  | NO   |     | 0.00    |                |
	| goods_desc | text        | YES  |     | NULL    |                |
	+------------+-------------+------+-----+---------+----------------+
	7 rows in set (0.00 sec)

	
	mysql> desc category;
	+-----------+-------------+------+-----+---------+----------------+
	| Field     | Type        | Null | Key | Default | Extra          |
	+-----------+-------------+------+-----+---------+----------------+
	| cat_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
	| cate_name | varchar(20) | NO   |     |         |                |
	| parent_id | int(11)     | NO   |     | 0       |                |
	+-----------+-------------+------+-----+---------+----------------+

create table goods( goods_id int(11) primary key, goods_name varchar(20), cat_id int(11) default 0, brand_id int(11) default 0, goods_sn char(12), shop_price float(6,2) default 0.00, goods_desc text not null) engine=myisam character set = utf8;在这里插入图片描述
在这里插入图片描述

create table category( cat_id int(11) primary key, cate_name varchar(20), parent_id
int(11) )engine=myisam character set = utf8;
在这里插入图片描述
在这里插入图片描述

3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段

alter table goods drop goods_desc;
alter table goods drop goods_sn;
alter table goods add click_count varchar(255);
在这里插入图片描述

4、在 goods_name 列上加唯一性索引(用alter table方式)

alter table goods add unique index(goods_name);
在这里插入图片描述

5、在 shop_price 列上加普通索引(用create index方式)

create index shop_price on goods(shop_price);
在这里插入图片描述

6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)

1、create

create index in_count on goods(click_count);
alter table goods drop index in_count;
在这里插入图片描述

2、alter

alter table goods add index in_count(click_count);
alter table goods drop index in_count;
在这里插入图片描述

试图练习

1、创建表

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。

create view stu_info (name,sex,course,score) as select S.Sname,S.Ssex,C.Cname,SC.Scorere from SC,Course C,Student S where SC.Sno=C.Cno and SC.Cno=S.Sno;
在这里插入图片描述

3、删除视图 stu_info。

drop view stu_info;
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值