標籤:variable 結束 注釋 faq 視窗 mit time begin tran
outfile 將資料庫的資料匯出
select * into outfile ‘e://mysqloutfile//1.txt‘ from 表格名;
備份與還原
不再mysql目錄下進行備份,mysqldump -uroot -p 資料庫名 +表格名 > 具體的路徑名(你要匯入到哪裡)
如果你想得到多張表的那麼就在表格後面加一個表格
還原:
先刪除資料庫的所有東西
如果刪除不了,那麼就是還有沒有刪除乾淨
建立一個資料庫 ,用資料庫
找到檔案 source +具體的檔案;將資料匯入
視圖:
什麼是視圖呢?
視圖其實是不存在的是虛擬 是一句sql語句當查詢時,這條語句執行,得出一個結果,然後利用一個結果進行查詢
建立視圖
create view 視圖名 as select * from 表的名字;
select * from 視圖名;
改變視圖結構
alter view 視圖名 (你想改變的什麼s1,s2) select * from 表格名;
s1,s2將替代欄位
事物:
什麼是事物呢?
事物是sql的集合,如果其中一個sql集合的一個語句失敗,就意味著整個過程都是沒有意義的,應該是資料庫回到初始的狀態。
先查看 show variables like ‘%autocommit%‘
查看autocommit的值 如果autocommit是開啟的,那麼就關閉。
set autocommit=0;關閉事物。
建立兩張表格
create table stu_mon(
id int primary key auto_increment,
stu_money decimal(10,2)
)engine innodb character set utf8;
create table cla_mon(
id int primary key auto_increment,
cla_money deciaml(10,2),
stu_count int
)engine innodb character set utf8;
改變資料庫中的資料
update stu_mon set stu_money =stu_money-50 where id=1;
update cla_mon set cla_money=cla_money+50 where id=1;
開啟另一個視窗,如果改變資料將在這個視窗的值不會改變,因為改變autocommit的值為0,就不會立刻改變資料庫的值。
你過你想改變視窗的值那麼你就要 提交,那麼就改變了資料庫中的值。
commit;
如果你改變了值,卻改錯了,所以就需要復原了;
rollback;
start transaction
這個指令 就是為了不設定autocommit的值
首先設定autocommit的值為1;
當一開始時是開啟的,開就是開,在操作事物時關閉,當你執行完事物時,執行完食物就開啟回到初始的狀態
觸發器:
在當前的表上,對錶的每行進行設定一個監聽事件,每當事件發生時,會執行一段sql語句
建立觸發器
create trigger 觸發器的名稱+ 事件(比如after update)on 你想操作的表格stu_mon
for each row
update 另一個表cla_mon set cla_money=cla_money+10 where id=1;
如果你進行改變stu_mon的值,那沒cla_mon的值也會改變
觸發器的使用細節:
不能同名;
一類事件設定一個觸發器;
觸發器程式內的new old
具體的例子
刪除之前的觸發器
drop trigger 觸發器的名稱
create trigger 觸發器的名稱 +事件+on+ 你想操作的表格
for each row
update 另一個表 set cla_money=cla_money+(new.stu_money-old.stu_money) where id=1;
多條sql語句
刪除之前的觸發器
drop trigger 用過的觸發器名稱
decimal $$//結束符
create trigger chufaqi after update on stu_mon
for each row
begin
update cla_mon set cla_money=cla_money+(new.stu_money-old.stu_money) where id=1;
update cla_mon set stu_count=stu_count+1 where id=1;
end
$$//結束符避免分號的衝突
你插進了一個資料,那麼就改變了cla表中的值
如果你想對插入事件進行操作
但是insert 不能使用old資料
delete 不能使用new資料
decimal //
create trigger chufaqi after insert on stu_mon
for each row
begin
update cla_mon set stu_count=stu_count+new.id;
update cla_mon set cla_money=cla_money+50;
end
//
insert into stu_mon values(3,5000);
那麼觸發器中的new.id=3
mysql函數
注釋:# --[空格]
select * from stu_mon #where id=1;
;//結束符
查出來
decimal 來修改語句的結束符。
變數:設定變數
1.select @who:=‘aa‘;
2.select * from stu_mon into where @who=‘aa‘;
變數一定要加@
查看變數
select @who;
內建函數
rand()0-1之間的隨機數
format(x,d)
x為你要格式化的資料 d你想精確到幾位元
unix_timestamp();得到秒的數字
substring(具體的資料,2,2)順時針得到兩個
資料庫是從1開始的
加密方式:
md5(你想要加密的)
sha1()
password()
自訂函數
decliniter //
資料庫 的outfile 備份與還原 視圖 事物 觸發器 mysql函數和自訂函數