MYSQL實用教程(第二版)電子工業出版社

MYSQL代碼(基本比較全)		
		數據庫建表

第一章 緒論
show variables like’char%’;//改中文字符
set character_set_database=‘gbk’;
create table xs (
學號 char(6) not null primary key,
姓名 char(8) not null,
專業名 char(10) null,
性別 tinyint(1) not null,
出生日期 date
not null,
總學分 tinyint(1) null,
照片 blob null,
備註 text null
)

create table KC (
課程號 char(3) not null,
課程名 char(16) not null,
開課學期 tinyint(1) not null default 1,
學時 tinyint(1) not null,
學分 tinyint(1) null,
primary key(課程號)
)

create table XS_KC(
學號 char(6) not null,
課程號 char(3) not null,
成績 tinyint(0) null,
學分 tinyint(1) null,
primary key (學號,課程號)
)
第三章
1.delete from table_name where 學號='00000’or 學號=‘00001’;//刪除一個人的數據,and 有兩個主鍵;
2.insert into xs//插入多條數據
values(‘091102’,‘程明’,‘計算機’,1,‘1991-02-01’,50,null,null),
(‘091103’,‘王燕’,‘計算機’,0,‘1989-10-06’,50,null,null)
3.alter table table-name
add 姓名 char(8) not null after 學號,
drop column;//插入刪除一列數據
4.alter table table_name change a b integer//把列名a變爲b
5.alter table table_name modify b bigint not null//改變列的數據類型
6.alter table a rename to b //把表名a改爲b
7.rename table user1 to user2//把數據庫user1重命名爲user2
8 drop table if exists user1 //刪除表user1
9.drop database if exits database_name;//刪除數據庫
10.update xs
set 總學分=總學分+10,備註=‘拂袖’;
where 姓名=‘張三’;
11.truncate table table-name//清除指定表的所有數據
12.desc xs 學號//看列信息
13.source 路徑 文件;//文件導入
mysqldump -u dbuser -p dbname users> dbname_users.sql

14mysqldump -u root -p dbname > dbname.sql//導出整個數據庫
mysqldump -u dbuser -p dbname users> dbname_users.sql//導出一個表

第四章
1.select 學號 as number, 姓名 as name from xs where 專業名=‘計算機’;
2.select 學號,姓名,
case
when 總學分 is null then ‘尚未選課’
when 總學分 <50 then ‘不及格’
when 總學分 >=50 and 總學分 <=52 then ‘合格’
else ‘優秀’
end as 等級
from xs
where 專業名=‘計算機’;
3.select 學號,課程號,成績1.20 as 成績120
from xs_kc //計算列值
where 學號=‘081101’;
4. select distinct 專業名,總學分 from xs;//消除重複行
5.聚合函數:
a. select count(
) as '學生總數’from xs; //求學生的總人數
b. select count(備註) as ‘備註不爲空的學生數目’from xs;
c. select count(總學分) as’總學分50以上的人數’
from xs
where 總學分>50
d.select max(成績),min(成績)
from xs_kc
where 課程號=‘101’;
e.select sum(成績) as ‘課程總成績’
from xs_kc
where 學號=‘081101’;
f.select avg(成績) as ‘課程101平均成績’
from xs_kc
where 課程號 = ‘101’;
g.select variance(成績)
from xs_kc
where 課程號 = ‘101’;//方差
h.select stddev(成績)
from xs_kc
where 課程號 = ‘101’; //標準差
i.select group_concat(學號)
from xs_kc
where 課程號 = ‘206’;//求選修206課程的學生的學號
j. select bin(bit_or(bin_value)) from bits//或運算

6.多表連接
(1) 全連接
select distinct kc.課程名,xs_kc.課程號
from kc,xs_kc
where kc.課程號=xs_kc.課程號
(2)JOIN連接
a.內鏈接
select distinct 課程名,xs_kc.課程號
from kc inner join xs_kc
on (kc.課程號=xs_kc.課程號)

select 姓名,成績
from xs join xs_kc on xs.學號=xs_kc.學號
where 課程號 = ‘206’ and 成績>=80 //查找選修了206課程且成績在80分以上的學生姓名及成績

select xs.學號,姓名,成績,課程名
from xs join xs_kc on xs.學號=xs_kc.學號
join kc on xs_kc.課程號=kc.課程號
where 課程名 = ‘計算機基礎’ and 成績>=80//查選修了“計算機基礎”課程且成績在80分以上的

select a.學號,a.課程號,b.課程號,a.成績
from xs_kc as a join xs_kc as b
on a.成績=b.成績 and a.學號=b.學號 and a.課程號!=b.課程號//查找數據庫中課程不同,成績相同的學生的。。。。。

