找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
本帖最后由 武汉18期—沈依 于 2022-8-10 20:02 编辑

索引:
什么是索引
一个索引是存储在表中的数据结构,索引在表的列名上创建,
  普通索引:
     创建索引:create index xiaoshen on emp(sid);==为emp表中的sid创建个索引xiaoshen
     create index xiaoshen1 on emp(sid,name);==>为emp表中的sid,name创建两个索引xiaoshen1
     show index from emp==》查看索引
     alter table emp drop index xiaoshen1;==>删除索引

  唯一索引
    它与前面的普通索引类似,不同就是;索引列的值必须是唯一,但允许有空值
     create unique index aa emp(sid);==>创建唯一索引
   
  主键索引:
    和主键约束相辅相成
   注;添加一个主键约束就是添加主键索引
    alter table emp drop primary key;
    show index from emp
    desc emp;

视图
1.什么是视图
视图是一个虚拟表,他不在数据库中以储存数据
2. 视图特点
select from emp;
create view dcs as (select sid,name,age from emp);==>创建视图
select * from dcs
update dcs set name="张三" where sid=1789;==》修改视图数据
update emp set name="张三" where sid=1789;==》修改基本表数据
drop view dcs;>删除视图

DDL;数据库定义语言(对表和表结构操作)
create
alter table
drop

DML;数据库操作语言(表数据操作)
insert into
delete
update
selec

数据库约束
not null--非空约束
primary key--主键约束
auto_increment--自增长约束
default--默认值约束
foreign key--外键约束 对表于表之间的约束
  1.保持数据的一致,完整性
   表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 xiaoshen FOREIGN key dcs2(sid)REFERENCES dcs1(id))ENGINE=INNODB;==》创建dcs2表同时创建外键约束

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


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

alter table dcs2 drop foreing key xiaoshen;==》删除主键约束

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


call dcs18(8)#调用存储过程
8-->n n=8

if 条件判定语句
单分支
if 条件 then
   sql 语句1
else
  sql语句2
end if;
#单分支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,23);
insert into aa values(5,76),(6,77);
insert into aa values(7,78),(8,79);
#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
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)调用存储过程

whlie 语句的格式:
while 条件do
执行循环体(do)
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,23);
insert into aa values(5,76),(6,77);
insert into aa values(7,78),(8,79);*/
#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
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 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(10)#调用存储过程

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 个回复

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