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 change mysql> select * from emp; +------------+----------+------+--------+ | ename | hiredate | sal | deptno | +------------+----------+------+--------+ | aaaaa | NULL | NULL | 1 | | cccccccccc | NULL | NULL | 2 | | ddddddddd | NULL | NULL | 3 | | ffffff | NULL | NULL | 4 | | ggg | NULL | NULL | 5 | | a1 | NULL | NULL | 5 | +------------+----------+------+--------+ 6 rows in set (0.00 sec) mysql> show create table emp \G; *************************** 1. row *************************** Table: emp Create Table: CREATE TABLE `emp` ( `ename` varchar(10) DEFAULT NULL, `hiredate` date DEFAULT NULL, `sal` decimal(10,2) DEFAULT NULL, `deptno` int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.01 sec) ERROR: No query specified mysql> DELIMITER && mysql> CREATE PROCEDURE num_from_employee (IN input_deptno int, OUT count_num INT ) -> READS SQL DATA -> BEGIN -> SELECT COUNT(*) FROM emp WHERE deptno=input_deptno ; -> END && Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call num_from_employee(5,@a); +----------+ | COUNT(*) | +----------+ | 2 | +----------+ 1 row in set (0.02 sec) Query OK, 0 rows affected (0.02 sec) mysql> call num_from_employee(1,@a); +----------+ | COUNT(*) | +----------+ | 1 | +----------+ 1 row in set (0.01 sec) Query OK, 0 rows affected (0.01 sec) mysql> create table inventory( -> film_id int(11), -> store_id int(11), -> inventory_in_stock varchar(50) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.02 sec) mysql> insert into inventory(film_id,store_id,inventory_in_stock) values (1,2,'aaaaaaaa'), (3,4,'bbbb'), (5,6,'cccccccccc'), (7,8,'dddddd'), (9,10,'fff'); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from inventory; +---------+----------+--------------------+ | film_id | store_id | inventory_in_stock | +---------+----------+--------------------+ | 1 | 2 | aaaaaaaa | | 3 | 4 | bbbb | | 5 | 6 | cccccccccc | | 7 | 8 | dddddd | | 9 | 10 | fff | +---------+----------+--------------------+ 5 rows in set (0.00 sec) mysql> delimiter $$ mysql> create procedure film_in_stock(in p_film_id int,in p_store_id int,out p_film_count int) -> reads sql data -> begin -> select film_id -> from inventory -> where film_id = p_film_id -> and store_id = p_store_id; -> select found_rows() into p_film_count; -> end $$ Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call film_in_stock(5,6,@a); +---------+ | film_id | +---------+ | 5 | +---------+ 1 row in set (0.01 sec) Query OK, 1 row affected (0.01 sec) mysql> show create procedure film_in_stock \G; *************************** 1. row *************************** Procedure: film_in_stock sql_mode: Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `film_in_stock`(in p_film_id int,in p_store_id int,out p_film_count int) READS SQL DATA begin select film_id from inventory where film_id = p_film_id and store_id = p_store_id; select found_rows() into p_film_count; end character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci 1 row in set (0.01 sec) ERROR: No query specified mysql> create table actor( -> actor_id int(11) NOT NULL AUTO_INCREMENT , -> first_name varchar(30), -> last_name varchar(30), -> PRIMARY KEY (actor_id) -> ) engine = innodb charset = utf8; Query OK, 0 rows affected (0.02 sec) mysql> delimiter $$ mysql> create procedure actor_insert() -> begin -> set @x = 1; -> insert into actor(actor_id,first_name,last_name) values (201,'Test',201); -> set @x = 2; -> insert into actor(actor_id,first_name,last_name) values(1,'Test','1'); -> set @x = 3; -> end $$ Query OK, 0 rows affected (0.01 sec) mysql> call actor_insert(); Query OK, 0 rows affected (0.02 sec) mysql> call actor_insert(); ERROR 1062 (23000): Duplicate entry '201' for key 'PRIMARY' mysql> select @x; +------+ | @x | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> delimiter $$ mysql> create procedure actor_insert_new() -> begin -> DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1; -> set @x = 1; -> insert into actor(actor_id,first_name,last_name) values (201,'Test',201); -> set @x = 2; -> insert into actor(actor_id,first_name,last_name) values(1,'Test','1'); -> set @x = 3; -> end $$ Query OK, 0 rows affected (0.02 sec) mysql> delimiter ; mysql> call actor_insert_new(); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> call actor_insert_new(); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select @x,@x2; +------+------+ | @x | @x2 | +------+------+ | 3 | 1 | +------+------+ 1 row in set (0.00 sec) mysql> show create table payment \G; *************************** 1. row *************************** Table: payment Create Table: CREATE TABLE `payment` ( `staff_id` int(11) DEFAULT NULL, `amount` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.01 sec) ERROR: No query specified mysql> select * from payment; +----------+--------+ | staff_id | amount | +----------+--------+ | 1 | 10000 | | 2 | 20000 | | 3 | 30000 | | 4 | 400000 | | 5 | 500000 | +----------+--------+ 5 rows in set (0.01 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> declare tmp_name varchar(30) default ""; -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> select i_staff_id,d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call payment_stat(); +------------+----------+ | i_staff_id | d_amount | +------------+----------+ | 1 | 10000 | +------------+----------+ 1 row in set (0.01 sec) +------------+----------+ | i_staff_id | d_amount | +------------+----------+ | 2 | 20000 | +------------+----------+ 1 row in set (0.01 sec) +------+------+ | @x1 | @x2 | +------+------+ | 0 | 0 | +------+------+ 1 row in set (0.01 sec) Query OK, 0 rows affected (0.01 sec) mysql> drop procedure payment_stat; Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> declare tmp_name varchar(30) default ""; -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id; -> else -> set @x2 = @x2+ d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.01 sec) mysql> call payment_stat(); -> $$ +------+-------+ | @x1 | @x2 | +------+-------+ | 3 | 30000 | +------+-------+ 1 row in set (0.01 sec) Query OK, 0 rows affected (0.01 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id + 1; -> else -> set @x2 = @x2+ d_amount ; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ | @x1 | @x2 | +------+-------+ | 3 | 30000 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> drop procedure payment_stat; Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id; -> else -> set @x2 = @x2+ d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ | @x1 | @x2 | +------+-------+ | 3 | 30000 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id + 1; -> else -> set @x2 = @x2+ d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ | @x1 | @x2 | +------+-------+ | 5 | 30000 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> drop procedure payment_stat; Query OK, 0 rows affected (0.02 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id + 6; -> else -> set @x2 = @x2+ d_amount + 5; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ | @x1 | @x2 | +------+-------+ | 15 | 30005 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER $$ mysql> mysql> CREATE PROCEDURE addNum() -> BEGIN -> DECLARE x INT; -> SET x = 0; -> for_loop : LOOP -> SET x = x + 1; -> IF x > 30 THEN -> LEAVE for_loop; -> END IF; -> IF mod(x,2) = 0 then -> select "num:",x; -> ITERATE for_loop; -> END IF; -> END LOOP; -> select "count:",x; -> END $$ Query OK, 0 rows affected (0.01 sec) mysql> call addNum(); -> $$ +------+------+ | num: | x | +------+------+ | num: | 2 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 4 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 6 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 8 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 10 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 12 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 14 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 16 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 18 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 20 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 22 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 24 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 26 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 28 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 30 | +------+------+ 1 row in set (0.00 sec) +--------+------+ | count: | x | +--------+------+ | count: | 31 | +--------+------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure repeatPractise() -> begin -> set @v = 0 ; -> REPEAT -> set @v = @v+ 1; -> UNTIL @v >=5 -> END REPEAT; -> END -> $$ Query OK, 0 rows affected (0.01 sec) mysql> call repeatPractise(); -> $$ Query OK, 0 rows affected (0.00 sec) mysql> select @v; -> $$ +------+ | @v | +------+ | 5 | +------+ 1 row in set (0.00 sec)

mysqloffersvariousStorageengines,每個suitedfordferentusecases:1)InnodBisidealForapplicationsNeedingingAcidComplianCeanDhighConcurncurnency,supportingtransactionsancions and foreignkeys.2)myisamisbestforread-Heavy-Heavywyworks,lackingtransactionsactionsacupport.3)記憶