select 課程名
from kc inner join xs_kc
using (課程號); //查詢kc表中所有學生選過的課程名
b.外連接
select xs.* , 課程號 //查找所有學生情況及他們選修的課程號,未選課也要包括
from xs left outer join xs_kc on xs.學號=xs_kc.學號

select xs_kc.* ,課程名 //查找被選修了的課程的選修情況和所有開設的課程名
from xs_kc right join kc on xs_kc.課程號=kc.課程號

select 課程名,課程號 from kc
where 課程號 in
( select distinct 課程號 from kc natural right outer join xs_kc);

c.交叉連接
select 學號,姓名,課程號,課程名
from xs cross join kc;
select distinct 課程名,xs_kc.課程號
from kc straight_join xs_kc
on (kc.課程號=xs_kc.課程號)
7.select 姓名,學號,性別,總學分
from xs
where 專業名=‘計算機’ and 備註<=>null

8.select 學號,姓名,性別
from xs
where 姓名 like ‘王%’;//查詢姓王的學生的。。

select 學號,姓名,專業名
from xs
where 學號 like ‘%0_’;// 查詢學號倒數第二個數字爲0的學生的。。。

select 學號,姓名
from xs
where 學號 like ‘%#_%’ escape ‘#’;//查詢名字包含下劃線的學生的。。。

9.REGEXP運算符
select 學號,姓名,專業名
from xs
where 姓名 regexp ‘^李’;//查詢姓李的同學的。。。

select 學號,姓名,專業名
from xs
where 學號 regexp ‘[4,5,6]’;//查詢學號裏包含4,5,6的學生的。。。

select 學號,姓名,專業名
from xs
where 學號 regexp ‘^08.*08$’;//查詢學號以08開頭,以08結尾的學生的。。。。

10.select 學號,姓名,專業名,出生日期
from xs
where 出生日期 not between ‘1993-1-1’ and ‘1993-12-31’;
//查詢xs表中不在1993年出生的學生情況
11.select * from xs where 專業名 in(‘計算機’,‘通信工程’);
12.select * from xs where 總學分 is null;//查找總學分爲空的學生
13.IN查詢
select 姓名,學號
from xs
where 學號 in
( select 學號
from xs_kc
where 課程號 = ‘206’
);//查找選修了課程號爲206的課程的學生的。。。

select 姓名,學號,專業名
from xs
where 學號 not in
( select 學號
from xs_kc
where 課程號 in
(
select 課程號
from kc
where 課程名=‘離散數學’
)
);//查找未選修離散數學的學生的。。。。
14.比較子查詢
select 學號
from xs_kc
where 課程號=
(
select 課程號
from kc
where 課程名=‘離散數學’
);//查詢選修了離散數學的學生學號

select 學號,姓名,專業名,出生日期
from xs
where 出生日期<all
(
select 出生日期
from xs
where 專業名=‘計算機’
)//查詢xs表中比所有計算機系的學生年齡都大的學生。。。

select 學號
from xs_kc
where 課程號=‘206’ and 成績>=any
(
select 成績
from xs_kc
where 課程號=‘101’
);//查找課程號206的成績不低於課程號101的最低成績的學生的。。。

15.Exists子查詢
select 姓名
from xs
where exists
(
select *
from xs_kc
where 學號=xs.學號 and 課程號=‘206’
);//查找選修206號課程的學生姓名

select 姓名
from xs
where not exists
(
select *
from kc
where not exists
(
select *
from xs_kc
where 學號=xs.學號 and 課程號=kc.課程號
)
);//查詢選修了全部課程的同學的姓名

select 姓名,學號,總學分
from(
select 姓名,學號,性別,總學分
from xs
where 總學分>50
)as student
where 性別=‘1’;//查找總學分大於50分的男同學的。。。

select 學號,姓名,year(出生日期)-year (
( select 出生日期
from xs
where 學號=‘081101’
))as 年齡差距
from xs
where 性別=‘0’;//查詢所有女學生de學號姓名以及與081101號學生的年齡差距

select 學號,姓名
from xs
where(性別,總學分)=(select 性別,總學分
from xs
where 學號=‘081101’);//查找與081101號學生性別相同,總學分相同的學生的。。。
16.GROUP BY:
select 專業名
from xs
group by 專業名;//各專業名輸出

select 專業名,count(*) as ‘學生數’
from xs
group by 專業名;//各專業的學生數

select 課程號,avg(成績) as’平均成績’,count(學號) as’選修人數’
from xs_kc
group by 課程號;//被選修的各門課程的平均成績和選修該課程的人數

select 專業名,性別,count(*) as ‘人數’
from xs
group by 專業名,性別
with rollup;//每個專業的男生人數、女生人數、總人數,以及學生總人數
select 課程名,專業名,avg(成績) as ‘平均成績’
from xs_kc,xs,kc
where xs_kc.課程號=kc.課程號 and xs_kc.學號=xs.學號
group by 課程名,專業名
with rollup;//每門課程各專業的平均成績,每門課程的總平均成績和所有課程的總平均成績

