找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

索引,视图,约束,存储过程

[复制链接]
本帖最后由 武汉18期-徐韬 于 2022-8-10 21:57 编辑

1.什么是索引
一个索引是存储在表中的数据结构,索引在表的列名上创建。索引中包含了一个列的值,这些值保存在一个数据结构中。数据库使用索引的方式与使用说明书的目录很相似:通过搜索索引找到特定的值,然后随指针到达包含该值的行。
重点:索引是一种数据结构
普通索引:最基本的索引,它没有任何限制
创建索引: create index tt on dcs(id);  ==》给id创建索引
show index from emp;  ==》查看索引
create index xiaoxu on emp(sid);  ==》为emp表中的sid创建个索引xiaoxu
create index xiaoxu1 on emp(sid,name);  ==》为emp表中的sid、name创建个索引xiaoxu1
alter table emp drop index xiaoxu1;  ==》删除索引xiaoxu1

唯一索引:它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许为空
create unique index aa on emp(sid);  ==》创建唯一索引

主键索引:它是一种特殊的唯一索引,不允许有空值。一般是建表的时候同时创建主键索引。
(和主键约束是相辅相成的)注:添加一个主键约束就是添加主键索引,主键索引就是主键约束
alter table emp drop primary key;   ==》删除主键约束
show index from emp;
desc emp;
alter table emp add primary key(sid);  ==》给id字段创建主键索引

视图
视图是一个虚拟的表,它不会在数据库中以存储数据的形式保存,
1.视图是有基本表产生的虚表
2.视图的更新和删除会影响基础表
3.基础表的更新和删除也会影响到视图

show tables;
select sid,name,age from emp;
create  view dcs as(select sid,name,age from emp);   ==》创建视图
select * from dcs;  
update dcs set name="zhangsan" where sid=1789;  ==》修改视图数据
update emp set name="张三" where sid=1789;  ==》修改基本表数据
drop view dcs;  ==》删除视图

DDL:数据定义语言(对表和表结构操作)
create
alter table
drop
DML:数据操作语言(表数据修改)
insert into
delete
update
select
数据库约束
not null  ==》非空约束
primary key  ==》主键约束
auto_increment  ==》自增长约束
default  ==》默认值约束  前四种都是对表结构的约束
foreigin key  ==》外键约束  对表与表之间的约束
SHOW CREATE TABLE dcs1;  ==》查询建表语句
CREATE TABLE dcs1(id INT(5)PRIMARY KEY ,NAME VARCHAR(10))ENGINE=INNODB;  ==》创建dcs1表,修改类型为INNODB
CREATE TABLE dcs2(sid INT(5)PRIMARY KEY,sname VARCHAR(10),CONSTRAINT xiaoxu FOREIGN KEY dcs2(sid) REFERENCES dcs1(id))ENGINE=INNODB;

如果dcs1和dcs2存在外键约束,dcs1(主表) dcs2(子表)
主表中不存在的数据,不能在子表中进行插入
SELECT * FROM dcs1;
SELECT * FROM dcs2;
INSERT into dcs1 VALUES(1,"zhangsan"),(2,"xiaoxu");
INSERT into dcs2 VALUES(1,"lisi"),(3,"wangwu");
删除数据
delete from dcs2 where sid=1;
delete from dcs1 where id=1;
如果要删除主表的数据,需要先删除与主表相关的子表数据
alter table dcs2 drop foreign key xiaoxu;

存储过程
固定格式
create procedure存储过程名称(参数名 数据类型) ==》创建存储过程
begin  ==》存储过程开始
存储过程体
end  ==》存储过程结束
call 存储过程名称()  ==》调用存储过程
drop procedure if exists dcs18; #如果dcs18这个存储过程存在则删除
create procedure dcs18(n int) #创建存储过程,n是变量,int是数据类型
begin #存储过程开始
#sql集合
drop table if exists aa; #如果aa存在删除
create table aa(id int(5)primary key auto_increment,score int(5));
insert into aa VALUES(1,88),(2,75);
insert into aa VALUES(3,68),(4,35);
insert into aa VALUES(5,90),(6,15);
insert into aa VALUES(7,75),(8,46);
SELECT * FROM aa;
SELECT * FROM aa WHERE id=n;
end #存储过程结束
call dcs18(5) #调用存储过程

