事务,视图,存储过程,存储函数,触发器,分区
事务:
事务是一组原子性的查询语言,也就是说,在一组操作中,要么全部执行,要么一个也不执行,如果任何操作失败,则所有操作都失效,保证数据的完整性。
首先数据库的存储引擎不能是MyISAM,因为MyISAM不支持事务。支持事务的存储引擎有InnoDB,NDB和BDB
查看MySQL支持的存储引擎的命令:
show engines
修改表引擎:
alter table demo engine= innodb
//创建银行表
create table bank(bid int,name char(20),money decimal(6,2));
insert into bank (bid,name,money) values (1,'小米',1000),(2,'小明',1000);
//1.开启事务
begin;
//2.关闭自动提交
set autocommit=0;
//3. 执行操作
select * from bank;
+------+-----------+---------+
| bid | name | money |
+------+-----------+---------+
| 1 | 小米 | 900.00 |
| 2 | 小明 | 1100.00 |
+------+-----------+---------+
update bank set money=money-100 where bid=1;
update bank set money=money+100 where bid=2;
// 4.如果没有问题,那么就确认,如果有问题,那么就rollback
commit;
在PHP中的使用:
try {
$pdo = new pdo('mysql:host=127.0.0.1;dbname=abc','root','');
//设置下面的语句都为异常错误,这样才能被try catch接收
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->query("SET NAMES UTF8");
//1.开启事务
$pdo->query("BEGIN");
//2.关闭自动提交
$pdo->query("SET AUTOCOMMIT=0");
//3.执行操作
//bid为1的用户减去100
$pdo->exec("UPDATE bank SET money=money-100 WHERE bid=1");
//bid为2的用户加100
$pdo->exec("UPDATE bank SET money=money+100 WHERE bid=2");
//4.确认了
$pdo->exec('COMMIT');
} catch (PDOException $e) {
//回滚
$pdo->exec('ROLLBACK');
echo $e->getMessage();
}
视图
视图是一张虚表,是一组查询记录。
将部分表字段分配到视图中,重要字段不分配,提高数据库安全性。
示例:
创建一张学生信息表:
mysql> create table stu(id tinyint primary key auto_increment not null,name char(10) not null default '',sex tinyint(1) not null default 0,birthday date);//创建学生信息表
Query OK, 0 rows affected (0.55 sec)
mysql> show tables;//显示当前数据库内的所有表
+---------------+
| Tables_in_stu |
+---------------+
| stu |
+---------------+
1 row in set (0.00 sec)
mysql> desc stu;//查看stu表的结构
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | tinyint(4) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | | |
| sex | tinyint(1) | NO | | 0 | |
| birthday | date | YES | | NULL | |
+----------+------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> insert into stu(name,sex,birthday)value('Lucy',0,19900101);//往stu表里插入1条数据
Query OK, 1 row affected (0.11 sec)
mysql> select*from stu;//查看表数据
+----+------+-----+------------+
| id | name | sex | birthday |
+----+------+-----+------------+
| 1 | Lucy | 0 | 1990-01-01 |
+----+------+-----+------------+
1 row in set (0.00 sec)
mysql> insert into stu(name,sex,birthday)value('LiLy',0,19900110),('李雷',1,19910306),('韩梅梅',0,19921112);//再次插入3条数据
Query OK, 3 rows affected (0.12 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select*from stu;//查看表数据
+----+--------+-----+------------+
| id | name | sex | birthday |
+----+--------+-----+------------+
| 1 | Lucy | 0 | 1990-01-01 |
| 2 | LiLy | 0 | 1990-01-10 |
| 3 | 李雷 | 1 | 1991-03-06 |
| 4 | 韩梅梅 | 0 | 1992-11-12 |
+----+--------+-----+------------+
4 rows in set (0.06 sec)
mysql> show table status where comment='VIEW';//查看视图
Empty set (0.00 sec)//为空,表示当前数据库没有视图
mysql> create view stuview as select name,sex from stu;//创建视图
Query OK, 0 rows affected (0.11 sec)
mysql> show table status where comment='VIEW'\G//再次查看视图
*************************** 1. row ***************************
Name: stuview
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW
1 row in set (0.00 sec)
mysql> select * from stuview;//查看视图表数据
+--------+-----+
| name | sex |
+--------+-----+
| Lucy | 0 |
| LiLy | 0 |
| 李雷 | 1 |
| 韩梅梅 | 0 |
+--------+-----+
4 rows in set (0.00 sec)
mysql> alter view stuview as select id,name,sex from stu;//修改视图
Query OK, 0 rows affected (0.08 sec)
mysql> select * from stuview;//再次查看视图表数据
+----+--------+-----+
| id | name | sex |
+----+--------+-----+
| 1 | Lucy | 0 |
| 2 | LiLy | 0 |
| 3 | 李雷 | 1 |
| 4 | 韩梅梅 | 0 |
+----+--------+-----+
4 rows in set (0.00 sec)
mysql> drop view stuview;//删除视图
Query OK, 0 rows affected (0.00 sec)
分区:
查看MySQL插件有无partition,这是实现分区的基本条件:
show plugins;
拓展阅读:
MySQL表的四种分区类型
Mysql 分区 分表相关总结之方案选择
mysql 导出表结构和表数据 mysqldump用法