17.HAVING子句
select 學號,avg(成績) as’平均成績’
from xs_kc
group by 學號
having avg(成績)>=85;//平均成績在85分以上的學生的。。。

select 學號
from xs_kc
where 成績 >=80
group by 學號
having count(*)>2;//查找選修課程超過2門且成績都在80分以上的學生。。

select 學號,avg(成績) as’平均成績’
from xs_kc
where 學號 in
(
select 學號
from xs
where 專業名=‘通信工程’
)
group by 學號
having avg(成績)>=85;//查找通信工程專業平均成績在85分以上的學生的。。
18.ORDER BY子句
select 學號,姓名,專業名,出生日期
from xs
where 專業名=‘通信工程’
order by 出生日期;//按出生日期先後排序

select 姓名,課程名,成績
from xs,kc,xs_kc
where xs.學號=xs_kc.學號
and xs_kc.課程號=kc.課程號
and 課程名=‘計算機基礎’
and 專業名=‘計算機’
order by 成績 desc;//把計算機專業學生的“計算機基礎”課程成績按降序排列

select 學號,姓名,專業名
from xs
where 專業名=‘計算機’
order by(
select avg(成績)
from xs_kc
group by xs_kc.學號
having xs.學號=xs_kc.學號
);//按平均成績排序

19.LIMIT子句
select 學號,姓名,專業名,性別,出生日期,總學分
from xs
order by 學號
limit 5;//查找學號最靠前的5位學生的信息

select 學號,姓名,專業名,性別,出生日期,總學分
from xs
order by 學號
limit 3,5;//查從第4位同學開始的5位學生的信息

20.UNION 語句
select 學號,姓名,專業名,性別,出生日期,總學分
from xs
where 學號=‘081101’
union
select 學號,姓名,專業名,性別,出生日期,總學分
from xs
where 學號=‘081210’

21.HANDLER 語句
handler kc open;

handler kc read first
where 學分>4;

handler kc read next;

handler kc close;

MYSQL視圖
22.創建視圖
create or replace view test.cs_kc
as
select xs.學號,課程號,成績
from test.xs,test.xs_kc
where xs.學號=xs_kc.學號 and xs.專業名=‘計算機’
with check option;//創建test數據庫上的cs_kc視圖,包括計算機專業各學生的學號、其選修的課程號及成績

use test
create view cs_kc_avg(num,score_avg)
as
select 學號,avg(成績)
from cs_kc
group by 學號;//創建test數據庫上的計算機專業學生的平均成績視圖cs_kc_avg
23.查詢視圖
select 學號,課程號
from cs_kc;
create view xs_kc_avg(num,score_avg)
as
select 學號,avg(成績)
from xs_kc
group by 學號;
select * from xs_kc_avg
where score_avg>=80;
24.更新視圖
create or replace view cs_xs
as
select *
from xs
where 專業名=‘計算機’
with check option;
insert into cs_xs values(‘081255’,‘李牧’,‘計算機’,1,‘1994-10-14’,50,null,null);

update cs_xs
set 總學分=總學分+8;

update cs_kc
set 成績=90
where 學號='081101’and 課程號=‘101’//若一個視圖依賴於多個基本表,一次只能修改一個基本表的數據

delete from cs_xs where 學號=‘081255’;

25.修改視圖
alter view cs_kc
as
select 學號,姓名,總學分
from xs
where 專業名=‘計算機’;//修改爲只包含計算機專業的學生的。。。
26.刪除視圖
drop view cs_xs;

第五章 MYSQL索引與完整性約束

  1. CREATE INDEX 語句創建
    create index xh_xs on xs(學號(5) asc);
    create index xskc_in on xs_kc(學號,課程號);
    2.ALTER TABLE 語句創建
    alter table xs
    add index xs_xm using btree(姓名);//在xs表的姓名列上創建一個非唯一的索引
    alter table xs
    add index mark(出生日期,性別);
    show index from xs;// 查看索引
    3.在建立表時創建索引
    create table xs_kc
    (
    學號 char(6) not null,
    課程號 char(3) not null,
    成績 tinyint(1),
    學分 tinyint(1),
    primary key(學號,課程號),
    index cj(成績)
    )
    4.刪除索引
    drop index index_name on table_name;
    alter table xs drop index mark;
    5.索引的利弊 加快查詢速度,佔據磁盤空間 利大於弊
    6.主鍵約束
    create table xs1
    (
    學號 varchar(6) null,
    姓名 varchar(8) not null primary key,
    出生日期 datetime
    );

