mysql事務、索引
阿新 • • 發佈:2018-08-05
chan uniq art 索引 nal ext 必須 length 如果 mysl事務(innodb支持事務處理)
在Mysql控制臺使用事務來操作
mysql> begin; #開始一個事務
mysql> insert into a (a) values(555);
mysql>rollback; 回滾 , 這樣數據是不會寫入的
當然如果上面的數據沒問題,就輸入commit提交命令就行;
mysql> use oldboydb
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> begin; (開啟事務)
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | ZhanYang | 2018-06-20 | NULL |
| 4 | LiuJia | 2018-05-30 | NULL |
| 8 | JiaLiu | 2018-06-20 | NULL |
| 9 | JiaLiu | 2018-04-20 | NULL |
| 10 | gaoyf | 2018-04-20 | NULL |
| 11 | zhujh | 2018-04-20 | NULL |
| 12 | zhouha | 2018-04-20 | NULL |
| 13 | hanzb | 2018-04-21 | M |
+----+----------+---------------+------+
8 rows in set (0.00 sec)
mysql> insert into student (name,register_date,sex) values("Alex","2016-05-21","M");
Query OK, 1 row affected (0.00 sec)
mysql> mysql> insert into student (name,register_date,sex) values("Jack","2016-05-21","M");
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | ZhanYang | 2018-06-20 | NULL |
| 4 | LiuJia | 2018-05-30 | NULL |
| 8 | JiaLiu | 2018-06-20 | NULL |
| 9 | JiaLiu | 2018-04-20 | NULL |
| 10 | gaoyf | 2018-04-20 | NULL |
| 11 | zhujh | 2018-04-20 | NULL |
| 12 | zhouha | 2018-04-20 | NULL |
| 13 | hanzb | 2018-04-21 | M |
| 14 | Alex | 2016-05-21 | M |
| 15 | Jack | 2016-05-21 | M |
+----+----------+---------------+------+
10 rows in set (0.00 sec)
mysql> rollback; (回滾)
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | ZhanYang | 2018-06-20 | NULL |
| 4 | LiuJia | 2018-05-30 | NULL |
| 8 | JiaLiu | 2018-06-20 | NULL |
| 9 | JiaLiu | 2018-04-20 | NULL |
| 10 | gaoyf | 2018-04-20 | NULL |
| 11 | zhujh | 2018-04-20 | NULL |
| 12 | zhouha | 2018-04-20 | NULL |
| 13 | hanzb | 2018-04-21 | M |
+----+----------+---------------+------+
8 rows in set (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student (name,register_date,sex) values("ZhanXing","2018-05-21","M");
Query OK, 1 row affected (0.00 sec)
mysql> insert into student (name,register_date,sex) values("XingYan","2018-05-21","M");
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | ZhanYang | 2018-06-20 | NULL |
| 4 | LiuJia | 2018-05-30 | NULL |
| 8 | JiaLiu | 2018-06-20 | NULL |
| 9 | JiaLiu | 2018-04-20 | NULL |
| 10 | gaoyf | 2018-04-20 | NULL |
| 11 | zhujh | 2018-04-20 | NULL |
| 12 | zhouha | 2018-04-20 | NULL |
| 13 | hanzb | 2018-04-21 | M |
| 16 | ZhanXing | 2018-05-21 | M |
| 17 | XingYan | 2018-05-21 | M |
+----+----------+---------------+------+
10 rows in set (0.00 sec)
mysql> commit; (從內存寫入磁盤)
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | ZhanYang | 2018-06-20 | NULL |
| 4 | LiuJia | 2018-05-30 | NULL |
| 8 | JiaLiu | 2018-06-20 | NULL |
| 9 | JiaLiu | 2018-04-20 | NULL |
| 10 | gaoyf | 2018-04-20 | NULL |
| 11 | zhujh | 2018-04-20 | NULL |
| 12 | zhouha | 2018-04-20 | NULL |
| 13 | hanzb | 2018-04-21 | M |
| 16 | ZhanXing | 2018-05-21 | M |
| 17 | XingYan | 2018-05-21 | M |
+----+----------+---------------+------+
10 rows in set (0.00 sec)
MySQL索引:
創建索引
這是最基本的索引,它沒有任何限制。它有以下幾種創建方式:
CREATE INDEX indexName ON mytable(username(length));
如果是CHAR,VARCHAR類型,length可以小於字段實際長度;如果是BLOB和TEXT類型,必須指定 length。
修改表結構
ALTER mytable ADD INDEX [indexName] ON (username(length))
創建表的時候直接指定
CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL,
INDEX [indexName] (username(length))
);
刪除索引的語法
DROP INDEX [indexName] ON mytable;
mysql> mysql> show index from student; (查看索引)
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 10 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> create index index_name on student (name(32)); (創建索引)
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 10 | NULL | NULL | | BTREE | | |
| student | 1 | index_name | 1 | name | A | 10 | NULL | NULL | | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> drop index index_name on student; (刪除索引)
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from student;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 10 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
唯一索引:
它與前面的普通索引類似,不同的就是:索引列的值必須唯一,但允許有空值。
如果是組合索引,則列值的組合必須唯一。它有以下幾種創建方式:
創建索引
CREATE UNIQUE INDEX indexName ON mytable(username(length))
修改表結構
ALTER mytable ADD UNIQUE [indexName] ON (username(length))
創建表的時候直接指定
CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL,
UNIQUE [indexName] (username(length))
);
使用ALTER 命令添加和刪除索引
有四種方式來添加數據表的索引:
ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): 該語句添加一個主鍵,這意味著索引值必須是唯一的,且不能為NULL。
ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): 這條語句創建索引的值必須是唯一的(除了NULL外,NULL可能會出現多次)。
ALTER TABLE tbl_name ADD INDEX index_name (column_list): 添加普通索引,索引值可出現多次。
ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list):該語句指定了索引為 FULLTEXT ,用於全文索引。
以下實例為在表中添加索引。
mysql> ALTER TABLE testalter_tbl ADD INDEX (c);
你還可以在 ALTER 命令中使用 DROP 子句來刪除索引。嘗試以下實例刪除索引:
mysql> ALTER TABLE testalter_tbl DROP INDEX (c);
mysql事務、索引