MySQL中常見的安全漏洞包括SQL注入、弱密碼、權限配置不當和未更新的軟件。 1.SQL注入可以通過使用預處理語句防止。 2.弱密碼可以通過強制使用強密碼策略避免。 3.權限配置不當可以通過定期審查和調整用戶權限解決。 4.未更新的軟件可以通過定期檢查和更新MySQL版本來修補。

在MySQL中識別慢查詢可以通過啟用慢查詢日誌並設置閾值來實現。 1.啟用慢查詢日誌並設置閾值。 2.查看和分析慢查詢日誌文件,使用工具如mysqldumpslow或pt-query-digest進行深入分析。 3.優化慢查詢可以通過索引優化、查詢重寫和避免使用SELECT*來實現。

要監控MySQL服務器的健康和性能,應關注系統健康、性能指標和查詢執行。 1)監控系統健康:使用top、htop或SHOWGLOBALSTATUS命令查看CPU、內存、磁盤I/O和網絡活動。 2)追踪性能指標:監控查詢每秒數、平均查詢時間和緩存命中率等關鍵指標。 3)確保查詢執行優化:啟用慢查詢日誌,記錄並優化執行時間超過設定閾值的查詢。

MySQL和MariaDB的主要區別在於性能、功能和許可證:1.MySQL由Oracle開發,MariaDB是其分支。 2.MariaDB在高負載環境中性能可能更好。 3.MariaDB提供了更多的存儲引擎和功能。 4.MySQL採用雙重許可證,MariaDB完全開源。選擇時應考慮現有基礎設施、性能需求、功能需求和許可證成本。

MySQL使用的是GPL許可證。 1)GPL許可證允許自由使用、修改和分發MySQL,但修改後的分發需遵循GPL。 2)商業許可證可避免公開修改,適合需要保密的商業應用。

選擇InnoDB而不是MyISAM的情況包括:1)需要事務支持,2)高並發環境,3)需要高數據一致性;反之,選擇MyISAM的情況包括:1)主要是讀操作,2)不需要事務支持。 InnoDB適合需要高數據一致性和事務處理的應用,如電商平台,而MyISAM適合讀密集型且無需事務的應用,如博客系統。

在MySQL中,外鍵的作用是建立表與表之間的關係,確保數據的一致性和完整性。外鍵通過引用完整性檢查和級聯操作維護數據的有效性,使用時需注意性能優化和避免常見錯誤。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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

SublimeText3 Linux新版
SublimeText3 Linux最新版

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