1. 程式人生 > >MySQL個人學習筆記

MySQL個人學習筆記

https temp 刪除索引 ola 數據庫名 sql語句 偏移量 同時 安全模式

目錄:

  數據庫的基本操作

  創建、刪除用戶及授權

  數據庫字符校對集

  創建、刪除數據庫和表

  DML操作

  DDL操作

  索引

  事務

一、數據庫的基本操作

技術分享圖片
-- 選擇要操作的數據庫
-- 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;

-- 查看指定用戶的數據庫權限
-- admin@localhost:用戶名@訪問主機
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‘;

-- 修改用戶名
-- test@localhost:要修改的用戶名和訪問主機
-- test@%:修改為的用戶名和訪問主機
rename user ‘test‘@‘localhost‘ to ‘test‘@‘%‘;

-- 修改用戶密碼
-- test@localhost:要修改的用戶名和訪問主機
-- 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中事務隔離級別最高的,該級別會鎖住事務中操作的整張表,因此不會出現以上三個級別的問題。但是這種隔離級別並發性極地,開發中很少會用到。
技術分享圖片

MySQL個人學習筆記