MySQL系列之開(kāi)篇 MySQL關(guān)系型數(shù)據(jù)庫(kù)基礎(chǔ)概念
MySQL系列之一 MariaDB-server安裝
MySQL系列之二 多實(shí)例配置
MySQL系列之三 基礎(chǔ)篇
MySQL系列之四 SQL語(yǔ)法
MySQL系列之六 用戶(hù)與授權(quán)
MySQL系列之七 MySQL存儲(chǔ)引擎
MySQL系列之八 MySQL服務(wù)器變量
MySQL系列之九 mysql查詢(xún)緩存及索引
MySQL系列之十 MySQL事務(wù)隔離實(shí)現(xiàn)并發(fā)控制
MySQL系列之十一 日志記錄
MySQL系列之十二 備份與恢復(fù)
MySQL系列之十三 MySQL的復(fù)制
MySQL系列之十四 MySQL的高可用實(shí)現(xiàn)
MySQL系列之十五 MySQL常用配置和性能壓力測(cè)試
視圖:VIEW,虛表,保存有實(shí)表的查詢(xún)結(jié)果,實(shí)際數(shù)據(jù)不保存在磁盤(pán)
物化視圖:實(shí)際數(shù)據(jù)在磁盤(pán)中有保存,加快訪(fǎng)問(wèn),MySQL不支持物化視圖
基表:視圖依賴(lài)的表
視圖中的數(shù)據(jù)事實(shí)上存儲(chǔ)于“基表”中,因此,其修改操作也會(huì)針對(duì)基表實(shí)現(xiàn)。其修改操作受基表限制。
注意:修改視圖時(shí)是修改的原表
CREATE VIEW view_name AS select_statement
MariaDB [testdb]> CREATE VIEW v_students AS SELECT id,name,ages FROM students; MariaDB [testdb]> SELECT * FROM v_students; +----+---------------+------+ | id | name | ages | +----+---------------+------+ | 1 | tom | 26 | | 2 | jerry | 19 | | 3 | maria | 19 | | 4 | xiaolongnv | 18 | | 5 | dongfangbubai | 28 | | 6 | ouyangfeng | 56 | +----+---------------+------+
SHOW CREATE VIEW view_name
MariaDB [testdb]> SHOW CREATE VIEW v_students\G View: v_students Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_students` AS select `students`.`id` AS `id`,`students`.`name` AS `name`,`students`.`ages` AS `ages` from `students`
MariaDB [testdb]> SHOW TABLE STATUS LIKE 'v_students'\G Name: v_students Comment: VIEW #判斷一個(gè)表是否是視圖
DROP VIEW [IF EXISTS] view_name [, view_name] ...
MariaDB [testdb]> DROP VIEW v_students;
說(shuō)明: 參數(shù)可以有多個(gè),也可以沒(méi)有參數(shù),必須有且只有一個(gè)返回值。
參考官方文檔:https://dev.mysql.com/doc/refman/5.7/en/func-op-summary-ref.html
自定義函數(shù)保存在mysql.proc表中
MariaDB [testdb]> DELIMITER // #修改結(jié)束符為// MariaDB [testdb]> CREATE FUNCTION addTwoNumber(x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED) -> RETURNS SMALLINT -> BEGIN -> DECLARE a, b SMALLINT UNSIGNED DEFAULT 10; -> SET a = x, b = y; -> RETURN a+b; -> END// Query OK, 0 rows affected (0.01 sec) MariaDB [testdb]> DELIMITER ; #定義完函數(shù)后再修改回來(lái) MariaDB [testdb]> SELECT addTwoNumber(8,9); #調(diào)用UDF求和 +-------------------+ | addTwoNumber(8,9) | +-------------------+ | 17 | +-------------------+
存儲(chǔ)過(guò)程把經(jīng)常使用的SQL語(yǔ)句或業(yè)務(wù)邏輯封裝起來(lái),預(yù)編譯保存在數(shù)據(jù)庫(kù)中,當(dāng)需要時(shí)從數(shù)據(jù)庫(kù)中直接調(diào)用,省去了編譯的過(guò)程。提高了運(yùn)行速度同時(shí)降低網(wǎng)絡(luò)數(shù)據(jù)傳輸量
存儲(chǔ)過(guò)程:存儲(chǔ)過(guò)程保存在mysql.proc表中
ALTER語(yǔ)句修改存儲(chǔ)過(guò)程只能修改存儲(chǔ)過(guò)程的注釋等無(wú)關(guān)緊要的東西,不能修改存儲(chǔ)過(guò)程體,所以要修改存儲(chǔ)過(guò)程,方法就是刪除重建
流程控制
存儲(chǔ)過(guò)程和函數(shù)中可以使用流程控制來(lái)控制語(yǔ)句的執(zhí)行
觸發(fā)器的執(zhí)行不是由程序調(diào)用,也不是由手工啟動(dòng),而是由事件來(lái)觸發(fā)、激活從而實(shí)現(xiàn)執(zhí)行。
CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_body
trigger_name:觸發(fā)器的名稱(chēng)
trigger_time:{ BEFORE | AFTER },表示在事件之前或之后觸發(fā)
trigger_event::{ INSERT |UPDATE | DELETE },觸發(fā)的具體事件
tbl_name:該觸發(fā)器作用在表名
示例:創(chuàng)建觸發(fā)器,在向?qū)W生表INSERT數(shù)據(jù)時(shí),學(xué)生數(shù)增加,刪除學(xué)生信息時(shí),學(xué)生數(shù)減少。
MariaDB [testdb]> CREATE TABLE students_info (id TINYINT(2) NOT NULL AUTO_INCREMENT,name VARCHAR(30) DEFAULT NULL,PRIMARY KEY(id)); #創(chuàng)建一張學(xué)生信息表 MariaDB [testdb]> CREATE TABLE students_count (stu_count TINYINT(2) DEFAULT 0); #創(chuàng)建一張學(xué)生數(shù)量表 MariaDB [testdb]> INSERT INTO students_count VALUES(0); #給個(gè)初識(shí)值0 MariaDB [testdb]> CREATE TRIGGER trigger_students_count_insert -> AFTER INSERT -> ON students_info FOR EACH ROW -> UPDATE students_count SET stu_count=stu_count+1; Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> CREATE TRIGGER trigger_students_count_delete -> AFTER DELETE -> ON students_info FOR EACH ROW -> UPDATE students_count SET stu_count=stu_count-1; Query OK, 0 rows affected (0.01 sec) MariaDB [testdb]> INSERT students_info(id,name) VALUES (1,'Tom'),(2,'Maria'); MariaDB [testdb]> SELECT * FROM students_info; +----+-------+ | id | name | +----+-------+ | 1 | Tom | | 2 | Maria | +----+-------+ MariaDB [testdb]> SELECT * FROM students_count; #插入記錄,觸發(fā)事件,數(shù)量增加為2 +-----------+ | stu_count | +-----------+ | 2 | +-----------+ MariaDB [testdb]> DELETE FROM students_info WHERE id=1; MariaDB [testdb]> SELECT * FROM students_info; +----+-------+ | id | name | +----+-------+ | 2 | Maria | +----+-------+ MariaDB [testdb]> SELECT * FROM students_count; #刪除記錄,數(shù)量減1 +-----------+ | stu_count | +-----------+ | 1 | +-----------+
到此這篇關(guān)于MySQL視圖、存儲(chǔ)函數(shù)、存儲(chǔ)過(guò)程、觸發(fā)器的文章就介紹到這了,更多相關(guān)MySQL視圖、存儲(chǔ)函數(shù)、存儲(chǔ)過(guò)程、觸發(fā)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:福州 揚(yáng)州 山西 三明 定西 溫州 無(wú)錫 阿里
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL系列之五 視圖、存儲(chǔ)函數(shù)、存儲(chǔ)過(guò)程、觸發(fā)器》,本文關(guān)鍵詞 MySQL,系列,之五,視圖,存儲(chǔ),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。