MySQL個人學習筆記 MySQL個人學習筆記
阿新 • • 發佈:2018-11-10
MySQL個人學習筆記
目錄:
一、資料庫的基本操作
-- 選擇要操作的資料庫 -- world:資料庫名 use world; -- 顯示已存在的資料庫列表 show databases; -- 顯示指定資料庫下的表的資訊 show tables; -- 顯示指定表的列的資訊 -- world.country:資料庫名.表名 show columns from world.country; -- 顯示指定表的索引資訊 -- world.country:資料庫名.表名 show index from world.country; -- 顯示指定資料庫下的表的詳細資訊 -- world:資料庫名 show table status from world; -- 顯示指定資料庫下的表名稱以字母'c'開頭的表的詳細資訊 -- world:資料庫名 show table status from world like 'c%'; -- 顯示資料庫表的結構,如:欄位名,欄位型別等 -- world.country:資料庫名.表名 describe world.country; -- 檢視建立表的SQL語句 -- demo.test:資料庫名.表名 show create table demo.test; -- 檢視建立儲存過程的SQL語句 -- demo.test_proc:資料庫名.儲存過程名 show create procedure demo.test_proc; -- 檢視建立檢視的SQL語句 -- demo.test_view:資料庫名.檢視名 show create view demo.test_view; -- 檢視建立函式的SQL語句 -- demo.test_fun:資料庫名.函式名 show create function demo.test_fun; -- 檢視當前使用者的資料庫許可權 show grants; -- 檢視指定使用者的資料庫許可權 --[email protected]:使用者名稱@訪問主機 show grants for 'admin'@'localhost'; -- 查詢資料庫使用者資訊 select * from mysql.user; -- 獲取伺服器版本資訊 SELECT VERSION(); -- 獲取當前資料庫名 (或者返回空) SELECT DATABASE(); -- 獲取當前使用者名稱 SELECT USER(); -- 獲取伺服器狀態 SHOW STATUS; -- 獲取伺服器配置變數 SHOW VARIABLES; 例如: -- 查詢自增長值的步長,即每次增加多少,預設為1。 show variables like '%auto_increment%'; -- 設定自增長值每次增加的數值,會影響所有資料表。 set auto_increment_increment=3; -- 設定自增長值的起始值,會影響所有資料表。 set auto_increment_offset=100; -- mysql執行在安全模式下時,非主鍵條件下是無法執行update或者delete命令的 -- 檢視安全模式狀態 show variables like '%sql_safe_updates%'; -- 設定安全模式為關閉 set sql_safe_updates=off; -- 獲取最近一次向具有identity屬性(即自增列)的表插入資料時對應的自增列的值,@@identity是系統定義的全域性變數。 select @@identity; -- LAST_INSERT_ID函式將返回當前連線自增列最新的 insert or update 操作生成的第一個記錄的ID。因為其基於Connection的,所以也是執行緒安全的。 select LAST_INSERT_ID();
二、建立、刪除使用者及授權
-- 建立一個新的使用者,並設定登入密碼 -- test:使用者名稱;localhost:本地主機訪問(如果需要其他任意主機訪問,請使用萬用字元'%');123456:使用者密碼; create user 'test'@'localhost' identified by '123456'; -- 建立一個新的使用者,不指定登入密碼,即不需要登入密碼 create user 'test01'@'localhost'; -- 刪除指定的使用者 drop user 'test01'@'localhost'; -- 修改使用者名稱 --[email protected]:要修改的使用者名稱和訪問主機 -- [email protected]%:修改為的使用者名稱和訪問主機 rename user 'test'@'localhost' to 'test'@'%'; -- 修改使用者密碼 -- [email protected]:要修改的使用者名稱和訪問主機 -- 123456:新的使用者密碼 set password for 'test'@'localhost' = Password('123456');-- 授予指定使用者'test'對於'world'資料庫下'country'表的查詢許可權 -- select:查詢許可權;world.country:資料庫名.表名;'test'@'localhost':使用者名稱@訪問主機; grant select on world.country to 'test'@'localhost'; -- 立即啟用修改(預設再次登入才會生效) flush privileges; -- 撤銷指定使用者'test'對於'world'資料庫下'country'表的查詢許可權 -- select:查詢許可權;world.country:資料庫名.表名;'test'@'localhost':使用者名稱@訪問主機; revoke select on world.country from 'test'@'localhost'; -- 立即啟用修改(預設再次登入才會生效) flush privileges; -- 授予指定使用者'test'對於'world'資料庫下所有表的查詢、新增、修改、刪除許可權 grant select,insert,update,delete on world.* to 'test'@'localhost'; -- 撤銷指定使用者'test'對於'world'資料庫下所有表的查詢、新增、修改、刪除許可權 revoke select,insert,update,delete on world.* from 'test'@'localhost'; -- 授予指定使用者'test'對於'world'資料庫下所有表的表結構進行建立、修改、刪除許可權 grant create,alter,drop on world.* to 'test'@'localhost'; -- 撤銷指定使用者'test'對於'world'資料庫下所有表的表結構進行建立、修改、刪除許可權 revoke create,alter,drop on world.* from 'test'@'localhost'; -- 授予指定使用者'test'對於'world'資料庫下所有儲存過程的執行許可權,並且該使用者有許可權轉授予其他使用者 grant execute on world.* to 'test'@'localhost' with grant option; -- 撤銷指定使用者'test'對於'world'資料庫下所有儲存過程的執行許可權,轉授予許可權一併撤銷 revoke execute on world.* from 'test'@'localhost';
三、資料庫字元校對集
字元校對集,即排序規則,在某個字符集的情況下,字符集的排列順序應該是什麼,稱之為校對集。
-- 檢視所有的字元校對集 -- 字尾為_bin:表示基於二進位制編碼的直接比較 -- 字尾為_ci:表示對大小寫不敏感的比較 -- 字尾為_cs:表示對大小寫敏感的比較 show collation;
四、建立、刪除資料庫和表
-- 建立一個名為'test'的資料庫 create database test; -- 建立一個名為'test'的資料庫,如果該資料庫已存在則不建立,否則再建立 -- 並指定預設字符集為'utf8',字元校對集為'utf8_general_ci' create database if not exists test default charset utf8 collate utf8_general_ci; -- 刪除名為'test'的資料庫 drop database test; -- 建立一個名為'Student'的資料表,如果該資料表已存在則不建立,否則再建立 -- engine:指定資料庫引擎為'InnoDB' -- auto_increment:指定自增列的起始值為1 create table if not exists Student ( ID int not null auto_increment, #自動增長列 StuNo varchar(32) not null, StuName varchar(8) not null, StuSex varchar(8) null, StuBirthday tinyint null, CreateTime datetime null, primary key (ID) #指定主鍵列 ) engine=InnoDB auto_increment=1 default charset=utf8 collate=utf8_general_ci; -- 刪除資料表 student,該操作會刪除所有資料包括表結構、檢視、索引、約束等。 drop table test.student; -- 刪除資料表中的所有資料,該操作會刪除表中所有的資料,但是會保留表結構、檢視、索引、約束等。 truncate table test.student; -- 建立一個臨時表,臨時表的建立與資料表的建立類似,只不過需要新增關鍵字 temporary。 -- 臨時表的作用域為當前會話,即當前連線可見,當斷開當前連線時會自動銷燬,當然也可以手動刪除,刪除方式與資料表一樣。 create temporary table Product ( ProName varchar(32) not null, Price decimal(10,3) not null default 0.000 ); -- 複製指定資料表的表結構到建立的新表。 create table test.StudentBak like test.student; -- 複製指定資料表的表結構及所有資料到建立的新表。 create table test.StudentBak select * from test.student;
五、DML操作
-- 向資料表中插入資料 insert into student(StuNo,StuName,StuSex,Stubirthday,CreateTime) select 'A001','小張','男',str_to_date('1988-06-09','%Y-%m-%d'),current_timestamp() union all select 'A002','小紅','女',str_to_date('1990-08-10','%Y-%m-%d'),current_timestamp() -- 在插入重複的資料時,會直接跳過重複資料的插入。在有自增列或主鍵的資料表中不起作用,因為自增列和主鍵都具有唯一性。 insert ignore into test.student(stuno,stuname,stusex,stubirthday,createtime) values ('A003','小魚','女','1991-07-07',current_timestamp()); -- MySQL的WHERE子句預設是不區分大小寫的,如果需要區分大小寫,就要在欄位前加上關鍵字 binary select * from student where stuno='a001'; #'1', 'A001', '小張', '男', '1988-06-09', '2018-01-12 12:17:00' select * from student where binary stuno='a001'; #null -- limit:用於設定返回的記錄數。 -- offset:用於設定select語句開始查詢的資料偏移量,預設為零。 -- 表示只取前10條資料 select * from world.city limit 10; -- 表示躍過5條,從第6條資料開始取10條資料。 select * from world.city limit 10 offset 5; -- 表示從第10條開始取5條資料。 select * from world.city limit 10,5; -- regexp:用於設定正則表示式匹配項,類似於模糊匹配like。 -- 表示查詢名稱以字元 'A'(不區分大小寫)開頭的記錄。 select * from world.city where Name regexp '^A'; -- 表示查詢名稱中包含字串 'mer' 的記錄。 select * from world.city where Name regexp 'mer'; -- 表示查詢名稱以字元 'a' 或字元 'b' 開頭的記錄或者以字元 'r' 結尾的記錄。 select * from world.city where Name regexp '^[ab]|r$';
六、DDL操作
-- 向指定資料表新增一列,預設新增到資料表字段的末尾。 alter table test.student add column1 varchar(10) null; -- 向指定資料表新增一列,並設定預設值為:0 alter table demo.chinesecharinfo add column IsUseful tinyint unsigned not null default 0; -- first關鍵字用於把新增的列設定為第一列。 alter table test.student add column1 varchar(10) null first; -- after關鍵字用於把新增的列設定在指定列的後面,StuSex為指定列的列名。 alter table test.student add column1 varchar(10) null after StuSex; -- 刪除指定列名的列,當資料表僅剩一個欄位時,無法進行刪除。 alter table test.student drop column1; -- 修改指定列的資料型別,並設定該列位於指定列名的列之後。 alter table test.student modify column1 char(10) null after CreateTime; -- 關鍵字column可省略 alter table test.student modify column column1 char(10) null after CreateTime; -- 修改指定列的列名和資料型別,並設定該列位於指定列名的列之後。 -- column1:為原列名 -- column2:為新的列名 alter table test.student change column1 column2 varchar(10) null after CreateTime; -- 修改指定列的預設值。 alter table test.student alter column2 set default '123'; -- 刪除指定列的預設值。 alter table test.student alter column2 drop default; -- 修改資料表的儲存引擎。 alter table test.student engine = myisam; alter table test.student engine = InnoDB; -- 修改資料表的自增長值的起始值。 alter table test.student auto_increment=10; -- 重建自增長列,當刪除資料過多,自增長列的值比較混亂時可以使用,但是重建時如果有新的資料插入,有可能會出現混亂。 alter table test.student drop ID; alter table test.student add ID int not null auto_increment first; alter table test.student add primary key(ID); -- 修改資料表的表名稱。 alter table test.student rename to test.StudentBak;
七、索引
-- 檢視指定資料表的索引。 show index from test.student; -- 刪除指定的索引。 drop index index_name on test.student; -- 修改表結構的方式刪除索引。 alter table test.student drop index index_name; -- 建立普通索引。 create index index_name on test.student(StuNo); -- 修改表結構的方式新增索引,這種方式可以不指定索引名稱,不指定系統會自動預設一個索引名稱。 alter table test.student add index index_name(StuNo); -- 建立唯一索引,指定建立唯一索引的列的值必須是唯一的,不能重複,但是可以為null。 create unique index index_name on test.student(StuNo); -- 修改表結構的方式新增唯一索引。 alter table test.student add unique index index_name(StuNo); -- 修改表結構的方式新增主鍵,必須保證新增主鍵的列的值不能為null,並且是唯一的,不可重複。 alter table test.student add primary key PrimaryKey_Name(ID); -- 刪除指定資料表的主鍵,刪除主鍵時只需指定 primary key,刪除索引時必須指定索引名。 -- 注意:當主鍵列同時是自增長列時,不能直接刪除主鍵,需要先刪除自增長約束。 alter table test.student drop primary key; -- 新增全文索引。 alter table test.student add fulltext index_name(StuNo); -- 加上關鍵字ignore建立的唯一索引和主鍵,在插入重複資料時,會直接過濾掉重複資料,並且不會報錯,否則就會丟擲錯誤。 alter ignore table test.student add primary key(ID); alter ignore table test.student add unique index index_name(StuNo);
八、事務
-- 關閉自動提交事務 set autocommit=0; -- 開啟自動提交事務,預設為開啟。 set autocommit=1; -- 顯式地開啟一個事務,有以下兩種方法。 start transaction; begin; -- commit用於提交事務,只有當自動提交事務被關閉時需要使用。 commit; -- rollback用於回滾事務,撤銷對於資料庫所做的未提交的操作。 rollback; -- 用於設定一個儲存點,identifier是指儲存點的名稱。 savepoint identifier; -- 用於刪除一個儲存點,如果指定的儲存點不存在,將會丟擲一個異常。 release savepoint identifier; -- 把事務回滾到指定的儲存點。 rollback to identifier; -- 設定事務隔離級別,只對下一個事務有效。 set transaction isolation level {事務隔離級別}; -- 設定事務隔離級別,對當前會話的事務有效。 set session transaction isolation level {事務隔離級別}; -- 設定事務隔離級別,對後面建立MySQL連線的事務有效。 set global transaction isolation level {事務隔離級別}; -- 事務的隔離級別 read uncommitted(讀取未提交): -- 該級別引發的問題是髒讀,會讀取到其他事務未提交的資料。 read committed(讀取已提交): -- 該級別引發的問題是不可重複讀,即設定為該級別的事務只能讀取到其他事務已經提交的資料,未提交的資料不能讀取,會造成多次查詢的結果不一致。 repeatable read(可重複讀): -- 該級別引發的問題是幻讀,即當用戶修改某一範圍內的資料行時,另一個事務又在該範圍內插入了新的行,當用戶再次讀取該範圍內的資料時,會發現有新的資料行沒有被修改。 -- 該級別是MySQL資料庫預設的事務隔離級別。注意:該級別不會對事務查詢到的行加行鎖,也就是該事務查詢到的行,其他事務依然能進行修改,但是能保證資料的一致性。 serializable(可序列化): -- 該級別是MySQL中事務隔離級別最高的,該級別會鎖住事務中操作的整張表,因此不會出現以上三個級別的問題。但是這種隔離級別併發性極地,開發中很少會用到。
目錄:
一、資料庫的基本操作
-- 選擇要操作的資料庫 -- world:資料庫名 use world; -- 顯示已存在的資料庫列表 show databases; -- 顯示指定資料庫下的表的資訊 show tables; -- 顯示指定表的列的資訊 -- world.country:資料庫名.表名 show columns from world.country; -- 顯示指定表的索引資訊 -- world.country:資料庫名.表名 show index from world.country; -- 顯示指定資料庫下的表的詳細資訊 -- world:資料庫名 show table status from world; -- 顯示指定資料庫下的表名稱以字母'c'開頭的表的詳細資訊 -- world:資料庫名 show table status from world like 'c%'; -- 顯示資料庫表的結構,如:欄位名,欄位型別等 -- world.country:資料庫名.表名 describe world.country; -- 檢視建立表的SQL語句 -- demo.test:資料庫名.表名 show create table demo.test; -- 檢視建立儲存過程的SQL語句 -- demo.test_proc:資料庫名.儲存過程名 show create procedure demo.test_proc; -- 檢視建立檢視的SQL語句 -- demo.test_view:資料庫名.檢視名 show create view demo.test_view; -- 檢視建立函式的SQL語句 -- demo.test_fun:資料庫名.函式名 show create function demo.test_fun; -- 檢視當前使用者的資料庫許可權 show grants; -- 檢視指定使用者的資料庫許可權 -- [email protected]:使用者名稱@訪問主機 show grants for 'admin'@'localhost'; -- 查詢資料庫使用者資訊 select * from mysql.user; -- 獲取伺服器版本資訊 SELECT VERSION(); -- 獲取當前資料庫名 (或者返回空) SELECT DATABASE(); -- 獲取當前使用者名稱 SELECT USER(); -- 獲取伺服器狀態 SHOW STATUS; -- 獲取伺服器配置變數 SHOW VARIABLES; 例如: -- 查詢自增長值的步長,即每次增加多少,預設為1。 show variables like '%auto_increment%'; -- 設定自增長值每次增加的數值,會影響所有資料表。 set auto_increment_increment=3; -- 設定自增長值的起始值,會影響所有資料表。 set auto_increment_offset=100; -- mysql執行在安全模式下時,非主鍵條件下是無法執行update或者delete命令的 -- 檢視安全模式狀態 show variables like '%sql_safe_updates%'; -- 設定安全模式為關閉 set sql_safe_updates=off; -- 獲取最近一次向具有identity屬性(即自增列)的表插入資料時對應的自增列的值,@@identity是系統定義的全域性變數。 select @@identity; -- LAST_INSERT_ID函式將返回當前連線自增列最新的 insert or update 操作生成的第一個記錄的ID。因為其基於Connection的,所以也是執行緒安全的。 select LAST_INSERT_ID();
二、建立、刪除使用者及授權
-- 建立一個新的使用者,並設定登入密碼 -- test:使用者名稱;localhost:本地主機訪問(如果需要其他任意主機訪問,請使用萬用字元'%');123456:使用者密碼; create user 'test'@'localhost' identified by '123456'; -- 建立一個新的使用者,不指定登入密碼,即不需要登入密碼 create user 'test01'@'localhost'; -- 刪除指定的使用者 drop user 'test01'@'localhost'; -- 修改使用者名稱 -- [email protected]:要修改的使用者名稱和訪問主機 -- [email protected]%:修改為的使用者名稱和訪問主機 rename user 'test'@'localhost' to 'test'@'%'; -- 修改使用者密碼 -- [email protected]:要修改的使用者名稱和訪問主機 -- 123456:新的使用者密碼 set password for 'test'@'localhost' = Password('123456');-- 授予指定使用者'test'對於'world'資料庫下'country'表的查詢許可權 -- select:查詢許可權;world.country:資料庫名.表名;'test'@'localhost':使用者名稱@訪問主機; grant select on world.country to 'test'@'localhost'; -- 立即啟用修改(預設再次登入才會生效) flush privileges; -- 撤銷指定使用者'test'對於'world'資料庫下'country'表的查詢許可權 -- select:查詢許可權;world.country:資料庫名.表名;'test'@'localhost':使用者名稱@訪問主機; revoke select on world.country from 'test'@'localhost'; -- 立即啟用修改(預設再次登入才會生效) flush privileges; -- 授予指定使用者'test'對於'world'資料庫下所有表的查詢、新增、修改、刪除許可權 grant select,insert,update,delete on world.* to 'test'@'localhost'; -- 撤銷指定使用者'test'對於'world'資料庫下所有表的查詢、新增、修改、刪除許可權 revoke select,insert,update,delete on world.* from 'test'@'localhost'; -- 授予指定使用者'test'對於'world'資料庫下所有表的表結構進行建立、修改、刪除許可權 grant create,alter,drop on world.* to 'test'@'localhost'; -- 撤銷指定使用者'test'對於'world'資料庫下所有表的表結構進行建立、修改、刪除許可權 revoke create,alter,drop on world.* from 'test'@'localhost'; -- 授予指定使用者'test'對於'world'資料庫下所有儲存過程的執行許可權,並且該使用者有許可權轉授予其他使用者 grant execute on world.* to 'test'@'localhost' with grant option; -- 撤銷指定使用者'test'對於'world'資料庫下所有儲存過程的執行許可權,轉授予許可權一併撤銷 revoke execute on world.* from 'test'@'localhost';
三、資料庫字元校對集
字元校對集,即排序規則,在某個字符集的情況下,字符集的排列順序應該是什麼,稱之為校對集。
-- 檢視所有的字元校對集 -- 字尾為_bin:表示基於二進位制編碼的直接比較 -- 字尾為_ci:表示對大小寫不敏感的比較 -- 字尾為_cs:表示對大小寫敏感的比較 show collation;
四、建立、刪除資料庫和表
-- 建立一個名為'test'的資料庫 create database test; -- 建立一個名為'test'的資料庫,如果該資料庫已存在則不建立,否則再建立 -- 並指定預設字符集為'utf8',字元校對集為'utf8_general_ci' create database if not exists test default charset utf8 collate utf8_general_ci; -- 刪除名為'test'的資料庫 drop database test; -- 建立一個名為'Student'的資料表,如果該資料表已存在則不建立,否則再建立 -- engine:指定資料庫引擎為'InnoDB' -- auto_increment:指定自增列的起始值為1 create table if not exists Student ( ID int not null auto_increment, #自動增長列 StuNo varchar(32) not null, StuName varchar(8) not null, StuSex varchar(8) null, StuBirthday tinyint null, CreateTime datetime null, primary key (ID) #指定主鍵列 ) engine=InnoDB auto_increment=1 default charset=utf8 collate=utf8_general_ci; -- 刪除資料表 student,該操作會刪除所有資料包括表結構、檢視、索引、約束等。 drop table test.student; -- 刪除資料表中的所有資料,該操作會刪除表中所有的資料,但是會保留表結構、檢視、索引、約束等。 truncate table test.student; -- 建立一個臨時表,臨時表的建立與資料表的建立類似,只不過需要新增關鍵字 temporary。 -- 臨時表的作用域為當前會話,即當前連線可見,當斷開當前連線時會自動銷燬,當然也可以手動刪除,刪除方式與資料表一樣。 create temporary table Product ( ProName varchar(32) not null, Price decimal(10,3) not null default 0.000 ); -- 複製指定資料表的表結構到建立的新表。 create table test.StudentBak like test.student; -- 複製指定資料表的表結構及所有資料到建立的新表。 create table test.StudentBak select * from test.student;
五、DML操作
-- 向資料表中插入資料 insert into student(StuNo,StuName,StuSex,Stubirthday,CreateTime) select 'A001','小張','男',str_to_date('1988-06-09','%Y-%m-%d'),current_timestamp() union all select 'A002','小紅','女',str_to_date('1990-08-10','%Y-%m-%d'),current_timestamp() -- 在插入重複的資料時,會直接跳過重複資料的插入。在有自增列或主鍵的資料表中不起作用,因為自增列和主鍵都具有唯一性。 insert ignore into test.student(stuno,stuname,stusex,stubirthday,createtime) values ('A003','小魚','女','1991-07-07',current_timestamp()); -- MySQL的WHERE子句預設是不區分大小寫的,如果需要區分大小寫,就要在欄位前加上關鍵字 binary select * from student where stuno='a001'; #'1', 'A001', '小張', '男', '1988-06-09', '2018-01-12 12:17:00' select * from student where binary stuno='a001'; #null -- limit:用於設定返回的記錄數。 -- offset:用於設定select語句開始查詢的資料偏移量,預設為零。 -- 表示只取前10條資料 select * from world.city limit 10; -- 表示躍過5條,從第6條資料開始取10條資料。 select * from world.city limit 10 offset 5; -- 表示從第10條開始取5條資料。 select * from world.city limit 10,5; -- regexp:用於設定正則表示式匹配項,類似於模糊匹配like。 -- 表示查詢名稱以字元 'A'(不區分大小寫)開頭的記錄。 select * from world.city where Name regexp '^A'; -- 表示查詢名稱中包含字串 'mer' 的記錄。 select * from world.city where Name regexp 'mer'; -- 表示查詢名稱以字元 'a' 或字元 'b' 開頭的記錄或者以字元 'r' 結尾的記錄。 select * from world.city where Name regexp '^[ab]|r$';
六、DDL操作
-- 向指定資料表新增一列,預設新增到資料表字段的末尾。 alter table test.student add column1 varchar(10) null; -- 向指定資料表新增一列,並設定預設值為:0 alter table demo.chinesecharinfo add column IsUseful tinyint unsigned not null default 0; -- first關鍵字用於把新增的列設定為第一列。 alter table test.student add column1 varchar(10) null first; -- after關鍵字用於把新增的列設定在指定列的後面,StuSex為指定列的列名。 alter table test.student add column1 varchar(10) null after StuSex; -- 刪除指定列名的列,當資料表僅剩一個欄位時,無法進行刪除。 alter table test.student drop column1; -- 修改指定列的資料型別,並設定該列位於指定列名的列之後。 alter table test.student modify column1 char(10) null after CreateTime; -- 關鍵字column可省略 alter table test.student modify column column1 char(10) null after CreateTime; -- 修改指定列的列名和資料型別,並設定該列位於指定列名的列之後。 -- column1:為原列名 -- column2:為新的列名 alter table test.student change column1 column2 varchar(10) null after CreateTime; -- 修改指定列的預設值。 alter table test.student alter column2 set default '123'; -- 刪除指定列的預設值。 alter table test.student alter column2 drop default; -- 修改資料表的儲存引擎。 alter table test.student engine = myisam; alter table test.student engine = InnoDB; -- 修改資料表的自增長值的起始值。 alter table test.student auto_increment=10; -- 重建自增長列,當刪除資料過多,自增長列的值比較混亂時可以使用,但是重建時如果有新的資料插入,有可能會出現混亂。 alter table test.student drop ID; alter table test.student add ID int not null auto_increment first; alter table test.student add primary key(ID); -- 修改資料表的表名稱。 alter table test.student rename to test.StudentBak;
七、索引
-- 檢視指定資料表的索引。 show index from test.student; -- 刪除指定的索引。 drop index index_name on test.student; -- 修改表結構的方式刪除索引。 alter table test.student drop index index_name; -- 建立普通索引。 create index index_name on test.student(StuNo); -- 修改表結構的方式新增索引,這種方式可以不指定索引名稱,不指定系統會自動預設一個索引名稱。 alter table test.student add index index_name(StuNo); -- 建立唯一索引,指定建立唯一索引的列的值必須是唯一的,不能重複,但是可以為null。 create unique index index_name on test.student(StuNo); -- 修改表結構的方式新增唯一索引。 alter table test.student add unique index index_name(StuNo); -- 修改表結構的方式新增主鍵,必須保證新增主鍵的列的值不能為null,並且是唯一的,不可重複。 alter table test.student add primary key PrimaryKey_Name(ID); -- 刪除指定資料表的主鍵,刪除主鍵時只需指定 primary key,刪除索引時必須指定索引名。 -- 注意:當主鍵列同時是自增長列時,不能直接刪除主鍵,需要先刪除自增長約束。 alter table test.student drop primary key; -- 新增全文索引。 alter table test.student add fulltext index_name(StuNo); -- 加上關鍵字ignore建立的唯一索引和主鍵,在插入重複資料時,會直接過濾掉重複資料,並且不會報錯,否則就會丟擲錯誤。 alter ignore table test.student add primary key(ID); alter ignore table test.student add unique index index_name(StuNo);
八、事務
-- 關閉自動提交事務 set autocommit=0; -- 開啟自動提交事務,預設為開啟。 set autocommit=1; -- 顯式地開啟一個事務,有以下兩種方法。 start transaction; begin; -- commit用於提交事務,只有當自動提交事務被關閉時需要使用。 commit; -- rollback用於回滾事務,撤銷對於資料庫所做的未提交的操作。 rollback; -- 用於設定一個儲存點,identifier是指儲存點的名稱。 savepoint identifier; -- 用於刪除一個儲存點,如果指定的儲存點不存在,將會丟擲一個異常。 release savepoint identifier; -- 把事務回滾到指定的儲存點。 rollback to identifier; -- 設定事務隔離級別,只對下一個事務有效。 set transaction isolation level {事務隔離級別}; -- 設定事務隔離級別,對當前會話的事務有效。 set session transaction isolation level {事務隔離級別}; -- 設定事務隔離級別,對後面建立MySQL連線的事務有效。 set global transaction isolation level {事務隔離級別}; -- 事務的隔離級別 read uncommitted(讀取未提交): -- 該級別引發的問題是髒讀,會讀取到其他事務未提交的資料。 read committed(讀取已提交): -- 該級別引發的問題是不可重複讀,即設定為該級別的事務只能讀取到其他事務已經提交的資料,未提交的資料不能讀取,會造成多次查詢的結果不一致。 repeatable read(可重複讀): -- 該級別引發的問題是幻讀,即當用戶修改某一範圍內的資料行時,另一個事務又在該範圍內插入了新的行,當用戶再次讀取該範圍內的資料時,會發現有新的資料行沒有被修改。 -- 該級別是MySQL資料庫預設的事務隔離級別。注意:該級別不會對事務查詢到的行加行鎖,也就是該事務查詢到的行,其他事務依然能進行修改,但是能保證資料的一致性。 serializable(可序列化): -- 該級別是MySQL中事務隔離級別最高的,該級別會鎖住事務中操作的整張表,因此不會出現以上三個級別的問題。但是這種隔離級別併發性極地,開發中很少會用到。