創(chuàng)建存儲(chǔ)過(guò)程之前....
mysql -u root -p;
password:*******
show databases; --顯示所有數(shù)據(jù)庫(kù)
create database myDataBase_1; --創(chuàng)建自己的數(shù)據(jù)庫(kù) 大小寫(xiě)會(huì)被忽略
--drop database DataBaseName; --刪除數(shù)據(jù)庫(kù)
--drop database if exists DataBaseName --若數(shù)據(jù)庫(kù)存在則刪除
use mydatabase_1; --切換到自己的數(shù)據(jù)庫(kù)
create table mytable_1(....); --創(chuàng)建表
----------------------例--------------------------
create table student(
id int(4) not null primary key auto_increment,
name char(10) not null
);
--not null --不能為空
--primary key --設(shè)置主鍵
--auto_increment --自動(dòng)編號(hào)
---------------------------------------------------
--drop table TableName; --刪除表
alter table TableName add ...... --添加表結(jié)構(gòu)字段
alter table TableName drop...... --刪除表結(jié)構(gòu)字段
alter table TableName change...... --修改表結(jié)構(gòu)字段
update...select..insert..delete...略
變量
創(chuàng)建
declare name type [default val];
例>declare c int;
賦值
set @c = 20;
>@
@符號(hào)標(biāo)識(shí)后面跟的一個(gè)變量
創(chuàng)建存儲(chǔ)過(guò)程
固定格式
create procedure procedureName(in|out|inout| name type)
begin
.....
end;
設(shè)置結(jié)束標(biāo)志
mysql數(shù)據(jù)庫(kù)固定用;作為結(jié)束標(biāo)志石抡,在存儲(chǔ)過(guò)程中寫(xiě)語(yǔ)句時(shí)可能會(huì)發(fā)生沖突克胳,所以....
delimiter //
create procedure name()........
delimite ;
----------------------例--------------------------
delimiter //
create procedure insert_student(in id int, in name char(10))
begin
insert into student values(id,name);
end;
// --設(shè)置了//為分割標(biāo)識(shí)
delimiter ; --分割標(biāo)識(shí)改回 ;
---------------------------------------------------
運(yùn)行存儲(chǔ)過(guò)程
call insert_student(2,"jingbo");
執(zhí)行完成后烫堤,student表中添加一條記錄同窘,可執(zhí)行多次淑际,只需要改變參數(shù)奔穿,就可多次插入數(shù)據(jù)闻鉴。
修改存儲(chǔ)過(guò)程
修改
alter procedure procedureName;
刪除
drop procedure precedureName;