create table course
(
學號 varchar(6) not null,
姓名 varchar(8) not null,
畢業日期 date not null,
課程號 varchar(3),
學分 tinyint,
primary key(課程號,學號,畢業日期)
);
create table course
(
學號 varchar(6) not null,
姓名 varchar(8) not null,
畢業日期 date not null,
課程號 varchar(3),
學分 tinyint,
primary key index_course(課程號,學號,畢業日期)
);//給索引賦名稱
7.替代鍵約束
create table xs1
(
學號 varchar(6) not null,
姓名 varchar(8) null unique,
出生日期 datetime null,
primary key(學號)
);
//書上有錯誤
create table xs1
(
學號 varchar(6) not null,
姓名 varchar(8) not null,
出生日期 datetime null,
primary key(學號),
unique(姓名)
);
8.參照完整性約束
create table xs1
(
學號 varchar(6) null,
姓名 varchar(8) not null,
出生日期 datetime null,
primary key(姓名),
foreign key(學號)
references xs(學號)
on delete restrict
on update restrict
);

select * from xs1
where 學號 not in
(
select 學號 from xs
);

create table xs2
(
學號 varchar(6) not null,
姓名 varchar(8) not null,
出生日期 datetime null,
班長 varchar(6) not null,
primary key(學號),
foreign key(班長) references xs2(學號)
);//自參照表

create table xs1
(
學號 varchar(6) not null,
姓名 varchar(8) not null,
出生日期 datetime null,
primary key(學號),
foreign key(學號)
references xs(學號)
on update cascade
);//主表更新刪除子表跟着

9.CHECK完整性約束
create table student
(
學號 char(6) not null,
性別 char(1) not null
check(性別 in (‘男’,‘女’))
);

create table student1
(
學號 char(6) not null,
出生日期 date not null
check(出生日期>‘1990-01-01’)
);

create table student2
(
學號 char(6) not null,
性別 char(1) not null
check(性別 in
(select 性別 from student))
);

create table student3
(
學號 char(6) not null,
最好成績 int(1) not null,
平均成績 int(1) not null,
check(最好成績>平均成績)
);
10.命名完整性約束
create table xs1
(
學號 varchar(6) null,
姓名 varchar(8) not null,
出生日期 datetime null,
constraint primary_key_xs1
primary key(姓名)
);
11.刪除完整性約束
alter table xs1 drop primary key;
alter table table_name add constraint primary key();//增加主鍵
第六章MYSQL語言結構

第七章 MYSQL5過程式數據庫對象

1.delimiter // 將MYSQL結束符修改爲兩個斜槓
2.創建儲存過程
delimiter createproceduredeletestudent(inxhchar(6))begindeletefromxswhere=xh;end create procedure delete_student(in xh char(6)) begin delete from xs where 學號=xh; end
delimiter;
3.儲存過程體
a.局部變量
declare num int(4);
declare str1,str2 varchar(6);
局部變量只能在begin。。。end語句塊中聲明

b.使用set語句賦值
set num=1,str1=‘hello’;

c.select…into語句
select 姓名,專業名 into name,project
from xs
where 學號=‘081101’;

4.流程控制語句
(1)if語句
delimiter createproceduretest.compar1(ink1integer,ink2integer,outk3char(6))beginifk1>k2thensetk3=;elseifk1=k2thensetk3=;elsesetk3=;endif;end create procedure test.compar1 (in k1 integer,in k2 integer,out k3 char(6)) begin if k1>k2 then set k3='大於'; elseif k1=k2 then set k3='等於'; else set k3='小於'; end if; end
delimiter ;

(2)case語句
delimiter createproceduretest.result(instrvarchar(4),outsexvarchar(4))begincasestrwhenmthensetsex=;whenfthensetsex=;elsesetsex=;endcase;end create procedure test.result (in str varchar(4),out sex varchar(4)) begin case str when 'm' then set sex='男'; when 'f' then set sex='女'; else set sex='無'; end case; end
delimiter ;

case
when str=‘m’ then set sex=‘男’;
when str=‘f’ then set sex=‘女’;
else set sex=‘無’
end case;

(3)循環語句
delimiter createproceduredowhile()begindeclarev1intdefault5;whilev1>0dosetv1=v11;endwhile;end create procedure dowhile() begin declare v1 int default 5; while v1>0 do set v1=v1-1; end while; end
delimiter ; //while 循環

delimiter createproceduredowhile()begindeclarev1intdefault0;repeatsetv1=v11;untilv1<1;endrepeatend create procedure dowhile() begin declare v1 int default 0; repeat set v1=v1-1; until v1<1; end repeat end
delimiter ; //repeat 循環

delimiter createproceduredoloop()beginset@a=10;label:loopset@a=@a1;if@a<0thenleavelabel;endif;endlooplabel;end create procedure doloop() begin set @a=10; label:loop set @a=@a-1; if @a<0 then leave label; end if; end loop label; end
delimiter ; //loop循環 允許某特定語句或語句羣重複執行

