上一篇博客咱們介紹了數據庫的一些基本操做,那麼此次就讓咱們一次來認識一下操做數據庫的一些更深刻的方法。mysql
1、視圖sql
視圖一般分爲三種:數據庫
(1)簡單視圖的建立網絡
mysql> create view v1_students as select name,age from students; Query OK, 0 rows affected (0.01 sec)
(2)複雜視圖的建立函數
mysql> create view v2_students as select s.name student_name,t.name teacher_name from students s join teachers t on s.teacherid=t.tid; Query OK, 0 rows affected (0.00 sec)
(3)查看指定的視圖信息ui
mysql> show create view v2_students\G; *************************** 1. row *************************** View: v2_students Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2_students` AS select `s`.`Name` AS `student_name`,`t`.`Name` AS `teacher_name` from (`students` `s` join `teachers` `t` on((`s`.`TeacherID` = `t`.`TID`))) character_set_client: latin1 collation_connection: latin1_swedish_ci 1 row in set (0.00 sec)
(4)查看全部的視圖信息spa
MariaDB [hidb]> select * from information_schema.views\G; mysql> select * from information_schema.views\G; *************************** 1. row *************************** TABLE_CATALOG: NULL TABLE_SCHEMA: hellodb TABLE_NAME: v1_students VIEW_DEFINITION: select `hellodb`.`students`.`Name` AS `name`,`hellodb`.`students`.`Age` AS `age` from `hellodb`.`students` CHECK_OPTION: NONE IS_UPDATABLE: YES DEFINER: root@localhost SECURITY_TYPE: DEFINER CHARACTER_SET_CLIENT: latin1 COLLATION_CONNECTION: latin1_swedish_ci *************************** 2. row *************************** TABLE_CATALOG: NULL TABLE_SCHEMA: hellodb TABLE_NAME: v2_students VIEW_DEFINITION: select `s`.`Name` AS `student_name`,`t`.`Name` AS `teacher_name` from (`hellodb`.`students` `s` join `hellodb`.`teachers` `t` on((`s`.`TeacherID` = `t`.`TID`))) CHECK_OPTION: NONE IS_UPDATABLE: YES DEFINER: root@localhost SECURITY_TYPE: DEFINER CHARACTER_SET_CLIENT: latin1 COLLATION_CONNECTION: latin1_swedish_ci 2 rows in set (0.05 sec)
(5)刪除視圖code
MariaDB [hidb]> drop view v1_students; Query OK, 0 rows affected (0.00 sec)
(6)更新視圖數據orm
mysql> update v2_students set teacher_name='Tie Muzhen' where student_name='da ji'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from teachers; +-----+------------+-----+--------+
| TID | Name | Age | Gender |
+-----+------------+-----+--------+
| 1 | Liu Bang | 45 | M |
| 2 | Ying Zheng | 94 | M |
| 3 | Wu Zetian | 77 | F |
| 4 | Tie Muzhen | 93 | F |
+-----+------------+-----+--------+
4 rows in set (0.00 sec)
由這個例子咱們能夠看出,修改視圖實際上修改的是基表,那麼須要注意的就是有些狀況下是不能修改視圖的數據的。分爲如下幾種狀況,select字句中不能包含distinct,組函數,union以及group by 等語句。blog
2、自定義函數
函數有多種,大體分爲自定義和內置函數兩種,內置函數也有不少,不須要定義,可直接進行調用,那麼咱們一塊兒來詳細的認識一下自定義函數的用法。
(1)自定義函數的建立
create +function 而後加函數名 後跟renturn內容便可。
mysql> CREATE FUNCTION simpleFun() RETURNS VARCHAR(20) RETURN "Hello World!"; Query OK, 0 rows affected (0.04 sec)
(2) 查看全部函數信息
mysql> show function status\G; *************************** 1. row *************************** Db: hellodb Name: simpleFun Type: FUNCTION Definer: root@localhost Modified: 2018-09-06 01:31:30 Created: 2018-09-06 01:31:30 Security_type: DEFINER Comment: character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec)
(3)查看指定自定義函數的定義
mysql> show create function simplefun\G; *************************** 1. row ***************************
Function: simplefun sql_mode: Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `simplefun`() RETURNS varchar(20) CHARSET utf8 RETURN "Hello World!" character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec)
(4)帶參自定義函數
mysql> delimiter // 將分割符定義爲// mysql> create function addtwonumber(x smallint unsigned,y smallint unsigned) -> returns smallint begin
-> declare a,b smallint unsigned; -> set a=x,b=y; -> return a+b; -> end// Query OK, 0 rows affected (0.03 sec) mysql> delimiter ; 從新定義爲; mysql> select addtwonumber(10,20); +-------------------------------+
| addtwonumber(10,20) |
+-------------------------------+
| 30 |
+-------------------------------+
1 row in set (0.03 sec)
(5)刪除自定義函數
MariaDB [mysql]> drop function simplefun; Query OK, 0 rows affected (0.00 sec)
3、觸發器trigger
觸發器是基於兩個表之間的聯繫,一個表發生變化觸動觸發器而後引發的另外一個表也發生變化。
先建兩個表
mysql> create table student_info( stu_id int(11) primary key auto_increment, -> stu_name varchar(255) default null); Query OK, 0 rows affected (0.01 sec) create table student_count( student_count int(11) default 0); Query OK, 0 rows affected (0.01 sec) mysql> insert into student_count values(0); Query OK, 1 row affected (0.00 sec) 表的基本操做已完成,接下來就是創建觸發器了。
(1)觸發器建立
mysql> create trigger trigger_student_count_insert after insert on student_info for each row update student_count set student_count=student_count+1; Query OK, 0 rows affected (0.01 sec) 這裏須要注意的是觸發器的名字儘可能起與之相關的,容易看懂。 mysql> insert into student_info values (1,'wsy'); Query OK, 1 row affected (0.00 sec) mysql> select * from student_count; +---------------+
| student_count |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
(2)查看現有觸發器
mysql> SHOW TRIGGERS\G; *************************** 1. row *************************** Trigger: trigger_student_count_insert Event: INSERT Table: student_info Statement: update student_count set student_count=student_count+1 Timing: AFTER Created: NULL sql_mode: Definer: root@localhost character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec)
(3)刪除觸發器
mysql>DROP TRIGGER trigger_student_count_insert;
4、存儲過程
存儲過程:存儲過程保存在mysql.proc表中
(1)建立一個無參存儲過程
mysql> delimiter // mysql> create procedure showtime() begin select now(); -> end // Query OK, 0 rows affected (0.00 sec)
(2) 建立一個含參存儲過程
mysql>create procedure deletebyld(in uid smallint unsigned,out num smallint unsigned) begin delete from hellodb.students where stuid=uid; select row_count() into num; end// mysql> delimiter ; mysql> call deletebyld(2,@line); Query OK, 0 rows affected (0.02 sec) mysql> select @line; +-------+
| @line |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
(2)存儲過程的調用
mysql> call showtime; +---------------------+
| now() |
+---------------------+
| 2018-09-06 02:15:54 |
+---------------------+
1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)
(2)查看指定的存儲過程建立
mysql>show create procedure showtime\G;
(3)刪除存儲過程
mysql> drop procedure showtime;
(4)查看存儲過程列表
mysql> SHOW PROCEDURE STATUS\G; *************************** 1. row *************************** Db: hellodb Name: showtime Type: PROCEDURE Definer: root@localhost Modified: 2018-09-06 02:15:37 Created: 2018-09-06 02:15:37 Security_type: DEFINER Comment: character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: utf8_general_ci 1 row in set (0.00 sec)
(5)存儲過程的優點
有關MySQL的相關函數及觸發器內容就介紹到這裏了。