您好,登錄后才能下訂單哦!
●在一本書中,無須閱讀整本書,利用目錄就可以快速查找所需信息
●書中的目錄是一個詞語列表,其中注明了包含各個詞的頁碼
●在數據庫中,索引使數據庫程序無須對整個表進行掃描,就可以在其中找到所需數據
●數據庫中的索引是某個表中一列或者若干列值的集合,以及物理標識這些值的數據頁的邏輯指針清單
1、設置了合適的索引之后,數據庫利用各種快速的定位技術,能夠大大加快查詢速率
2、特別是當表很大時,或者查詢涉及到多個表時,使用索引可使查詢加快成干倍
3、可以降低數據庫的10成本,并且索引還可以降低數據庫的排序成本
4、通過創建唯一性索引保證數據表數據的唯一性
5、可以加快表與表之間的連接
6、在使用分組和排序時,可大大減少分組和排序時間
●這是最基本的索引類型,而且它沒有唯一性之 類的限制
●這種索引和前面的“普通索引”基本相同,但有一個區別:索引列的所有值都只能出現一次,即必須唯一
●主鍵是一種唯一性索引, 但它必須指定為"PRIMARY KEY"
●MySQL從3.23.23版開始支持全文索引|和全文檢索。在MySQL中全文索引|的索引|類型為FULLTEXT,全文索引可以在VARCHAR或者TEXT類型的列上創建
●索引可以是單列上創建的索引,也可以是在多列上創建的索引
1、表的主鍵、外鍵必須有索引
2、數據量超過300行的表應該有索引
3、經常與其他表進行連接的表,在連接字段.上應該建立索引
4、唯一性太差的字段不適合建立索引
5、更新太頻繁地字段不適合創建索引
6、經常出現在Where子句中的字段,特別是大表的字段,應該建立索引
7、索引應該建在選擇性高的字段上
8、索引應該建在小字段上,對于大的文本字段甚至超長字段,不要建索引
■(1)創建普通索引
CREATE INDEX <索引的名字> ON tablename (列的列表);
■普通索引例子
CREATE INDEX salary index ON IT salary(薪資);
■(2)創建唯一性索引
CREATE UNIQUE INDEX <索引的名字> ON tablename (列的列表);
■創建唯一性索引例子
CREATE UNIQUE INDEX salary_unique_index ON IT_salary(姓名);
■(3)創建主鍵索引
CREATE TABLE tablename ( [..], PRIMARY KEY (列的列表) );
ALTER TABLE tablename ADD PRIMARY KEY (列的列表);
■主鍵索引例子
ALTER TABLE IT_ salary ADD PRIMARY KEY (員工ID);
1.創建表 直接定義
2.create index 索引名稱 on 表名 (列表1,列表2);
3.alter table 表名 add index 索引名稱 (列表);
1、事務是一 種機制、 一個操作序列,包含了一組數據庫操作命令,并且把所有的命令作為一個整體一起向系統提交或撤銷操作請求,即這一組數據庫命令要么都執行 ,要么都不執行
2、事務是一個不可分割的工作邏輯單元,在數據庫系統上執行并發操作時,事務是最小的控制單元
3、適用于多用戶同時操作的數據庫系統的場景,如銀行、保險公司及證券交易系統等等
4、通過事務的整體性以保證數據的一致性
●事務是一個完整的操作,事務的各元素是不可分的(原子的)
●事務中的所有元素必須作為一個整體提交或回滾
●如果事務中的任何元素失敗,則整個事務將失敗
●當事務完成時,數據必須處于一致狀態:在事務開始之前,數據庫中存儲的數據處于一致狀態;在正在進行的事務中,數據可能處于不一致的狀態;當事務成功完成時,數據必須再次回到已知的一致狀態
●對數據進行修改的所有并發事務是彼此隔離的,這表明事務必須是獨立的,它不應以任何方式依賴于或影響其他事務
●修改數據的事務可以在另一個使用相同數據的事務開始之前訪問這些數據,或者在另一個使用相同數據的事務結束之后訪問這些數據
●事務持久性指不管系統是否發生故障,事務處理的結果都是永久的
●一旦事務被提交,事務的效果會被永久地保留在數據庫中
1、默認情況下MySQL的事務是自動提交的,當sq|語句提交時事務便自動提交
2、手動對事務進行控制的方法
●事務處理命令控制
●使用set設置事務處理方式
1、事務處理命令控制事務
●begin:開始一個事務
●commit:提交一個事務
●rollback:回滾一個事務
2、使用set命令進行控制
●set autocommit=0:禁止自動提交
●set autocommit=1:開啟自動提交
savepoint 打標記/存檔
1、MySQL中的數據用各種不同的技術存儲在文件中,每種技術都使用不同的存儲機制、索引技巧、鎖定水平并最終提供不同的功能和能力,這些不同的技術以及配套的功能在MySQL 中稱為存儲引擎
2、存儲引擎就是MySQL將數據存儲在文件系統中的存儲方式或者存儲格式
3、目前MySQL常用的兩種存儲引擎
MyISAM(輕量級)
InnoDB
4、MySQL存儲引擎是MySQL數據庫服務器中的組件,負責為數據庫執行實際的數據1/0操作
5、使用特殊存儲引擎的主要優點之一在于,僅需提供特殊應用所需的特性,數據庫中的系統開銷較小,具有更有效和更高的數據庫性能
6、MySQL系統中,存儲引擎處于文件系統之上,在數據保存到數據文件之前會傳輸到存儲引擎,之后按照各個存儲引擎的存儲格式進行存儲
1、MyISAM存儲引擎是MySQL關系數據庫系統5.5版本之前默認的存儲引擎,前身是ISAM
2、ISAM是一個定義明確且歷經時間考驗的數據表格管理方法,在設計之時就考慮到數據庫被查詢的次數要遠大于更新的次數
3、ISAM的特點
●ISAM執行讀取操作的速度很快,
●它不支持事務處理
●而且不占用大量的內存和存儲資源
●不能夠容錯
4、數據存儲位置:
表定義文件
表數據存儲文件
表索引文件
1、不支持事務
2、表級鎖定形式,數據在更新時鎖定整個表
3、數據庫在讀寫過程中相互阻塞
●會在數據寫入的過程阻塞用戶數據的讀取
●也會在數據讀取的過程中阻塞用戶的數據寫入
4、可通過key_buffer_size來設置緩存索引,提高訪問性能,減少磁盤IO的壓力
●但緩存只會緩存索引文件,不會緩存數據
5、采用MyISAM存儲引擎數據單獨寫入或讀取,速度過程較快且占用資源相對少
6、MyISAM存儲引擎它不支持外鍵約束,只支持全文索引
7、每個MyISAM在磁盤.上存儲成三個文件,每- -個文件的名字以表的名字開始,擴展名指出文件類型
8、MyISAM在磁盤.上存儲的文件
●.frm文件存儲表定義
●數據文件的擴展名為.MYD (MYData)
●索引文件的擴展名是.MYI (MYIndex)
1、公司業務不需要事務的支持
2、一般單方面讀取數據比較多的業務,或單方面寫入數據比較多的業務
3、MyISAM存儲引擎數據讀寫都比較頻繁場景不適合使用讀寫并發訪問相對較低的業務
4、數據修改相對較少的業務
5、對數據業務-致性要求不是非常高的業務服務器硬件資源相對比較差
1、支持事務:支持4個事務隔離級別
2、行級鎖定,但是全表掃描仍然會是表級鎖定讀寫阻塞與事務隔離級別相關
3、具有非常高效的緩存特性:能緩存索引,也能緩存數據
4、表與主鍵以簇的方式存儲
5、支持分區、表空間,類似oracle數據庫
6、支持外鍵約束,5.5以前不支持全文索引,5.5版本以后支持全文索引
7、對硬件資源要求還是比較高的場合
1、業務需要事務的支持
2、行級鎖定對高并發有很好的適應能力,但需確保查詢是通過索引來完成
3、業務數據更新較為頻繁的場景,如:論壇,微博等
4、業務數據一致性要求較高,例如:銀行業務
5、硬件設備內存較大,利用Innodb較好的緩存能力來提高內存利用率,減少磁盤I0的壓力
1、需要考慮每個存儲引擎提供了哪些不同的核心功能及應用場景
2、支持的字段和數據類型
●所有引|擎都支持通用的數據類型
●但不是所有的引擎都支持其它的字段類型,如二進制對象
3、鎖定類型:不同的存儲引擎支持不同級別的鎖定
●表鎖定
●行鎖定
1、在企業中選擇好合適的存儲引擎之后,就可以進行修改了
2、修改步驟
查看數據庫可配置的存儲引擎
查看表正在使用的存儲引擎
配置存儲引擎為所選擇的類型
3、使用show engines查看系統支持的存儲引擎
4、查看表使用的存儲引擎
方法1: show table status from 庫名 where name='表名;
方法2: show create table 表名;
1、方法1: alter table修改;
alter table table_ name engine=引擎;
2、方法2:修改my.cnf,指定默認存儲引擎并重啟服務
default-storage-engine=InnDB
3、方法3: create table創建表時指定存儲引擎
create table 表名 (字段) engine= 引擎
4、方法4: Mysql convert table format 轉化存儲引擎
Mysql convert table format -user=root -password=密碼
[root@master2 ~]# mysql -uroot -p ##進入數據庫
Enter password: ##輸入密碼
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database school; ##創建數據庫school
Query OK, 1 row affected (0.00 sec)
mysql> use school; ##使用數據庫
Database changed
mysql> create table info( ##創建表
-> id int(4) not null primary key auto_increment, ##設置主鍵,自動增加
-> name varchar(10) not null, ##名字類型varchar不為空
-> address varchar(50) default 'nanjing', ##默認南京
-> age int(3) not null);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into info (name,address,age) values ('zhangsan','beijing',20),('lisi','shanghai',22);
##插入數據
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from info; ##查看表內容
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)
mysql> desc info; ##查看表結構
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| address | varchar(50) | YES | | nanjing | |
| age | int(3) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> create index index_age on info (age); ##創建普通索引
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from info; ##查看表中的索引
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| info | 1 | index_age | 1 | age | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> drop index index_age on info; ##刪除表中的索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from info; ##查看表中的索引
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.01 sec)
mysql> create unique index unique_name on info (name); ##創建唯一性索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from info; ##查看表中索引
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| info | 0 | unique_name | 1 | name | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> drop index unique_name on info; ##刪除表中的索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from info; ##查看表中的索引
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> alter table info add unique index index_name (name); ##使用alter插入表索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from info; ##查看表中的索引
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| info | 0 | index_name | 1 | name | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> create table user( ##創建user表
-> id int(4) not null primary key auto_increment, ##設置主鍵和自動增加
-> name varchar(10) not null,
-> score decimal not null,
-> hobby int(2) not null default '1', ##默認1
-> index index_score (score)); ##設置索引score
Query OK, 0 rows affected (0.01 sec)
mysql> desc user; ##查看表結構
+-------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| score | decimal(10,0) | NO | MUL | NULL | |
| hobby | int(2) | NO | | 1 | |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> insert into user (name,score,hobby) values ('test01',88,1),('stu01',99,2),('wangwu',77,3);
##向表中插入數據
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from user; ##查看表內容
+----+--------+-------+-------+
| id | name | score | hobby |
+----+--------+-------+-------+
| 1 | test01 | 88 | 1 |
| 2 | stu01 | 99 | 2 |
| 3 | wangwu | 77 | 3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)
mysql> create table hob( ##創建hob表
-> id int(2) not null primary key,
-> hob_name varchar(10) not null);
Query OK, 0 rows affected (0.00 sec)
mysql> desc hob; ##查看表結構
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(2) | NO | PRI | NULL | |
| hob_name | varchar(10) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into hob (id,hob_name) values (1,'看書'),(2,'運動'),(3,'聽歌'); ##插入表數據
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from hob; ##查看表內容
+----+----------+
| id | hob_name |
+----+----------+
| 1 | 看書 |
| 2 | 運動 |
| 3 | 聽歌 |
+----+----------+
3 rows in set (0.00 sec)
mysql> select * from user inner join hob on user.hobby=hob.id; ##關聯user和hob兩張表
+----+--------+-------+-------+----+----------+
| id | name | score | hobby | id | hob_name |
+----+--------+-------+-------+----+----------+
| 1 | test01 | 88 | 1 | 1 | 看書 |
| 2 | stu01 | 99 | 2 | 2 | 運動 |
| 3 | wangwu | 77 | 3 | 3 | 聽歌 |
+----+--------+-------+-------+----+----------+
3 rows in set (0.00 sec)
mysql> select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
##去除其他內容顯示name和hob_name內容
+--------+----------+
| name | hob_name |
+--------+----------+
| test01 | 看書 |
| stu01 | 運動 |
| wangwu | 聽歌 |
+--------+----------+
3 rows in set (0.00 sec)
mysql> select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id; ##設置簡易名稱
+--------+----------+
| name | hob_name |
+--------+----------+
| test01 | 看書 |
| stu01 | 運動 |
| wangwu | 聽歌 |
+--------+----------+
3 rows in set (0.00 sec)
mysql> create view view_user as select u.name,h.hob_name from user u inner join hob h on u.hobby
##創建視圖
Query OK, 0 rows affected (0.00 sec)
mysql> select * from view_user; ##查看視圖
+--------+----------+
| name | hob_name |
+--------+----------+
| test01 | 看書 |
| stu01 | 運動 |
| wangwu | 聽歌 |
+--------+----------+
3 rows in set (0.00 sec)
mysql> update user set hobby=3 where name='test01'; ##修改user表中內容
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from view_user; ##查看視圖,即視圖就是表的一個鏈接
+--------+----------+
| name | hob_name |
+--------+----------+
| stu01 | 運動 |
| test01 | 聽歌 |
| wangwu | 聽歌 |
+--------+----------+
3 rows in set (0.00 sec)
mysql> select * from info; ##查看表內容
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)
mysql> show index from info; ##查看表的索引
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| info | 0 | index_name | 1 | name | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> create fulltext index full_addr on info (address); ##以address創建全文索引
Query OK, 0 rows affected, 1 warning (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show index from info; ##查看表索引
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| info | 0 | index_name | 1 | name | A | 2 | NULL | NULL | | BTREE | | |
| info | 1 | full_addr | 1 | address | NULL | 2 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
mysql> select * from user; ##查看user表內容
+----+--------+-------+-------+
| id | name | score | hobby |
+----+--------+-------+-------+
| 1 | test01 | 88 | 3 |
| 2 | stu01 | 99 | 2 |
| 3 | wangwu | 77 | 3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)
mysql> create index index_name_score on user (name,score); ##創建name和score的組合索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from user; ##查看表索引
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user | 0 | PRIMARY | 1 | id | A | 3 | NULL | NULL | | BTREE | | |
| user | 1 | index_score | 1 | score | A | 3 | NULL | NULL | | BTREE | | |
| user | 1 | index_name_score | 1 | name | A | 3 | NULL | NULL | | BTREE | | |
| user | 1 | index_name_score | 2 | score | A | 3 | NULL | NULL | | BTREE | | |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
mysql> select * from info; ##查看表內容數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)
mysql> insert into info (name,address,age) values ('wangwu','hangzhou',30);##插入數據
mysql> begin; ##開啟事務
Query OK, 0 rows affected (0.00 sec)
mysql> insert into info (name,address,age) values ('zhaoliu','hangzhou',31); ##插入數據
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a; ##設置保存節點a
Query OK, 0 rows affected (0.00 sec)
mysql> select * from info; ##查看表數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 4 | zhaoliu | hangzhou | 31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> insert into info (name,address,age) values ('tianqi','hangzhou',32); ##繼續插入數據
Query OK, 1 row affected (0.00 sec)
mysql> savepoint b; ##設置保存節點b
Query OK, 0 rows affected (0.00 sec)
mysql> insert into info (name,address,age) values ('heiba','hangzhou',32); ##繼續插入數據
Query OK, 1 row affected (0.00 sec)
mysql> select * from info; ##查看表內容
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 4 | zhaoliu | hangzhou | 31 |
| 5 | tianqi | hangzhou | 32 |
| 6 | heiba | hangzhou | 32 |
+----+----------+----------+-----+
6 rows in set (0.00 sec)
[root@master2 ~]# mysql -uroot -p ##進入數據庫
Enter password: ##輸入密碼
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use school; ##使用數據庫
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> select * from info; ##查看表內容,此時并沒有提交
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)
mysql> rollback to b; ##利用回滾到保存節點b
Query OK, 0 rows affected (0.00 sec)
mysql> select * from info; ##查看表數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 4 | zhaoliu | hangzhou | 31 |
| 5 | tianqi | hangzhou | 32 |
+----+----------+----------+-----+
5 rows in set (0.00 sec)
mysql> rollback to a; ##回滾到保存節點a
Query OK, 0 rows affected (0.00 sec)
mysql> select * from info; ##查看表數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 4 | zhaoliu | hangzhou | 31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> rollback; ##回滾到初始,退出事務狀態
Query OK, 0 rows affected (0.00 sec)
mysql> select * from info; ##查看表數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)
mysql> begin; ##開啟事務
Query OK, 0 rows affected (0.00 sec)
mysql> insert into info (name,address,age) values ('heiba','hangzhou',32); ##插入數據
Query OK, 1 row affected (0.00 sec)
mysql> commit; ##提交事務
Query OK, 0 rows affected (0.00 sec)
mysql> select * from info; ##查看表數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 7 | heiba | hangzhou | 32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> select * from info; ##查看表數據
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 7 | heiba | hangzhou | 32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> set autocommit=0; ##設置不自動提交事務
Query OK, 0 rows affected (0.00 sec)
mysql> update info set address='beijing' where name='heiba'; ##修改表數據
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from info; ##查看表信息
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 7 | heiba | beijing | 32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
##另一個終端查看
mysql> select * from info; ##查看表信息,并沒有修改
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 7 | heiba | hangzhou | 32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> set autocommit=1; ##開啟自動提交事務
Query OK, 0 rows affected (0.00 sec)
##另一個終端查看
mysql> select * from info; ##查看表數據,此時就已經修改
+----+----------+----------+-----+
| id | name | address | age |
+----+----------+----------+-----+
| 1 | zhangsan | beijing | 20 |
| 2 | lisi | shanghai | 22 |
| 3 | wangwu | hangzhou | 30 |
| 7 | heiba | beijing | 32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> show engines; ##查看默認存儲引擎innodb
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
mysql> show create table info; ##查看創建的表的存儲引擎innodb
| info | CREATE TABLE "info" (
"id" int(4) NOT NULL AUTO_INCREMENT,
"name" varchar(10) NOT NULL,
"address" varchar(50) DEFAULT 'nanjing',
"age" int(3) NOT NULL,
PRIMARY KEY ("id"),
UNIQUE KEY "index_name" ("name"),
FULLTEXT KEY "full_addr" ("address")
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
[root@localhost ~]# vim /etc/my.cnf ##修改配置文件
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysql.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
default-storage-engine=Myisam ##添加默認存儲引擎為Myisam
[root@master2 ~]# systemctl restart mysqld.service ##重啟MySQL服務
[root@master2 ~]# mysql -uroot -p ##進入數據庫
Enter password: ##輸入密碼
mysql> use school; ##使用數據庫
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 a ( id int ); ##創建一個a表
Query OK, 0 rows affected (0.00 sec)
mysql> show create table a; ##查看表默認的存儲引擎Myisam
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------+
| a | CREATE TABLE "a" (
"id" int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table a engine=innodb; ##修改表的存儲引擎為innodb
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table a; ##查看表的存儲引擎innodb
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------+
| a | CREATE TABLE "a" (
"id" int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。