call doloop();//調用存儲過程
select @a; //查看用戶變量的值
leave label //退出被標註的循環語句
iterate label //重新開始一次循環

5.處理程序和條件
use test;
delimiter createproceduremyinsert1()begindeclarecontinuehandlerforsqlstate23000set@x2=1;set@x=2;insertintoxsvalues(071101,,,1,19940210,50,null,null);set@x=3;end create procedure my_insert1() begin declare continue handler for sqlstate '23000'set @x2=1; set @x=2; insert into xs values('071101','王民','計算機',1,'1994-02-10',50,null,null); set @x=3; end
delimiter ;

6.遊標
delimiter createprocedurecompute(outnumberinteger)begindeclarexhchar(6);declarefoundbooleandefaulttrue;declarenumberxscursorforselectfromxs;declarecontinuehandlerfornotfoundsetfound=false;setnumber=0;opennumberxs;fetchnumberxsintoxh;whilefounddosetnumber=number+1;fetchnumberxsintoxh;endwhile;closenumberxs;end create procedure compute (out number integer) begin declare xh char(6); declare found boolean default true; declare number_xs cursor for select 學號 from xs; declare continue handler for not found set found=false; set number=0; open number_xs; fetch number_xs into xh; while found do set number=number+1; fetch number_xs into xh; end while; close number_xs; end
delimiter ;

7.13存儲過程的調用、刪除和修改
7.存儲過程的調用
delimiter createproceduretest.doupdate(intxhchar(6),intkcmchar(16))begindeclarekchchar(3);declarexftinyint;declarecjtinyint;select,intokch,xffromkcwhere=kcm;ifcj<60thenupdatexskcset=0where=xhand=kch;elseupdatexskcset=xfwhere=xhand=kch;endif;end create procedure test.do_update(int xh char(6),int kcm char(16)) begin declare kch char(3); declare xf tinyint; declare cj tinyint; select 課程號,學分 into kch,xf from kc where 課程名=kcm; if cj<60 then update xs_kc set 學分=0 where 學號=xh and 課程號=kch; else update xs_kc set 學分=xf where 學號=xh and 課程號=kch; end if; end
delimiter ;

insert into xs_kc values(‘081101’,‘208’,50,10);

call do_update(‘081101’,‘數據結構’);
select *from xs_kc where 學號=‘081101’ and 課程號=‘208’;
//當某學生某門課程的成績小於60分時將其學分修改爲零,大於等於60分時將學分修改爲此課程的學分

create procedure test.do_insert1()
insert into xs values(‘091101’,‘陶偉’,‘軟件工程’,1,‘1994-03-05’,50,null,null);

delimiter createproceduretest.doinsert2(inxbit(1),outstrchar(8))begincalldoinsert1();ifx=0thenupdatexsset=,=0where=091101;setstr=;elseifx=1thendeletefromxswhere=091101;setstr=;endif;end create procedure test.do_insert2(in x bit(1),out str char(8)) begin call do_insert1(); if x=0 then update xs set 姓名='劉英',性別=0 where 學號='091101'; set str='修改成功'; elseif x=1 then delete from xs where 學號='091101'; set str='刪除成功'; end if; end
delimiter ;

call do_insert2(1,@str);
select @str;

call do_inisert2(0,@str);
select @str;

8.儲存過程的刪除
drop procedure if exists dowhile;

9.存儲過程的修改

delimiter dropprocedureifexistsdoquery;createproceduredoquery()beginselectfromxs;end drop procedure if exists do_query; create procedure do_query() begin select * from xs; end
delimiter ;

7.2存儲函數
7.2.1 創建存儲函數

SET GLOBAL log_bin_trust_function_creators = 1;//8.0版本要先加這句話
10.返回xs表中學生的數目作爲結果
delimiter createfunctionnumofxs()returnsintbeginreturn(selectcount()fromxs);end create function num_of_xs() returns int begin return(select count(*) from xs); end
delimiter ;

11.返回某個同學的姓名
delimiter createfunctionnameofstu(xhchar(6))returnschar(8)beginreturn(selectfromxswhere=xh);end create function name_of_stu(xh char(6)) returns char(8) begin return (select 姓名 from xs where 學號=xh); end
delimiter ;

12.刪除xs_kc表中有,但xs表中不存在的學號
delimiter createfunctiondeletestu(xhchar(6))returnsbooleanbegindeclarestuchar(6);selectintostufromxswhere=xh;ifstuisnullthendeletefromxskcwhere=xh;returntrue;elsereturnfalse;endif;end create function delete_stu(xh char(6)) returns boolean begin declare stu char(6); select 姓名 into stu from xs where 學號=xh; if stu is null then delete from xs_kc where 學號=xh; return true; else return false; end if; end
delimiter ;

7.2.2存儲函數的調用、刪除和修改
13.select num_of_xs();
select name_of_stu(‘081106’);

