1.备份数据
在Linux下,mysqldump -u root -p mayue>mayue.sql
2.还原数据
mysql -u root -p mayue<mayue.sql
3.update t_score_info set math=replace(math,'6','7');//将math字段的值中的数字
“6”全部替换成“7”
4.truncate t_score_info; //快速删除大量表数据,truncate后面直接接表名,比
delete删除速度要快
5.select abs(chinese-math) as "负数" from t_score_info where name='yingge';
绝对值
6.select floor(rand()*10); //向下取整,十以内的随机数【0,9】,多少以内就*多
少
select ceiling(rand()*10); //向上取整【1,10】
7.select cast(CONCAT('nihao',',','boy!')as char);拼接字符串
8.select curdate(); //查出当前数据库服务器的日期
9.select now(); //查出当前数据库服务器的日期(时分秒)
10.select curtime /查出当前数据库服务器的时间(只有时分秒)
11.存储过程 求1-n之和
create procedure p1(in n int)
begin
declare total int default 0;
declare num int default 0;
while num < n do
set num:=num+1;
set total:=total+num;
end while;
select total;
end$
call p1(10)$ //调用proc,注意存储过程名字后面的()不能少;
12.show procedure status where db='biao'; //显示数据库"biao"下面的存储过程
13.show create procedure proc_sum; //详细显示存储过程的语句
14.create procedure hi() select 'hello'; //创建procedure ,hi()后面不能少括
号
15.call hi();或者call hi; //调用一个存储过程 hi(),建议带上();
16.drop procedure proc_ji; //删除名字叫“proc_ji”的存储过程
17.创建一个简单的存储过程:myproc_multitable;
drop procedure if exists myproc_multitable;
delimiter $
create Procedure myproc_multitable()
BEGIN
drop table if exists S;
drop table if exists SC;
drop table if exists C;
create table S(Sid int(1),SNAME char(20),AGE int(1),SEX int(1));
create table SC(Sid int(1),Cid char(5),GRADE char(20)) ;
create table C(Cid char(5),CNAME char(20),TEACHER char(20));
insert into S(Sid,SNAME,AGE,SEX)values(1111,'David',28,1);
insert into S(Sid,SNAME,AGE,SEX)values(1112,'Dany',23,0);
insert into S(Sid,SNAME,AGE,SEX)values(1113,'TOM',38,1);
insert into SC(Sid,Cid,GRADE)values(1113,'C1','38');
insert into SC(Sid,Cid,GRADE)values(1113,'C2','80');
insert into SC(Sid, Cid,GRADE)values(1113,'C3','67');
insert into SC(Sid,Cid,GRADE)values(1111,'C4','88');
insert into C(Cid,CNAME,TEACHER)values('C4','Maths','zhangsan');
insert into C(Cid,CNAME,TEACHER)values('C3','English','lisi');
insert into C(Cid,CNAME,TEACHER)values('C2','Chinese','wangwu');
insert into C(Cid,CNAME,TEACHER)value('C1','Computer','zhaoliu');
select * from S;
select * from SC;
select * from C;
end $
call myproc_multitable()
18.如何对一个表中的一次性插入2000条数据,存储过程怎么写?
drop procedure if exists addrecord;
delimiter $
create procedure addrecord(in N int)
begin??
declare num int default 1;
while num < N do
set num:=num+1;
insert into sc(cid,grade)values('c3','66');
end while;
end ;
call addrecord(2000);
19.索引的类型:3种:普通索引(index),唯一索引(unique) 和主键索引(primary
key)
20.不能用CREATE INDEX语句创建PRIMARY KEY;
一个表只能有一个主键,但是可以创建多个主键索引;
若先有主键约束,则无法对任何字段再创建主键索引;倘若建表时未创建主键约束,则
可以一次创建多个字段的“联合索引”(避免联合索引中多字段值完全相同),同时发
现,无法再修改主键约束;可以直接清除主键索引(eg:alter table test04 drop
primary key;),不能转移主键约束---5.1的mysql;
21.create index S_sid_age_index on S(sid,age);创建多个索引
22.alter创索引
alter table table_name add index index_name(column_list) ;
alter table table_name add unique(column_list) ;
alter table table_name add primary key(column_list);
23.删除索引可以使用ALTER TABLE语句来实现。
其格式如下:
alter table table_name drop index index_name ;
alter table table_name drop primary key ;
24.
create view V_duoceshi12 as (select id,name from duoceshi_12); //将查询结
果集创建为视图V_duoceshi12
25.Show tables; //也能查看到视图
26.对视图的操作与对基本表的操作都是一样的(包括alter,create,insert into
,update ,delete ,select),且二者任意一方的字段值被修改,都会实时影响到对方(
如修改view的字段值,会同步修改table相应的字段值);但是视图的创建是基于基本表
的,它的作用可以对基本表的敏感信息进行保护;在实际工作中,出于安全考虑,将用
户常用的信息创建成视图给用户调用,避免了直接操作基本表!
27.存储过程练习
1.
delimiter %%
create procedure d12_proc_parameter(n int)
begin
select * from 12_duoceshi where sid <=n;
end %%
call d12_proc_parameter(12005);
-----------------------------------------------------------------------------
---------
2.
delimiter %%
create procedure d12_proc_parameter_if(n int)
begin
if(n<>0) then
select * from 12_duoceshi where sid <=n;
else
select * from 12_duoceshi;
end if;
end %%
call d12_proc_parameter_if(12001);
-----------------------------------------------------------------------------
---------
3.
drop procedure if exists d13_proc_parameter_if;
delimiter %%
create procedure d13_proc_parameter_if(n int)
begin
if n<>0 then
select * from 12_duoceshi where sid <=n;
else
select count(*) from 12_duoceshi;
end if;
end %%
call d13_proc_parameter_if(0);
4.
aa_cyg(id,name,score)
cc_cyg(s_id,score)
update aa_cyg a inner join cc_cyg c on a.id=c.s_id set a.score=c.score;
//将2表中相等id中cc_cyg对应的score值赋值给aa_cyg的score
5.
desc aa_cyg;
alter table aa_cyg change id id int(1) auto_increment;
insert into aa_cyg(score) select score from cc_cyg ; //将第2张表中一个字段
值插入到第1张表的一个字段(前提:id为自增长)
6.将cc表的前5条score的值更新到aa表中的前面5条;(关联的id值要相同才会赋值成功
)
update aa_cyg a left join cc_cyg c on a.id=c.s_id set a.score=c.score;
7.存储过程练习(if语句嵌套)
若n<0,则显示所有表数据(不修改);
若n=0,则初始化数据要求如下:
20岁以下 , score =70
20-40岁 , score =80
40以上 , score =90
若0<n<100,则修改20岁以下score=score+1;
若100<=n<200,20岁-40岁的score=score+1;
若200<=n,40岁以上的score=score+1;
将上面每一个条件下所有的年级、分数全部显示:
drop PROCEDURE if exists proc_parameter_stat;
create procedure proc_parameter_stat(n int)
BEGIN
if (n<0) then
select age,score from 12_duoceshi order by age asc;
else if (n=0) then
update 12_duoceshi set score=70 where age<20;
update 12_duoceshi set score=80 where age>=20 and
age<=40;
update 12_duoceshi set score=90 where age>40;
select age,score from 12_duoceshi order by age asc;
else if (n>0&&n<100) then
update 12_duoceshi set score=score+1 where
age<20;
select age,score from 12_duoceshi order by
age asc;
else if (n>=100&&n<200) then
update 12_duoceshi set score=score+1
where age>=20 and age<=40;
select age,score from 12_duoceshi
order by age asc;
else
update 12_duoceshi set
score=score+1 where age>40;
select age,score from
12_duoceshi order by age asc;
end if;
end if;
end if;
end if;
end
8.练习while循环
drop procedure if exists sum_ploc;
create procedure sum_ploc(n int)
BEGIN
declare i int default 0;
#declare sum int default 0;
#truncate 12_duoceshi;
alter table 12_duoceshi change sid sid int(1) auto_increment;
while (i<n) DO
set i:=i+1;
#set sum:=sum+i;
insert into 12_duoceshi
(name1,score) values('zhangsan',22);
end while;
select count(*) from 12_duoceshi;
end
call sum_ploc(100);
|
|