一、理论:
1.触发器不能使用采用call的动态sql,不能直接返回数据到客户端
2.不能在触发器中使用开始或者结束事务的语句
3.触发器只能在支持事务的表中回滚
二、实践:
mysql> use test1; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> create table film_text( -> film_id int(11), -> title varchar(20), -> description varchar(180) -> ) engine = innodb charset = utf8 ; Query OK, 0 rows affected (0.05 sec) mysql> create table film( -> film_id int(11), -> title varchar(20), -> description varchar(180) -> ) engine = innodb charset = utf8 ; Query OK, 0 rows affected (0.02 sec) mysql> CREATE TABLE `tri_demo` ( -> `id` int(11) auto_increment not null primary key, -> `note` varchar(50) DEFAULT NULL -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; Query OK, 0 rows affected (0.03 sec) mysql> create trigger ins_film_bef -> before insert on film for each row begin -> insert into tri_demo(note) values ('before insert'); -> end; -> $$ Query OK, 0 rows affected (0.02 sec) mysql> create trigger ins_film_aft -> after insert on film for each row begin -> insert into film_text(title) values ('after insert'); -> end; -> $$ Query OK, 0 rows affected (0.02 sec) mysql> create trigger upd_film_bef -> before update on film for each row begin -> insert into tri_demo(note) values ('before update'); -> end; -> $$ Query OK, 0 rows affected (0.00 sec) mysql> create trigger upd_film_aft -> after update on film for each row begin -> insert into tri_demo(note) values ('after update'); -> end; -> $$ Query OK, 0 rows affected (0.02 sec) mysql> delimiter ; mysql> insert into film values ( 1,'film_name','film_description'); Query OK, 1 row affected (0.00 sec) mysql> select * from film; +---------+-----------+------------------+ | film_id | title | description | +---------+-----------+------------------+ | 1 | film_name | film_description | +---------+-----------+------------------+ mysql> select * from tri_demo; +----+---------------+ | id | note | +----+---------------+ | 1 | before insert | +----+---------------+ mysql> update film set title='film_update' where film_id = 1; Query OK, 1 row affected, 1 warning (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> select * from film; +---------+--------------------+--------------------------+ | film_id | title | description | +---------+--------------------+--------------------------+ | 1 | film_update | film_description | +---------+--------------------+--------------------------+ 1 rows in set (0.00 sec) mysql> select * from tri_demo; +----+---------------+ | id | note | +----+---------------+ | 1 | before insert | | 2 | before update | | 3 | after update | +----+---------------+ 3 rows in set (0.00 sec) mysql> show triggers \G; *************************** 1. row *************************** Trigger: ins_film_bef Event: INSERT Table: film Statement: begin insert into tri_demo(note) values ('before insert'); end Timing: BEFORE Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Trigger: ins_film_aft Event: INSERT Table: film Statement: begin insert into film_text(title) values ('after insert'); end Timing: AFTER Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci *************************** 3. row *************************** Trigger: upd_film_bef Event: UPDATE Table: film Statement: begin insert into tri_demo(note) values ('before update'); end Timing: BEFORE Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci *************************** 4. row *************************** Trigger: upd_film_aft Event: UPDATE Table: film Statement: begin insert into tri_demo(note) values ('after update'); end Timing: AFTER Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci 4 rows in set (0.01 sec)

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)長度,始終使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mysqlStringTypesIncludeVarChar,文本,char,Enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)創建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。 2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。 3)GrantWriteAccessto'

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具