delimiter createfunctionisstu(xhchar(6))returnschar(10)begindeclarenamechar(8);selectnameofstu(xh)intoname;ifname=thenreturn(selectfromxswhere=xh);elsereturnfalse;endif;end create function is_stu(xh char(6)) returns char(10) begin declare name char(8); select name_of_stu(xh) into name; if name='王林' then return(select 出生日期 from xs where 學號=xh); else return 'false'; end if; end
delimiter ;

select is_stu(‘081102’);
select is_stu(‘081101’);

drop function if exists num_of_xs;

7.3 觸發器
14.在表上創建一個觸發器,每次插入操作時,將用戶變量str的值設爲“trigger is working”
create table tablel(a integer);
create trigger tablel_insert after insert
on tablel for each row
set @str=‘trigger is working’;

insert into tablel values(10);

select @str;

15.當刪除表xs中某個學生的信息時,同時將xs_kc表中與該學生有關的數據全部刪除
delimiter createtriggerxsdeleteafterdeleteonxsforeachrowbegindeletefromxskcwhere=old.;end create trigger xs_delete after delete on xs for each row begin delete from xs_kc where 學號=old.學號; end
delimiter ;

delete from xs where 學號=‘081101’;
select * from xs_kc;

16.當修改xs_kc表中數據時,如果修改後的成績小於60分,則觸發器將該成績對應的課程學分修改爲0,否則將學分改成對應課程的學分
delimiter createtriggerxskcupdatebeforeupdateonxskcforeachrowbegindeclarexfint(1);selectintoxffromkcwhere=new.;ifnew.<60thensetnew.=0;elsesetnew.=xf;endif;end create trigger xs_kc_update before update on xs_kc for each row begin declare xf int(1); select 學分 into xf from kc where 課程號=new.課程號; if new.成績<60 then set new.學分=0; else set new.學分=xf; end if; end
delimiter ;

17.當向xs_kc表中插入一行數據時,根據成績對xs表的總學分進行修改。如果成績>=60,總學分加上該課程的學分,否則總學分不變
delimiter createtriggerxskczxfafterinsertonxskcforeachrowbegindeclarexsint(1);selectintoxsfromkcwhere=new.;ifnew.>=60thenupdatexsset=+xfwhere=new.;endif;end create trigger xs_kc_zxf after insert on xs_kc for each row begin declare xs int(1); select 學分 into xs from kc where 課程號=new.課程號; if new.成績>=60 then update xs set 總學分=總學分+xf where 學號=new.學號; end if; end
delimiter ;

18.student 表中的數據與xs表同步
a.定義儲存過程
delimiter createprocedurechanges()beginreplaceintostudentselectfromxs;end create procedure changes() begin replace into student select * from xs; end
delimiter ;

b.創建觸發器
create trigger student_change after insert
on xs for each row
call changes();

c.驗證
insert into xs values
(‘091102’,‘王大慶’,‘計算機’,1,‘1994-08-14’,48,null,null);
select * from student;

19.刪除觸發器
drop trigger xs_delete;

7.4事件
7.4.1 創建事件
20.創建一個立即啓動的事件
create event direct
on schedule at now()
do insert into xs values
(‘091103’,‘張建’,‘軟件工程’,1,‘1994-06-05’,50,null,null)

21.創建一個30秒後啓動的事件
create event thrityseconds
on schedule at now()+interval 30 second
do insert into xs values
(‘091104’,‘陳建’,‘軟件工程’,1,‘1994-08-16’,50,null,null)

22.每個月啓動一次,開始於下個月並且在2018年的12月31號結束
delimiter createeventstartmonthonscheduleevery1monthstartscurdate()+interval1monthends20191231dobeginifyear(curdate())<2018theninsertintoxsvalues(091105,,,1,19940316,48,null,null);endif;end create event startmonth on schedule every 1 month starts curdate()+interval 1 month ends '2019-12-31' do begin if year(curdate())<2018 then insert into xs values ('091105','王建','軟件工程',1,'1994-03-16',48,null,null); end if; end
delimiter ;

7.4.2 修改和刪除事件
23.修改事件名字
alter event startmonth rename to firstmonth;
24.查看修改結果
show events;
25.刪除事件
drop event event_name;

第八章 MYSQL數據庫備份與恢復

8.3日誌文件
8.3.1啓用日誌
1.log-bin=E;/Program Files/MYSQL/MYSQL Server 8.0/bin/bin_log;

2.net stop mysql
3.net start mysql

8.3.2用mysqlbinlog處理日誌
4.查看日誌內容
mysqlbinlog bin_log.000001
5.將日誌保存到文本文件中
mysqlbinlog bin_log.000001>路徑
6.使用日誌恢復數據
p194 書上
7.清除所有日誌
reset master
第九章 MYSQL安全管理