#单分支if判断语句
if n=0 then
select count(*) from aa;
else
select * from aa order by score desc;
end if;
end#存储过程结束
call dcs18(0)#调用存储过程
if条件判断语句多分支
if 条件1 then
sql语句1
else if 条件2 then
sql语句2
else if 条件3 then
sql语句3
else
sql语句4
end if;
end if;
end if;

多分支判断
drop procedure if exists dcs18; #如果dcs18这个存储过程存在则删除
CREATE procedure dcs18(n int) #创建存储过程,n是变量,int是数据类型
begin #存储过程开始
#sql集合
drop table if exists aa; #如果aa存在删除
create table aa(id int(5)primary key auto_increment,score int(5));
insert into aa VALUES(1,88),(2,75);
insert into aa VALUES(3,68),(4,35);
insert into aa VALUES(5,90),(6,15);
insert into aa VALUES(7,75),(8,46);
/*SELECT * FROM aa;
#SELECT * FROM aa WHERE id=n;
#单分支if判断语句
if n=0 then
SELECT COUNT() FROM aa;
ELSE
SELECT * FROM aa ORDER BY score desc;
END IF; */
#if多分支判断
IF n=0 THEN #条件1
SELECT COUNT() FROM aa;
ELSE IF n>0 AND n<=i THEN #条件2
SELECT * FROM aa ORDER BY score desc;
ELSE IF n>i THEN #条件3
SELECT MAX(score) FROM aa;
ELSE #其他情况
SELECT * FROM aa;
END IF;
END IF;
END IF;
END #存储过程结束
call dcs18(-1) #调用存储过程

while 语句的格式:
while 条件 do
执行循环体(sql)
end while;
注意:
什么时候进入循环:当条件成立时,进入循环
什么时候退出循环:当条件不成立时,退出循环
drop procedure if exists dcs18;#如果dcs18这个存储过程存在则删除
create procedure dcs18(n int)#创建存储过程,n是变量 int是数据类型
begin#存储过程开始
#sql集合
declare i int(5)default(select count() from aa);#declare声明变量i 默认值为表的行数/drop table if exists aa;#如果aa存在删除create table aa(id int(5)primary key auto_increment,score int(5));
insert into aa VALUES(1,88),(2,75);
insert into aa VALUES(3,68),(4,35);
insert into aa VALUES(5,90),(6,15);
insert into aa VALUES(7,75),(8,46);
/* select * from aa;
select * from aa where id=n;#带参数查询
#单分支if判断语句
if n=0 then
select count() from aa;
else
select * from aa order by score desc;
end if;
#if多分支判断
if n=0 then #条件1
select count(*) from aa;
else if n>0 and n<=i then #条件2
select * from aa order by score desc;
else if n>i then #条件3
select max(score) from aa;
else #其他情况
select * from aa;
end if;
end if;
end if; */
#while 循环
if n=0 then
select count() from aa;
else if n>0 and n<=i then
select * from aa order by score desc;
else
while n>i do
insert into aa(score)values(88);
set i=i+1;
end while;
end if;
end if;
end#存储过程结束

call dcs18(200)#调用存储过程
drop procedure if exists dcs18;#如果dcs18这个存储过程存在则删除
create procedure dcs18(n int)#创建存储过程,n是变量 int是数据类型
begin#存储过程开始
#sql集合
declare i int(5)default(select count() from aa);#declare声明变量i 默认值为表的行数
declare max int(5)default(select max(score) from aa);#最高分数变量
/* drop table if exists aa;#如果aa存在删除
create table aa(id int(5)primary key auto_increment,score int(5));
insert into aa VALUES(1,88),(2,75);
insert into aa VALUES(3,68),(4,35);
insert into aa VALUES(5,90),(6,15);
insert into aa VALUES(7,75),(8,46); */
/* select * from aa;select * from aa where id=n;#带参数查询
#单分支if判断语句
if n=0 then
select count() from aa;
else
select * from aa order by score desc;
end if;
#if多分支判断
if n=0 then#条件1
select count() from aa;
else if n>0 and n<=i then#条件2
select * from aa order by score desc;
else if n>i then#条件3
select max(score) from aa;
else#其他情况
select * from aa;
end if;
end if;
end if; */
#while 循环
if n=0 then
select count()from aa;
else if n>0 and n<=i then
select * from aa order by score desc;
else
while n>i do
set max=max+1;
insert into aa(score)values(max);
set i=i+1;
end while;
end if;
end if;
end #存储过程结束

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册