日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Mysql的操作应用

發(fā)布時間:2025/5/22 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql的操作应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.創(chuàng)建數(shù)據(jù)庫格式:create database if not exists 數(shù)據(jù)庫名 default charset utf8;注意:1、數(shù)據(jù)庫是唯一的2、if not exists先判斷是否存在這個數(shù)據(jù)庫,存在不創(chuàng)建,不存在就創(chuàng)建。3、創(chuàng)建數(shù)據(jù)庫,并設定編碼集為utf82.顯示當前數(shù)據(jù)庫服務器下所有的數(shù)據(jù)庫名show databases;use 數(shù)據(jù)庫名 選擇數(shù)據(jù)庫注意:windows下面數(shù)據(jù)庫名是不區(qū)分大小寫的,在Linux下面嚴格區(qū)分。3.刪除數(shù)據(jù)庫 drop database 數(shù)據(jù)庫名查看已經(jīng)選擇的數(shù)據(jù)庫select database(); 查看當前的數(shù)據(jù)庫的版本號select version();4.數(shù)據(jù)表的操作show tables 查看數(shù)據(jù)庫中的數(shù)據(jù)表。創(chuàng)建數(shù)據(jù)表格式:create table 表名(列的一些信息);例子:create table t1(id int(12), name varchar(100));create table `test t1`(id int(10), name varchar(50));desc 表名 查看表的結構.show create table 表名 查看建表的語句\G 格式化輸出(文本式,豎立顯示)drop table 數(shù)據(jù)表名drop table [if exists] 數(shù)據(jù)表名 嘗試性的刪除表。5.記錄操作 增、刪、改、查(1)插入數(shù)據(jù)格式: insert into 表名(字段1,字段2,字段3) values(值1,值2,值3);insert into 表名(字段1,字段2,字段3) values(a值1,a值2,a值3);(2)查詢表數(shù)據(jù)格式: select * from 表名;select 字段1,字段2,字段3 from 表名;select * from 表名 where 字段=某個值;(3)修改表數(shù)據(jù) 格式:update 表名 set 字段=某個值 where 條件;update 表名 set 字段1=值1,字段2=值2 where 條件;update 表名 set 字段=字段+值 where 條件;(4)刪除數(shù)據(jù)格式: delete from 表名 where 字段=某個值;delete from 表名; (慎重使用)delete from 表名 where 字段 = 值;清空表數(shù)據(jù)truncate table 表名;6.修改用戶密碼在退出mysql服務的情況下 輸入1、mysqladmin -u 用戶名 -p password 新密碼輸入舊密碼:2、登錄到mysql的時候使用set password for '用戶名'@'登錄主機'=password('新密碼');7.修改表的字段信息修改表的字符集alter table 表名 charset utf8修改字段的類型alter table 表名 modify 字段名 字段類型修改字段的名字并同時修改字段類型alter table 表名 change 舊字段名 新字段名 字段類型修改字段的字符集alter table 表名 modify 字段名 類型 charset utf8添加新字段alter table 表名 add 字段名 類型刪除字段alter table 表名 drop 字段名修改表名 alter table 舊表名 rename as 新表名8.刪除默認匿名的賬戶為什么我直接輸入mysql也能進入數(shù)據(jù)庫中。是匿名用戶,MySQL默認創(chuàng)建的。他有test和information_schema這兩個庫的權限。我們可以把他刪掉刪除匿名用戶:drop user ''@'localhost';9.MySQL數(shù)據(jù)庫的數(shù)據(jù)類型:MySQL的數(shù)據(jù)類型分為四大類:數(shù)值類型、字串類型、日期類型、NULL。1 數(shù)值類型:*tinyint(1字節(jié)) 0~255 -128~127smallint(2字節(jié))mediumint(3字節(jié))*int(4字節(jié))bigint(8字節(jié))*float(4字節(jié)) float(6,2) float(m,d)*double(8字節(jié)) double (m,d)decimal(自定義)字串形數(shù)值 decimal(m,d) m精度 d標度2 字串類型普通字串*char 定長字串 char(8):長度為8位 *varchar 可變字串 varchar(8):最長為8位二進制類型tinyblobblobmediumbloblongblob文本類型tinytext*text 常用于<textarea></textarea>mediumtextlongtext*enum枚舉set集合3 時間和日期類型:date 年月日time 時分秒datetime 年月日時分秒timestamp 時間戳year 年4 NULL值NULL意味著“沒有值”或“未知值”可以測試某個值是否為NULL不能對NULL值進行算術計算對NULL值進行算術運算,其結果還是NULL0或NULL都意味著假,其余值都意味著真MySQL的運算符:算術運算符:+ - * / % 比較運算符:= > < >= <= <> != 數(shù)據(jù)庫特有的比較:in,not in, is null,is not null,like, between and 邏輯運算符:and or notlike: 支持特殊符號%和_ ; 其中 %表示任意數(shù)量的任意字符,_表示任意一位字符。表的字段約束:unsigned 無符號(正數(shù))zerofill 前導零填充default 默認值not null 非空auto_increment 自增 在產(chǎn)生一個唯一的標識或順序值的時候,可以利用這個約束條件。這個約束條件只能用于整數(shù)類型,值一般從1開始。每行加1,插入一個null到一個auto_increment列時,MySQL將插入一個比出現(xiàn)過的最大值+1的值。一個表中只能有一個auto_increment列,并且必須定義為primary key或unique才能夠使用primary key 主鍵索引 (非null并不重復)unique 唯一索引 (可以為null但不重復)index 常規(guī)索引9.建表語句格式:create table 表名(字段名 類型 [字段約束],字段名 類型 [字段約束],字段名 類型 [字段約束]...);實例:create table stu(id int unsigned not null auto_increment primary key,name varchar(8) not null unique,age tinyint unsigned,sex enum('m','w') not null default 'm',classid char(6));10.插入數(shù)據(jù)1 插入指定字段insert into t1(字段1,字段2,字段3...) values ('值1','值2','值3',....);2 插入所有的字段insert into t1 values ('值1','值2','值3',....);3 插入多條數(shù)據(jù)insert into t1(字段1,字段2,字段3...) values ('值1','值2'),('值1','值2')....;4 插入結果insert into t1(字段1,字段2,字段3...) select 字段1,字段2,字段3... from t1;insert into t1() select * from t1;5 插入單條數(shù)據(jù)insert into t1 set 字段1=值1,字段2=值2....;11.查詢數(shù)據(jù)格式:select [字段列表]|* from 表名[where 搜索條件][group by 分組字段 [having 子條件]][order by 排序 asc|desc][limit 分頁參數(shù)]查看stu表中的信息: select * from stu;where條件查詢1. 查詢班級為lamp138期的學生信息select * from stu where classid='lamp138';2. 查詢lamp138期的男生信息(sex為m)select * from stu where classid='lamp138' and sex='m';3. 查詢id號值在10以上的學生信息select * from stu where id>10;4. 查詢年齡在20至25歲的學生信息select * from stu where age>=20 and age<=25;select * from stu where age between 20 and 25;5. 查詢年齡不在20至25歲的學生信息select * from stu where age not between 20 and 25;select * from stu where age<20 or age>25;6. 查詢id值為1,8,4,10,14的學生信息select * from stu where id in(1,8,4,10,14);select * from stu where id=1 or id=8 or id=4 or id=10 or id=14;7. 查詢lamp138和lamp94期的女生信息select * from stu where classid in('lamp138','lamp94') and sex='w';select * from stu where (classid='lamp138' or classid='lamp94') and sex='wLIKE 子句這時我們就需要在 WHERE 子句中使用 SQL LIKE 子句。LIKE 子句中使用百分號%字符來表示任意字符,類似于UNIX或正則表達式中的星號*。如果沒有使用百分號%, LIKE 子句與等號=的效果是一樣的。LIKE 通常與 % 一同使用,類似于一個元字符的搜索。你可以使用 AND 或者 OR 指定一個或多個條件。你可以在 DELETE 或 UPDATE 命令中使用 WHERE...LIKE 子句來指定條件??梢允褂胷egexp正則來代替 like1.查詢name字段值是以zh開頭的所有信息select * from stu where name like "zh%";select * from stu where name regexp "^zh"; --正則寫法2.查詢姓名name中含有ang子串的所有信息select * from stu where name like "%ang%";select * from stu where name regexp "ang";3.查詢姓名是任意四位字符構成的信息。select * from stu where name like "____";select * from stu where name regexp "^[a-z0-9]{4}$";創(chuàng)建一個表結構和另一個表一樣的結構:create table t3 like t1;那么現(xiàn)在t3表的結構和t1表的結構是一樣的,但是要注意數(shù)據(jù)是沒有的MySQL的統(tǒng)計函數(shù)(聚合函數(shù)):max() min() count() sum() avg()1.獲取學生表中最大、最小以及平均年齡是多少?select max(age),min(age),avg(age) from stu;2.獲取學生表中男生m的數(shù)量select count(*) from stu where sex='m';GROUP BY 語句 分組GROUP BY 語句根據(jù)一個或多個列對結果集進行分組。統(tǒng)計班級信息,按性別分組,并統(tǒng)計每組人數(shù);select sex,count(*) from stu group by sex;1.統(tǒng)計每個班級的人數(shù)select classid,count(*) from stu group by classid;2.統(tǒng)計每個班級的,男生和女生各多少人數(shù)。select classid,sex,count(*) from stu group by classid,sex;ORDER BY 排序 -- asc 默認升序 desc 降序我們知道從 MySQL 表中使用 SQL SELECT 語句來讀取數(shù)據(jù)。如果我們需要對讀取的數(shù)據(jù)進行排序,我們就可以使用 MySQL 的ORDER BY子句來 設定你想按哪個字段哪種方式來進行排序,再返回搜索結果。你可以使用任何字段來作為排序的條件,從而返回排序后的查詢結果。你可以設定多個字段來排序。你可以使用 ASC 或 DESC 關鍵字來設置查詢結果是按升序或降序排列。 默認情況下,它是按升序排列。你可以添加 WHERE...LIKE 子句來設置條件。1.按年齡升序排序查詢學生信息select * from stu order by age;select * from stu order by age asc; --默認asc升序 可省略2.年齡降序排序select * from stu order by age desc;3.查詢學生信息,按班級做升序排序,相同班級按年齡降序排序select * from stu order by classid asc,age desc;LIMIT 關鍵字 查詢部分數(shù)據(jù)-- 例如: .... limit m; 查詢數(shù)據(jù)只顯示前m條-- 例如: .... limit m,n; 排除前m條,然后再查詢出前n條1.查詢前5條信息select * from stu limit 5;2.排除前2條后再獲取4條信息select * from stu limit 2,4;12.修改數(shù)據(jù)格式:update 表名 set 字段1=值1,字段2=值2,字段n=值n... where 條件 -- 將id為11的age改為35,sex改為m值update stu set age=35,sex='m' where id=11;-- 將id值為12和14的數(shù)據(jù)值sex改為m,classid改為lamp92update stu set sex='m',classid='lamp92' where id=12 or id=14 --等價于下面update stu set sex='m',classid='lamp92' where id in(12,14);13.刪除操作格式:delete from 表名 [where 條件]-- 刪除stu表中id值為100的數(shù)據(jù)delete from stu where id=100;-- 刪除stu表中id值為20到30的數(shù)據(jù)delete from stu where id>=20 and id<=30;-- 刪除stu表中id值為20到30的數(shù)據(jù)(等級于上面寫法)delete from stu where id between 20 and 30;-- 刪除stu表中id值大于200的數(shù)據(jù)delete from stu where id>200;14.導入和導出注意: 在操作導入和導出數(shù)據(jù)的時候 不要登錄到mysql服務中,要退出mysql服務進行操作-- 將lamp138數(shù)據(jù)庫里面的所有的表導出C:\>mysqldump -u root -p lamp138 > C:\lamp138.sqlEnter password:---- 將lamp138數(shù)據(jù)庫中的stu表導出C:\>mysqldump -u root -p lamp138 stu > C:\lamp138_stu.sqlEnter password:-- 將lamp138庫中的所有表導入C:\>mysql -u root -p lamp138 < C:\lamp138.sqlEnter password:-- 將lamp138庫中stu表導入C:\>mysql -u root -p lamp138 < C:\lamp138_stu.sqlEnter password:*********************-- 將所有的數(shù)據(jù)庫進行導出mysqldump -uroot -p密碼 --all-databases --events > /tmp/bak.sql 所有數(shù)據(jù)庫備份15.MySQL的權限管理格式:grant 權限 on 數(shù)據(jù)庫.數(shù)據(jù)表 to '用戶名'@'登錄主機' identified by '密碼'刷新權限flush privileges;舉例:grant select,insert,update,delete on *.* to 'wjs'@'%' identified by '12345'; 安全的做法grant select,insert,update,delete on pass.* to 'root'@'localhost' identified by '12345'; 只針對localhost主機里面的 pass數(shù)據(jù)庫里面的數(shù)據(jù)表進行增刪該查 刪除用戶drop user 'xxoo'@'%' 16.索引的作用:索引是數(shù)據(jù)庫中用來提高搜索性能的。我們通常在做數(shù)據(jù)庫優(yōu)化的時候通常先做索引的優(yōu)化,數(shù)據(jù)量少的時候沒效果,數(shù)據(jù)越多效果越明顯。查看表中都有哪些索引show index from 表名\G索引的分類:常規(guī)索引(index)最基本的索引,沒有任何限制添加表字段的常規(guī)索引create index 索引名 on 表名(字段名)alter table 表名 add index 索引名(字段名) 在創(chuàng)建表的時候也可以進行添加create table t2(id int(10) unsigned auto_increment primary key,name varchar(255), index 索引名(字段名));刪除表字段常規(guī)索引**drop index 索引名 on 表名alter table 表名 drop index 索引名唯一索引(unique)唯一索引是可以給每個字段進行添加的,添加完了之后字段里面的值就不可以重復了,主鍵索引和唯一索引類似,但是數(shù)據(jù)表里的主鍵索引只能加在一個字段里(一般都加在id上),id是自增的,索引不會有重復的時候出現(xiàn)添加表字段的唯一索引create unique index 索引名 on 表名(字段名)alter table 表名 add unique 索引名(字段名)在創(chuàng)建表的時候也可以進行添加create table t2(id int(10) unsigned auto_increment primary key,name varchar(255), unique 索引名(字段名));刪除唯一索引drop index 索引名 on 表名主鍵索引(primary key)主鍵索引是關系數(shù)據(jù)庫中最常見的索引類型,主要作用是確定數(shù)據(jù)表里一條特定的數(shù)據(jù)記錄的位置。我們可以在字段后添加PRIMARY KEY來對字段設置為主鍵索引。注意:1.最好為每張表指定一個主鍵,但不是必須指定。2.一個表只能指定一個主鍵,而且主鍵的值不能為空3.主鍵可以有多個候選索引(例如NOT NULL,AUTO_INCREMENT)添加表字段的主鍵索引alter table 表名 add primary key (字段名)添加自增alter table 表名 modify id int(4) auto_increment刪除主鍵索引如果字段有了auto_increment 和 primary key的時候,需要刪除主鍵的話,先刪除自增,然后再刪除主鍵 刪除自增alter table 表名 change 字段名 字段名 類名刪除主鍵alter table 表名 drop primary key 全文索引(fulltext)全文索引在MySQL中是一個FULLTEXT類型索引,但FULLTEXT索引只能用于MyISAM表,并且只可以在CHAR、VARCHAR或TEXT類型的列上創(chuàng)建,也允許創(chuàng)建在一個或多個數(shù)據(jù)列上。添加表字段的全文索引alter table 表名 add fulltext 索引名(字段名)刪除全文索引drop index (索引名) on 表名alter table 表名 drop index 索引名注意:如果在創(chuàng)建索引的時候,不添加索引名的話 默認會把字段名當做索引名17.表的存儲引擎存儲引擎: 是MySQL的一個特性之一,用戶可以根據(jù)應用的需要選擇如何存儲MySQL支持MyISAM、InnoDB、HEAP、BOB、ARCHIVE、CSV、MEMORY等多種數(shù)據(jù)表引擎,在創(chuàng)建一個新MySQL數(shù)據(jù)表時,可以為它設置一個引擎類型。MyISAM和InnoDB兩種表類型最為重要:1.MyISAM數(shù)據(jù)表類型的特點是成熟、穩(wěn)定和易于管理。2.MyISAM表類型會產(chǎn)生碎片空間,要經(jīng)常使用OPTIMIZE TABLE命令去清理表空間3.MyISAM不支持事務處理,InnoDB支持4.MyISAM不支持外鍵,InnoDB支持5.MyISAM表類型的數(shù)據(jù)表效率更高6.MyISAM表類型的數(shù)據(jù)表會產(chǎn)生三個文件---.frm (表結構)---.myd (表數(shù)據(jù))---.myi (表索引)InnoDB表類型表默認只會產(chǎn)生兩個文件---.frm (表結構)---.ibd (表數(shù)據(jù)+表索引)7.MyISAM表,其優(yōu)勢就是訪問的速度快。對事務沒有完整性的要求或以select、insert為主的通常使用MyISAM表8.InnoDB表:會占用更多的磁盤空間鎖機制:MyISAM 表鎖:開銷小,加鎖快,發(fā)生沖突的概率高。InnoDB 行鎖:開銷大,加鎖慢,發(fā)生沖突的概率低。9.默認是InnoDB引擎 數(shù)據(jù)存儲在 innodb_data_home_dir選項所選的文件名的目錄里查看默認的引擎show variables like 'default_storage_engine';查看支持的引擎show engines查看當前庫里面所有的表引擎show table status from 庫名修改表引擎alter table 表名 engine = 新的表引擎ubuntu下//mysql配置文件sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf//重啟sudo /etc/init.d/mysqld restart18.表的結構要滿足三范式1NF 原子性 字段不可再分,否則就不是關系數(shù)據(jù)庫; 即表的列的具有原子性,不可再分解,即列的信息,不能分解, 只要數(shù)據(jù)庫是關系型數(shù)據(jù)庫(mysql/oracle/db2/informix/sysbase/sql server),就自動的滿足1NF。數(shù)據(jù)庫表的每一列都是不可分割的原子數(shù)據(jù)項,而不能是集合,數(shù)組,記錄等非原子數(shù)據(jù)項。如果實體中的某個屬性有多個值時,必須拆分為不同的屬性 。通俗理解即一個字段只存儲一項信息。2NF 唯一性 要求數(shù)據(jù)庫表中的每個實例或行必須可以被惟一地區(qū)分。為實現(xiàn)區(qū)分通常需要我們設計一個主鍵來實現(xiàn)(這里的主鍵不包含業(yè)務邏輯)。3NF 無冗余性 要求一個數(shù)據(jù)庫表中不包含已在其它表中已包含的非主鍵字段。就是說,表的信息,如果能夠被推導出來,就不應該單獨的設計一個字段來存放(能盡量外鍵join就用外鍵join)。很多時候,我們?yōu)榱藵M足第三范式往往會把一張表分成多張表。29.left join、right join、inner join的區(qū)別left join(左聯(lián)接) 返回包括左表中的所有記錄和右表中聯(lián)結字段相等的記錄right join(右聯(lián)接) 返回包括右表中的所有記錄和左表中聯(lián)結字段相等的記錄inner join(等值連接) 只返回兩個表中聯(lián)結字段相等的行舉例:left joinselect * from p1 left join p2 on p1.id = p2.id;right joinselect * from p1 right join p2 on p1.id = p2.id;inner joinselect * from p1 inner join p2 on p1.id = p2.id;20.子查詢1、單行子查詢select name,age,scorefrom userwhere age=(select age from stu where id = 1);2、多行子查詢select name,age,class from user where age in (select age from stu where id < 10);select name,age.class from user where age in (select age from stu where name like '王%');21.分組 group by1 查詢各個班級中年齡大于20,性別為男的人數(shù)、姓名和班級 (數(shù)據(jù)表: stu) 為了防止班級里面有重名的現(xiàn)象發(fā)生(例如 兩個男生都是20歲 以上的) 所以會有count(*)select count(*),classid,name from t1 where sex='男' group by classid,age having age > 20; 2 學校評選先進學生,要求平均成績大于等于90分的學生,并且語文課必須在95分以上,請列出有資格的學生 (數(shù)據(jù)表: score)select classid,name from sc where (yw+sx+en)/3 >= 90 and yw>=95 group by classid,name;select classid,group_concat(name) from sc where (yw+sx+en)/3>=90 and yw>=95 group by classid;3 用一條sql語句查詢出每門課都大于80分的學生姓名 (數(shù)據(jù)表: courseinfo)select name from courseinfo group by name having min(score) > 80;select DISTINCT name from courseinfo where name not in (select distinct name from courseinfo where score < 80);22.創(chuàng)建視圖視圖是一個虛擬表,其內容由查詢定義。同真實的表一樣,視圖包含一系列帶有名稱的列和行數(shù)據(jù)。但是,視圖并不在數(shù)據(jù)庫中以存儲的數(shù)據(jù)值集形式存在。行和列數(shù)據(jù)來自由定義視圖的查詢所引用的表,并且在引用視圖時動態(tài)生成。如果你想創(chuàng)建視圖的話 首先你的數(shù)據(jù)庫要有創(chuàng)建視圖的權限,不然的話是創(chuàng)建不成功的create view v1 as select * from t1 where id > 1;創(chuàng)建v1的視圖 把查詢t1表里面的符合條件的數(shù)據(jù)放到v1里面注意:如果主表不存在 那么視圖表失效刪除視圖表drop view v1;23.mysql內置函數(shù)字符串鏈接 concat('name','id');最大值 max('price')最小值 min('price')平均值 avg('price')定義變量 set @a = 10獲取變量的值 select @a計算 select @a + @b; select 10 + 20字符串轉換成大寫和小寫 select ucase('name') select lcase('NAME')計算字符串的長度 select length('namesss') as len;select * from user where length(name) > 5; 名字大于5的長度找出來去除兩側的空白 select trim(' pass ');select length(trim(' over '));隨機數(shù) 從0-1之間的隨機數(shù) select rand();獲取0-10之間的隨機整數(shù)向上取整 select ceil(rand()*10)獲取時間戳 select unix_timestamp();24.mysql預處理**設置一個占位符prepare 名字 from "select * from t2 where id > ?";設置變量set @id = 2;執(zhí)行execute 名字 using @id;**設置多個占位符prepare 名字 from "select * from t2 where id > ? and id < ?";設置變量set @id = 2;set @id1 = 5;執(zhí)行execute 名字 using @id,$id1;25.mysql存儲過程簡單的說,就是一組SQL語句集,功能強大,可以實現(xiàn)一些比較復雜的邏輯功能MySQL存儲過程的創(chuàng)建創(chuàng)建的格式:CREATE PROCEDURE 過程名 ([過程參數(shù)[,...]])[特性 ...]過程體舉個例子:**修改結束符mysql-> delimiter //**創(chuàng)建mysql-> create procedure proc() -> begin-> select * from user; ->end// **使用mysql-> call proc()//**修改結束符 mysql-> delimiter ;注意:(1)這里需要注意的是DELIMITER //和DELIMITER ;兩句,DELIMITER是分割符的意思,因為MySQL默認以";"為分隔符,如果我們沒有聲明分割符,那么編譯器會把存儲過程當成SQL語句進行處理,則存儲過程的編譯過程會報錯,所以要事先用DELIMITER關鍵字申明當前段分隔符,這樣MySQL才會將";"當做存儲過程中的代碼,不會執(zhí)行這些代碼,用完了之后要把分隔符還原。格式:\d // 把結束符的分號改成 //結束本條sql語句\d 和 delimiter 是一樣的 都可以修改mysql的結束符(2)過程體的開始與結束使用BEGIN與END進行標識(3)存儲過程根據(jù)需要可能會有輸入輸出參數(shù),輸入?yún)?shù)x 類型int型 ,輸出參數(shù)num 類型是int型,如果有多個參數(shù)用","分割開。create procedure lamp172(in x int,out num int)-> begin-> set num = x + 20;-> end//調用 call lamp172(10,@s)//select @s as num//使用循環(huán)插入數(shù)據(jù)create procedure total(num int(10))->begin->set @i = 0;->while @i<num do->insesrt into t1 values (null,'www',20);->set @i = @i+1;->end while;->end//注意 : 寫存儲過程名字的時候 不要使用關鍵字 和 數(shù)字例如:create procedure 111() 數(shù)字錯誤create procedure add() 關鍵字錯誤刪除存儲名drop procedure if exists 存儲名;26.mysql觸發(fā)器 trigger 在t2表插入數(shù)據(jù)的同時使用trigger 修改其他表的信息格式: create trigger 名字 after/before insert/update/delete on 表名 for each row begin語句end例如:create trigger tg_1 before insert on t2 for each rowbegininsert into t3(name) values (new.name);end//插入數(shù)據(jù)insert into t3(name) values ('wwww')//create trigger tg_2 after update on t1 for each rowbeginupdate t2 set t2.name= new.name where t2.id = old.id;end//update t1 set name = 'xxoo' where id = 2//create trigger tg_3 after delete on t1 for each rowbegindelete from t2 where id = old.gid; // old.gid 這里面的gid是t1表與t2表相關的id號end//delete from t1 where gid = 2// gid=2 ====> old.gid//查看觸發(fā)器show triggers;//刪除觸發(fā)器drop trigger 名字;27.mysql 讀寫鎖 讀鎖: 所有的終端都可以讀取數(shù)據(jù) 但是不能操作數(shù)據(jù)(增 刪 改)lock table t2 read解鎖unlock tables;寫鎖: 當前的終端對表進行寫鎖之后,代表本終端可以進行讀寫操作,但別的終端不可以進行讀寫操作lock table t2 write; 解鎖unlock tables;28.mysql表數(shù)據(jù)的備份與恢復操作 使用SQL語句備份和恢復你可以使用SELECT INTO OUTFILE語句備份數(shù)據(jù),并用LOAD DATA INFILE語句恢復數(shù)據(jù)。 這種方法只能導出數(shù)據(jù)的內容,不包括表的結構,如果表的結構文件損壞,你必須要先恢復原來的表的結構。格式:備份操作SELECT * INTO {OUTFILE | DUMPFILE} 'file_name' FROM tbl_name恢復操作LOAD DATA [LOW_PRIORITY] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE tbl_name舉例:導出select * from t2 into outfile '../lamp185.txt'; //相對于mysql.exe文件的路徑 清空truncate table t2;導入load data infile '../lamp185.txt' into table t2; 29.增量備份一種mysql操作記錄的備份(1)操作修改配置文件my.ini[mysqld]log-bin=c:/mysql/mysql-log #(這里是備份文件存放的位置) 文件夾要存在才行expire_logs_days=7 # binlog過期清理時間 單位為天binlog_format = MIXED #binlog日志格式(2)查看可以使用mysql的命令進行查看增量備份中的內容mysqlbinlog c:/mysql/mysql-log.000001(3)數(shù)據(jù)恢復通過時間: mysqlbinlog --stop-datetime="2016-10-10 10:20:30" c:/mysql/mysql-log.000001 | mysql -uroot -p通過位置: mysqlbinlog --stop-position="150" c:/mysql/mysql-log.000001 | mysql -uroot -p#171208 15:50:06 server id 1 end_log_pos 2227 CRC32 0x7ff79ffa這里面的171208 15:50:06 時間是上面的datetime的時間這里面的end_log_pos 2227 是上面 position="150"(4)重新記錄增量備份reset master 在mysql命令行中進行輸入30.查詢處理語句分析 explain指令格式:explain select * from usertype 類型快慢順序system> const >eq_ref > ref > fulltext >ref_or_null >index_merge > unique_subquery > index_subquery > range>index > allconst:表中滿足條件的記錄最多一條,通常會出現(xiàn)在主鍵和unique索引中explain select * from test where id=1explain select * from test_2 where name = 'zhangsan'eq_ref:對于每個來自于前面的表的行組合,從該表中讀取一行。這可能是最好的聯(lián)接類型,除了const類型。它用在一個索引的所有部分被聯(lián)接使用并且索引是unique或primary keyexplain select type.name,typechild.childname from type,typechild where type.id = typechild.pid;ref:使用普通索引進行查詢explain select * from test where name = 'abc';ref_or_null:通過普通索引檢索,并且會檢索null值explain select * from test_2 where nickname = 'zhangsan' or nickname = null;range :當查詢范圍的時候explain select * from test where id < 3;或explain select * from test where id in (1,2,3);或explain select * from test where id between 1 and 3;index:跟all一樣,不過只會掃描索引explain select id from test;all:全表掃描explain select * from test注意:查詢一定或者盡量不要出現(xiàn)all類型(全表掃描,速度是最慢的)key(代表的是搜索時使用到的索引名)31.開啟慢查詢記錄需要進行配置 在mysql中的my.ini中進行設置slow-query-log = 1 # 開啟慢查詢操作slow-query-log-file = slow.log # 文件默認存放在mysql的data目錄下 (文件名自定義)long-query-time = 1 # 超時時間定為1秒 如果執(zhí)行的sql語句超過1秒鐘就把語句存放到slow.log文件中 (不能使用小數(shù))# 菜鳥教程.鏈接python的一個網(wǎng)站: www.runoob.com

  

轉載于:https://www.cnblogs.com/w-zc/p/8029115.html

總結

以上是生活随笔為你收集整理的Mysql的操作应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。