找回密码
 立即注册
  • 便民服务
  • 关注我们
  • 社区新手
本帖最后由 武汉18期-黄美铃 于 2022-8-10 22:11 编辑

索引:
是一种数据结构
可以利用索引快速访问数据库中的特定信息

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

唯一索引(对应字段的值必须是唯一的,但允许有空值)
create  unique  index   aa  on  emp(sid)==》创建唯一索引

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

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


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

DDL:数据库定义语言(对表和表结构进行操作)
建:create
查:alter   table
删:drop

DML:数据库操作语言(表数据操作)
增:insert  into
删:delate
改:update
查:select

数据库约束
not  null==》非空约束
primary   key==》主键约束
auto_increment==》自增长约束
default==》默认值约束,前四种都是对表结构的约束
foregin  key==》外键约束,对表与表之间的约束

show create table  duo;==》查看建表语句
create table duo(id  int(5)primary  key,name  VARBINARY(10)) engine=innodb;==》创建duo表,修改类型为innodb


create  table duo1(sid int(5)PRIMARY key,sname varchar(10),
constraint  dada  foreign key duo2(sid) REFERENCES duo1(id))ENGINE=INNODB;
==>创建dcs2表同时创建外键约束

如果duo1和duo2存在外键约束,duo1(主表)duo2(子表)
主表中没存在的数据,不能在子表中插入

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

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

存储过程
固定格式
create   prcocedure    存储过程名称(参数名,数据类型)#创建存储过程
bengin#存储过程开始
存储过程体
end#存储过程结束

call  存储过程名称()#调用存储过程

带参数查询
drop procedure if exists ling8;#如果ling8这个存储过程存在删除
create  procedure  ling8(n int)#创建存储过程
begin#存储过程开始
#sql 集合
drop table  if exists  bb;#如果bb存在删除
create  table bb(id int(5)primary key auto_increment,score  int(5));
insert into bb  values (1,22),(2,66);
insert into bb  values (3,33),(4,77);
insert into bb  values (5,44),(6,88);
insert into bb  values (7,55),(8,99);
select*from  bb;
select*from  bb where id=n;
end#存储过程结束

call   ling8(6)#调用存储过程

if条件判断语句
单分支
if  条件   then
    sql语句1
else
    sql语句2
end   if;

多分支判断
drop procedure if exists ling8;#如果ling8这个存储过程存在删除
create  procedure  ling8(n int)#创建存储过程,n是变量 int是数则据类型

begin#存储过程开始
#sql 集合
drop table  if exists  bb;#如果bb存在删除
create  table  bb(id int(5)primary key auto_increment,score  int(5));
insert  into  bb  values (1,22),(2,66);
insert  into  bb  values (3,33),(4,77);
insert  into  bb  values (5,44),(6,88);
insert  into  bb  values (7,55),(8,99);
#select*from  bb;
#select*from  bb where id=n;#带参数查询
#单分支if判断语句

if  n=0 then
  select count(*) from  bb;
else  
  select*from  bb order by score desc;  
end if;
end#存储过程结束
call   ling8(6)#调用存储过程

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 ling8;#如果ling8这个存储过程存在删除
create  procedure  ling8(n int)#创建存储过程,n是变量  int是数据类型
begin#存储过程开始
#sql 集合
drop table  if exists  bb;#如果bb存在删除
create  table  bb(id int(5)primary key auto_increment,score  int(5));
insert into bb  values (1,22),(2,66);
insert into bb  values (3,33),(4,77);
insert into bb  values (5,44),(6,88);
insert into bb  values (7,55),(8,99);
#select*from  bb;
#select*from  bb where id=n;#带参数查询
#单分支if判断句
if n=0 then
  select count(*) from bb;
else
  select * from aa order by score desc;
end if;
#if多分支判断句
if  n=0 then #条件1
  select count(*) from  bb;
else  if  n>0  and  n<=8  then#条件2
  select*from  bb order by score desc;
else  if  n>8 then #条件3
  select  max(score) from  bb;
else #其他情况
  select*from  bb;
end if;
end if;
end if;
end#存储过程结束

call   ling8(6)#调用存储过程

while循环语句
whlie语句格式
whlie  条件  do
执行循环体(sql)
end  while;

注意:
什么时候进入循环:当条件成立时,进入循环
什么时候退出循环:当条件不成立时,退出循环

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

concat(“user”.i)



  









分享至 : QQ空间
收藏

0 个回复

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