请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
  • 便民服务
  • 关注我们
  • 社区新手

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

[复制链接]
索引
是一种数据结构
可以利用索引快速访问数据库中的特定信息

普通索引
show index from emp; ==》查看索引
create index xiaoliu on emp(sid); ==》为emp表中的sid创建个索引xiaoliu
create index xiaoliu1 on emp(sid,name); ==>为emp表中的sid、name创建个索引xiaoliu1
alter table emp drop index xiaoliu1; ==>删除索引

唯一索引(对应字段的值必须是唯一,但允许有空值,唯一索引(对应字段的值必须唯一。它是一种特殊的唯一索引,不允许有空值。一般是在建表的时候同时创建主键索引))
create unique index aa on emp(sid); ==》创建唯一索引

主键索引:和主键约束相辅相成
注:添加一个主键约束就是添加主键索引,主键索引就是主键约束
alter table emp drop PRIMARY key;
show index from emp;
desc emp;

视图
1视图是由基本表产生的虚表
2视图的更新和删除会影响基础表
3基础表的更新和删除也会影响到视图

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 ==》外键约束 对表与表之间的约束

表1 姓名 年龄 身份证号  语文 数学 英语
表2 姓名 年龄 身份证号  语文 篮球


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 xiaoliu foreign key dcs2(sid) REFERENCES dcs1(id))ENGINE=INNODB;
==>创建dcs2表同时创建外键约束

如果dcs1和dcs2存在外键约束,dcs1(主表) dcs2(子表)
主表中步存在的数据,不能在子表中进行插入


删除数据
delete from dcs1 where id=1;
DELETE from dcs2 where sid=1;
如果要删除主表的数据,需要先删除与主表相关的子表数据;

alter table dcs2 drop FOREIGN key xiaoliu;==>删除主键约束

存储过程
固定格式
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,79),(4,95);
insert into aa values(5,85),(6,63);
insert into aa values(7,90),(8,99);
select * from aa;
select * from aa where id=n;#带参数查询
end#存储过程结束

call dcs18(8)#调用存储过程


if条件判定语句
单分支

if 条件 then
  sql语句1
else
  sql语句2
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,79),(4,95);
insert into aa values(5,85),(6,63);
insert into aa values(7,90),(8,99);
/*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;
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,79),(4,95);
insert into aa values(5,85),(6,63);
insert into aa values(7,90),(8,99);
/*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<=8 then#条件2
  select * from aa order by score desc;
else if n>8 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,79),(4,95);
insert into aa values(5,85),(6,63);
insert into aa values(7,90),(8,99);*/
/*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,79),(4,95);
insert into aa values(5,85),(6,63);
insert into aa values(7,90),(8,99);*/
/*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 个回复

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