9.1用戶管理
1.添加兩個用戶
create user
‘king’@'localhost’identified by ‘queen’,
‘palo’@'localhost’identified by ‘530415’;
use mysql
show tables;
select * from user;

2.刪除用戶
drop user palo@localhost;

3.修改用戶名
rename user
‘king’@‘localhost’ to ‘ken’@‘localhost’;
4.修改密碼
set password for ‘ken’@‘localhost’ = password(‘qen’);

9.2權限控制
9.2.1修改用戶名密碼
5.授予用戶ken在xs表上的SELECT權限
grant select
on xs
to ken@localhost;

6.用戶liu和zhang不存在,授予他們在xs表上的SELECT和UPDATE權限
grant select,update
on xs
to liu@localhost identified by ‘lpwd’,
zhang@localhost identified by ‘zpwd’;

7.授權ken在xs表上的學號列和姓名列的update權限
grant update(姓名,學號)
on xs
to ken@localhost;
8.授予ken在數據庫中的所有表的SELECT權限
grant select on test,* to ken@localhost;
9.授予ken在數據庫中所有的數據庫權限
grant all
on *
to ken@localhost;

10.授予peter對所有數據庫中的所有表的CREATE、ALTERT和DROP權限
grant create,alter,drop
on .
to peter@localhost identified by ‘ppwd’;

11.授予peter創建新用戶的權利
grant create user
on .
to peter@localhost;

9.2.2權限轉移和限制
12.在root用戶下授予caddy用戶SELECT權限
grant select
on test.xs
to caddy@localhost identified by ‘19830925’
with grant option;

mysql -hlocalhost -ucaddy -p19830925

grant select
on test.xs
to Jim@localhost;

  1. 授予Jim每小時只能處理一條SELECT語句的權限
    grant select
    on xs
    to Jim@localhost
    with max_queries_per-hour 1;
    9.2.3權限回收
    14.回收用戶caddy在xs表上的SELECT
    revoke select
    on xs
    from caddy@localhost;

9.3表維護語句
15.ANALYZE TABLE語句
//更新表xs的索引的可壓縮性,並隨後顯示
analyze table xs;
show index from xs;

16.CHECK TABLE語句
//檢查xs表是否正確
check table xs;

17.CHECKSUM TABLE語句
獲得表xs的效驗和的值
checksum table xs;

18.OPTIMIZE TABLE語句
//優化xs表
optimize table kc;

19.REPAIR TABLE語句
//如果一個表或索引已經損壞,可以使用REPAIR TABLE語句嘗試去修復它
只對MyISAM和ARCHIVE表起作用

還有兩個表維護語句:BACKUP TABLE和RESTOTRE TABLE語句,但是效果不理想,不推薦使用
CREATE TABLE XS
(sno char(6) not null primary key,
sname char(8) not null,
zuanyemin char(10) null,
ssex tinyint(1) not null default 1,
chushengriiqii date not null,
zongxuefen tinyint(1) null,
zhaopian blob null,
beizu text null
)engine = innodb;

CREATE TABLE KC
(kechenghao char(3) not null primary key,
kechengming char(16) not null,
kaikexueqi tinyint(1) not null default 1,
xueshi tinyint(1) not null,
xuefen tinyint(1) null
);

CREATE TABLE XC_KC
(sno char(6) not null ,
kechenghao char(3) not null ,
chengji tinyint(1) null,
xuefen tinyint(1) null,
primary key(sno,kechenghao)
);

use XSCJ

insert into XS
values(‘081101’,‘王林’,‘計算機’,1,‘1994-02-10’,50,null,null);
insert into XS
values(‘081102’,‘程明’,‘計算機’,1,‘1991-02-01’,50,null,null);
insert into XS
values(‘081103’,‘王燕’,‘計算機’,0,‘1989-10-06’,50,null,null);
insert into XS
values(‘081104’,‘韋嚴平’,‘計算機’,1,‘1990-08-26’,50,null,null);
insert into XS
values(‘081106’,‘李方方’,‘計算機’,1,‘1990-11-20’,50,null,null);
insert into XS
values(‘081107’,‘李明’,‘計算機’,1,‘1990-05-01’,54,null,‘提起修完《數據結構》,並獲學分’);
insert into XS
values(‘081108’,‘林一凡’,‘計算機’,1,‘1989-08-05’,52,null,‘已提前修完一門課’);
insert into XS
values(‘081109’,‘張強民’,‘計算機’,1,‘1989-08-11’,50,null,null);
insert into XS
values(‘081110’,‘張蔚’,‘計算機’,0,‘1991-07-22’,50,null,‘三好生’);
insert into XS
values(‘081111’,‘趙琳’,‘計算機’,0,‘1990-03-18’,50,null,null);
insert into XS
values(‘081113’,‘嚴紅’,‘計算機’,0,‘1989-08-11’,48,null,‘有一門課不及格,待補考’);
insert into XS
values(‘081201’,‘王敏’,‘通信工程’,1,‘1989-06-10’,42,null,null);
insert into XS
values(‘081202’,‘王林’,‘通信工程’,1,‘1989-01-29’,40,null,‘有一門課不及格,待補考’);
insert into XS
values(‘081204’,‘馬琳琳’,‘通信工程’,0,‘1989-02-10’,42,null,null);
insert into xs
values(‘081206’,‘李計’,‘通信工程’,1,‘1989-09-20’,42,null,null);
insert into xs
values(‘081210’,‘李紅慶’,‘通信工程’,1,‘1989-05-01’,44,null,‘已提前修完一門課,並獲得學分’);
insert into xs
values(‘081216’,‘孫祥欣’,‘通信工程’,1,‘1989-03-09’,42,null,null);
insert into xs
values(‘081218’,‘孫研’,‘通信工程’,1,‘1990-10-09’,42,null,null);
insert into xs
values(‘081220’,‘吳薇華’,‘通信工程’,0,‘1990-03-18’,42,null,null);
insert into xs
values(‘081221’,‘劉燕敏’,‘通信工程’,0,‘1989-11-12’,42,null,null);
insert into xs
values(‘081241’,‘羅林琳’,‘通信工程’,0,‘1990-01-30’,50,null,‘轉專業學習’);

