广义查询:
delete:
insert into:
update:
insert into TB_NAME (COL1, COL2,...)values (VAL1,VAL2,...)[,(VAL1,VAL2,...),...]
字符型:单引号
数值型:不需要引号
日期时间型:不需要引号
空值:NULL,需要单引号
mysql> help insert
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
Or:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
Or:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
insert into tutors (tname,gender,age) value (‘tom‘,‘f‘,30);
insert into tutors set tname=‘tom‘, gender=‘f‘,age=30;
insert into tutors (tname.gender,age) select name,gender,age from students where age > 20;
查看表里的最后一个数据,先降序排序,然后只显示第一个
select * from tutors order by TID desc limit 1;
select last_insert_id(); 此函数为表的计数函数,即把表清空后再插入也是从之前的行数+1开始插入
replace 插入的时候判断数据是否存在,如果存在则不插入
mysql> help replace
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
Or:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
Or:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...
delete: mysql> help delete
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
truncate TB_NAME 清楚表里的数据并重置计数器
update:
mysql> help update
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
Multiple-table syntax:
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
update TB_NAME set COL1=...,COL2=... where
连接管理器:
接受请求
创建线程
认证用户
建立安全连接
并发控制:
多版本并发控制:MVCC。通过快照和时间点来管理
锁:
读锁:共享锁
写锁:独占锁
mysql> help lock
Name: ‘LOCK‘
LOCK TABLES
tbl_name [[AS] alias] lock_type
[, tbl_name [[AS] alias] lock_type] ...
lock_type:
READ [LOCAL]
| [LOW_PRIORITY] WRITE
UNLOCK TABLES
lock tables TBNAME {read|write};
unlock tables 解锁锁
锁粒度:从大到小,mysql服务器仅支持表级锁,行锁需要由存储引擎完成:
表锁:
行锁:
页锁:
事物:
RDBMS:ACID(原子性,一致性,隔离性,持久性)
MyISAM
Innodb
隔离性:
隔离级别:
READ UNCOMMITTED 读未提交
READ COMMITTED 读提交
REPATABLEREAD 可重读
SERIABLIZABLE 可串行
mysql> show global variables like ‘%iso%‘; 默认的隔离级别
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| tx_isolation | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)
修改: set {session|global} var_name=‘‘;
mysql> set tx_isolation =‘read-uncommitted‘;
mysql> show variables like ‘%iso%‘;
+---------------+------------------+
| Variable_name | Value |
+---------------+------------------+
| tx_isolation | READ-UNCOMMITTED |
+---------------+------------------+