1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
mysql> create table t (id
int
, name
var
char(
30
) , msg
var
char(
100
)) engine = MyISAM;
mysql> show table status like
"t"
\G ;
***************************
1
. row ***************************
Name: t
Engine: MyISAM
Version:
10
Row_format: Dynamic
Rows:
0
Avg_row_length:
0
Data_length:
0
Max_data_length:
281474976710655
Index_length:
1024
Data_free:
0
Auto_increment: NULL
Create_time:
2013
-
09
-
12
00
:
39
:
29
Update_time:
2013
-
09
-
12
00
:
39
:
29
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1
row
in
set
(
0.00
sec)
|
1
2
3
4
5
6
7
8
9
10
11
12
|
mysql> create table tt (
id
int auto_increment primary key , name varchar(30) , msg varchar(100)) engine = MyISAM;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into tt(name,msg) values(
'chenzhongyang'
,
'good'
);
Query OK, 1 row affected (0.00 sec)
虽然我们没有指定名字是chenzhongyang的
id
是1,但是有了auto_increment这个参数,系统会自动给他加上1
mysql>
select
* from tt;
+----+---------------+------+
|
id
| name | msg |
+----+---------------+------+
| 1 | chenzhongyang | good |
+----+---------------+------+
1 row
in
set
(0.01 sec)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
mysql> alter table tt auto_increment=
2000
;
Query OK,
1
row affected (
0.02
sec)
Records:
1
Duplicates:
0
Warnings:
0
mysql> insert into tt(name,msg) values(
'tianhongyan'
,
'baby'
);
Query OK,
1
row affected (
0.00
sec)
mysql> select * from tt;
+------+---------------+------+
| id | name | msg |
+------+---------------+------+
|
1
| chenzhongyang | good |
|
2000
| tianhongyan | baby |
+------+---------------+------+
2
rows
in
set
(
0.00
sec)
mysql> insert into tt(name,msg) values(
'zhongguo'
,
'XXXXXXX-YYYYYYYYY-+VVVV'
);
Query OK,
1
row affected (
0.00
sec)
mysql> select * FROM tt;
+------+---------------+-------------------------+
| id | name | msg |
+------+---------------+-------------------------+
|
1
| chenzhongyang | good |
|
2000
| tianhongyan | baby |
|
2001
| zhongguo | XXXXXXX-YYYYYYYYY-+VVVV |
+------+---------------+-------------------------+
3
rows
in
set
(
0.00
sec)
|
1
2
3
4
5
6
7
|
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|
2001
|
+------------------+
1
row
in
set
(
0.00
sec)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
mysql> show
var
iables like
"%autocommit%"
;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1
row
in
set
(
0.00
sec)
mysql>
set
autocommit=OFF ;
Query OK,
0
rows affected (
0.00
sec)
mysql> show
var
iables like
"%autocommit%"
;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1
row
in
set
(
0.00
sec)
mysql>
delete
from tt where id=
1
;
Query OK,
1
row affected (
0.00
sec)
mysql> rollback;
Query OK,
0
rows affected,
1
warning (
0.00
sec)
mysql> select * from tt;
+------+-------------+-------------------------+
| id | name | msg |
+------+-------------+-------------------------+
|
2000
| tianhongyan | baby |
|
2001
| zhongguo | XXXXXXX-YYYYYYYYY-+VVVV |
+------+-------------+-------------------------+
2
rows
in
set
(
0.00
sec)
|
1
2
3
4
5
6
7
|
mysql> show
var
iables like
"%pointer%"
;
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| myisam_data_pointer_size |
6
|
+--------------------------+-------+
1
row
in
set
(
0.00
sec)
|
1
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
如果myisam_data_pointer_size=
2
,那么就意味着一个表的最大数据文件是
65535
/
1024
=64K
mysql>
set
global myisam_data_pointer_size=
2
;
Query OK,
0
rows affected (
0.00
sec)
mysql> show
var
iables like
"%pointer%"
;
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| myisam_data_pointer_size |
2
|
+--------------------------+-------+
1
row
in
set
(
0.00
sec)
我们来创建一个 大表ss
mysql> create table ss select * from information_schema.tables ;
Query OK,
54
rows affected (
0.09
sec)
Records:
54
Duplicates:
0
Warnings:
0
mysql> insert into ss select * from ss;
Query OK,
108
rows affected (
0.01
sec)
Records:
108
Duplicates:
0
Warnings:
0
mysql> insert into ss select * from ss;
Query OK,
216
rows affected (
0.01
sec)
Records:
216
Duplicates:
0
Warnings:
0
这个时候出现了表ss满了的错误,我们看看数据文件 是64K,要想继续可以插入数据,那么就要把这个参数调大
mysql> insert into ss select * from ss;
ERROR
1114
(HY000): The table
'ss'
is
full
mysql> insert into ss select * from ss;
ERROR
1114
(HY000): The table
'ss'
is
full
mysql> insert into ss select * from ss;
ERROR
1114
(HY000): The table
'ss'
is
full
[root@test3 test]# ls -lh
total 116K
-rw-rw----.
1
mysql mysql
9
.3K Sep
12
06
:
44
ss.frm
-rw-rw----.
1
mysql mysql 64K Sep
12
06
:
44
ss.MYD
-rw-rw----.
1
mysql mysql
1
.0K Sep
12
06
:
44
ss.MYI
mysql> insert into ss select * from ss;
ERROR
1114
(HY000): The table
'ss'
is
full
mysql> alter table ss max_ROWS=
10000000000
;
Query OK,
496
rows affected (
0.11
sec)
Records:
496
Duplicates:
0
Warnings:
0
mysql> insert into ss select * from ss;
Query OK,
496
rows affected (
0.02
sec)
Records:
496
Duplicates:
0
Warnings:
0
|
加锁:对整张表进行加锁,而不是行。
并发:在读数据的时候,所有的表上都可以获得共享锁(读锁),每个连接都不互相干扰。
在写数据的时候,获得排他锁,会把整个表进行加锁,而其他的连接请求(读,写请求)都处于等待中。
1
2
3
4
5
6
7
8
|
mysql> show status like
'table%'
;
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Table_locks_immediate |
2979
|
| Table_locks_waited |
0
|
+-----------------------+-------+
2
rows
in
set
(
0.00
sec))
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
获得表film_text的READ锁定
mysql> lock table film_text read;
Query OK,
0
rows affected (
0.00
sec)
当前session可以查询该表记录
mysql> select film_id,title from film_text where film_id =
1001
;
+---------+------------------+
| film_id | title |
+---------+------------------+
|
1001
| ACADEMY DINOSAUR |
+---------+------------------+
1
row
in
set
(
0.00
sec)
其他session也可以查询该表的记录
mysql> select film_id,title from film_text where film_id =
1001
;
+---------+------------------+
| film_id | title |
+---------+------------------+
|
1001
| ACADEMY DINOSAUR |
+---------+------------------+
1
row
in
set
(
0.00
sec)
当前session不能查询没有锁定的表
mysql> select film_id,title from film where film_id =
1001
;
ERROR
1100
(HY000): Table
'film'
was not locked
with
LOCK TABLES
其他session可以查询或者更新未锁定的表
mysql> select film_id,title from film where film_id =
1001
;
+---------+---------------+
| film_id | title |
+---------+---------------+
|
1001
| update record |
+---------+---------------+
1
row
in
set
(
0.00
sec)
mysql> update film
set
title =
'Test'
where film_id =
1001
;
Query OK,
1
row affected (
0.04
sec)
Rows matched:
1
Changed:
1
Warnings:
0
当前session中插入或者更新锁定的表都会提示错误:
mysql> insert into film_text (film_id,title) values(
1002
,
'Test'
);
ERROR
1099
(HY000): Table
'film_text'
was locked
with
a READ lock and can't be updated
mysql> update film_text
set
title =
'Test'
where film_id =
1001
;
ERROR
1099
(HY000): Table
'film_text'
was locked
with
a READ lock and can't be updated
其他session更新锁定表会等待获得锁:
mysql> update film_text
set
title =
'Test'
where film_id =
1001
;
等待
释放锁
mysql> unlock tables;
Query OK,
0
rows affected (
0.00
sec)
等待
Session获得锁,更新操作完成:
mysql> update film_text
set
title =
'Test'
where film_id =
1001
;
Query OK,
1
row affected (
1
min
0.71
sec)
Rows matched:
1
Changed:
1
Warnings:
0
当使用LOCK TABLES时,不仅需要一次锁定用到的所有表,而且,同一个表在SQL语句中出现多少次,就要通过与SQL语句中相同的别名锁定多少次,否则也会出错!举例说明如下。
(
1
)对actor表获得读锁:
mysql> lock table actor read;
Query OK,
0
rows affected (
0.00
sec)
(
2
)但是通过别名访问会提示错误:
mysql> select a.first_name,a.last_name,b.first_name,b.last_name from actor a,actor b where a.first_name = b.first_name and a.first_name =
'Lisa'
and a.last_name =
'Tom'
and a.last_name <> b.last_name;
ERROR
1100
(HY000): Table
'a'
was not locked
with
LOCK TABLES
(
3
)需要对别名分别锁定:
mysql> lock table actor
as
a read,actor
as
b read;
Query OK,
0
rows affected (
0.00
sec)
(
4
)按照别名的查询可以正确执行:
mysql> select a.first_name,a.last_name,b.first_name,b.last_name from actor a,actor b where a.first_name = b.first_name and a.first_name =
'Lisa'
and a.last_name =
'Tom'
and a.last_name <> b.last_name;
+------------+-----------+------------+-----------+
| first_name | last_name | first_name | last_name |
+------------+-----------+------------+-----------+
| Lisa | Tom | LISA | MONROE |
+------------+-----------+------------+-----------+
1
row
in
set
(
0.00
sec)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
获得表film_text的WRITE锁定
mysql> lock table film_text write;
Query OK,
0
rows affected (
0.00
sec)
当前session对锁定表的查询、更新、插入操作都可以执行:
mysql> select film_id,title from film_text where film_id =
1001
;
+---------+-------------+
| film_id | title |
+---------+-------------+
|
1001
| Update Test |
+---------+-------------+
1
row
in
set
(
0.00
sec)
mysql> insert into film_text (film_id,title) values(
1003
,
'Test'
);
Query OK,
1
row affected (
0.00
sec)
mysql> update film_text
set
title =
'Test'
where film_id =
1001
;
Query OK,
1
row affected (
0.00
sec)
Rows matched:
1
Changed:
1
Warnings:
0
其他session对锁定表的查询被阻塞,需要等待锁被释放:
mysql> select film_id,title from film_text where film_id =
1001
;
等待
释放锁:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
session_1
session_2
获得表film_text的READ LOCAL锁定
mysql> lock table film_text read local;
Query OK,
0
rows affected (
0.00
sec)
当前session不能对锁定表进行更新或者插入操作:
mysql> insert into film_text (film_id,title) values(
1002
,
'Test'
);
ERROR
1099
(HY000): Table
'film_text'
was locked
with
a READ lock and can't be updated
mysql> update film_text
set
title =
'Test'
where film_id =
1001
;
ERROR
1099
(HY000): Table
'film_text'
was locked
with
a READ lock and can't be updated
其他session可以进行插入操作,但是更新会等待:
mysql> insert into film_text (film_id,title) values(
1002
,
'Test'
);
Query OK,
1
row affected (
0.00
sec)
mysql> update film_text
set
title =
'Update Test'
where film_id =
1001
;
等待
当前session不能访问其他session插入的记录:
mysql> select film_id,title from film_text where film_id =
1002
;
Empty
set
(
0.00
sec)
释放锁:
mysql> unlock tables;
Query OK,
0
rows affected (
0.00
sec)
等待
当前session解锁后可以获得其他session插入的记录:
mysql> select film_id,title from film_text where film_id =
1002
;
+---------+-------+
| film_id | title |
+---------+-------+
|
1002
| Test |
+---------+-------+
1
row
in
set
(
0.00
sec)
Session2获得锁,更新操作完成:
mysql> update film_text
set
title =
'Update Test'
where film_id =
1001
;
Query OK,
1
row affected (
1
min
17.75
sec)
Rows matched:
1
Changed:
1
Warnings:
0
|