//kc表
insert into kc
values(‘101’,‘計算基礎’,1,80,5);
insert into kc
values(‘102’,‘程序設計語言’,2,68,4);
insert into kc
values(‘206’,‘離散數學’,4,68,4);
insert into kc
values(‘208’,‘數據結構’,5,68,4);
insert into kc
values(‘209’,‘操作系統’,6,68,4);
insert into kc
values(‘210’,‘計算機原理’,5,85,5);
insert into kc
values(‘212’,‘數據庫原理’,7,68,4);
insert into kc
values(‘301’,‘計算機網絡’,7,51,3);
insert into kc
values(‘302’,‘軟件工程’,7,51,3);

//xs_kc表
insert into xs_kc
values(‘081101’,101,80,5);
insert into xs_kc
values(‘081101’,102,78,4);
insert into xs_kc
values(‘081101’,206,76,4);
insert into xs_kc
values(‘081103’,101,62,5);
insert into xs_kc
values(‘081103’,102,70,4);
insert into xs_kc
values(‘081103’,206,81,4);
insert into xs_kc
values(‘081104’,101,90,5);
insert into xs_kc
values(‘081104’,102,84,4);
insert into xs_kc
values(‘081104’,206,65,4);
insert into xs_kc
values(‘081102’,102,78,4);
insert into xs_kc
values(‘081102’,206,78,4);
insert into xs_kc
values(‘081106’,101,65,5);
insert into xs_kc
values(‘081106’,102,71,4);
insert into xs_kc
values(‘081106’,206,80,4);
insert into xs_kc
values(‘081107’,101,78,5);
insert into xs_kc
values(‘081107’,102,80,4);
insert into xs_kc
values(‘081107’,206,68,4);
insert into xs_kc
values(‘081108’,101,85,5);
insert into xs_kc
values(‘081108’,102,64,4);
insert into xs_kc
values(‘081108’,206,87,4);

insert into xs_kc
values(‘081109’,101,66,5);
insert into xs_kc
values(‘081109’,102,83,4);
insert into xs_kc
values(‘081109’,206,70,4);

insert into xs_kc
values(‘081110’,101,95,5);
insert into xs_kc
values(‘081110’,102,90,4);
insert into xs_kc
values(‘081110’,206,89,4);

insert into xs_kc
values(‘081111’,101,91,5);
insert into xs_kc
values(‘081111’,102,70,4);
insert into xs_kc
values(‘081111’,206,76,4);

insert into xs_kc
values(‘081113’,101,63,5);
insert into xs_kc
values(‘081113’,102,79,4);
insert into xs_kc
values(‘081113’,206,60,4);

insert into xs_kc
values(‘081201’,101,80,5);
insert into xs_kc
values(‘081202’,101,65,5);
insert into xs_kc
values(‘081203’,101,87,5);

insert into xs_kc
values(‘081204’,101,91,5);
insert into xs_kc
values(‘081210’,101,76,5);
insert into xs_kc
values(‘081216’,101,81,5);

insert into xs_kc
values(‘081218’,101,70,5);
insert into xs_kc
values(‘081220’,101,82,5);
insert into xs_kc
values(‘081221’,101,76,5);
insert into xs_kc
values(‘081241’,101,90,5);

函數不能創建 錯誤代碼:1418
SET @@global.log_bin_trust_function_creators=‘ON’;
SHOW VARIABLES LIKE ‘log_bin_trust_function_creators’;

發佈了4 篇原創文章 · 獲贊 2 · 訪問量 390
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章