MariaDB10.2.X-新特性2-支持check约束and with as
2016-07-23 23:04
369 查看
前几天写了一篇MariaDB10.2支持分析函数,大家印象中MySQL不支持with as ,check约束,那么MariaDB10.2也同样给你惊喜
1.with as
MariaDB [test11]> with a as ( select * from t1 where channerId ='支付宝' ) select * from a;
+----+--------+------------+-----------+--------+
| id | userId | orderId | channerId | amount |
+----+--------+------------+-----------+--------+
| 1 | 张3 | 2016060101 | 支付宝 | 100 |
| 2 | 李4 | 2016060102 | 支付宝 | 98 |
| 7 | 张3 | 2016060107 | 支付宝 | 200 |
| 10 | 李4 | 2016060110 | 支付宝 | 300 |
+----+--------+------------+-----------+--------+
4 rows in set (0.01 sec)
MariaDB [test11]> explain with a as ( select * from t1 where channerId ='支付宝' ) select * from a;
+------+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | t1 | ref | idx_channerId | idx_channerId | 182 | const | 4 | Using index condition |
+------+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
MariaDB [test11]>
#check 约束
MariaDB [test11]> CREATE TABLE `t_user` (
-> `id` int NOT NULL AUTO_INCREMENT ,
-> `name` varchar(255) NOT NULL DEFAULT '' ,
-> `age` tinyint UNSIGNED NULL check(age > 0 and age < 120) ,
-> `gender` tinyint NULL COMMENT '(0男,1女,2未知)' ,
-> `address` varchar(255) NULL ,
-> PRIMARY KEY (`id`)
-> )
-> ;
Query OK, 0 rows affected (0.03 sec)
MariaDB [test11]> insert into t_user ( name,age,gender,address) values ( 'zhangsan',1,1,'afdadfa');
Query OK, 1 row affected (0.03 sec)
MariaDB [test11]> insert into t_user ( name,age,gender,address) values ( 'lisi',121,0,'afdadfa');
ERROR 4022 (23000): CONSTRAINT `age` failed for `test11`.`t_user`
MariaDB [test11]> insert into t_user ( name,age,gender,address) values ( 'lisi',0,0,'afdadfa');
ERROR 4022 (23000): CONSTRAINT `age` failed for `test11`.`t_user`
MariaDB [test11]> update t_user set age =122 where id =1;
ERROR 4022 (23000): CONSTRAINT `age` failed for `test11`.`t_user`
MariaDB [test11]>
###
这个很值得期待的版本,希望官方版本也能及时跟进,纵观MySQL5.6,5.7的很多特性都是借鉴MariaDB的特性
1.with as
MariaDB [test11]> with a as ( select * from t1 where channerId ='支付宝' ) select * from a;
+----+--------+------------+-----------+--------+
| id | userId | orderId | channerId | amount |
+----+--------+------------+-----------+--------+
| 1 | 张3 | 2016060101 | 支付宝 | 100 |
| 2 | 李4 | 2016060102 | 支付宝 | 98 |
| 7 | 张3 | 2016060107 | 支付宝 | 200 |
| 10 | 李4 | 2016060110 | 支付宝 | 300 |
+----+--------+------------+-----------+--------+
4 rows in set (0.01 sec)
MariaDB [test11]> explain with a as ( select * from t1 where channerId ='支付宝' ) select * from a;
+------+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | t1 | ref | idx_channerId | idx_channerId | 182 | const | 4 | Using index condition |
+------+-------------+-------+------+---------------+---------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
MariaDB [test11]>
#check 约束
MariaDB [test11]> CREATE TABLE `t_user` (
-> `id` int NOT NULL AUTO_INCREMENT ,
-> `name` varchar(255) NOT NULL DEFAULT '' ,
-> `age` tinyint UNSIGNED NULL check(age > 0 and age < 120) ,
-> `gender` tinyint NULL COMMENT '(0男,1女,2未知)' ,
-> `address` varchar(255) NULL ,
-> PRIMARY KEY (`id`)
-> )
-> ;
Query OK, 0 rows affected (0.03 sec)
MariaDB [test11]> insert into t_user ( name,age,gender,address) values ( 'zhangsan',1,1,'afdadfa');
Query OK, 1 row affected (0.03 sec)
MariaDB [test11]> insert into t_user ( name,age,gender,address) values ( 'lisi',121,0,'afdadfa');
ERROR 4022 (23000): CONSTRAINT `age` failed for `test11`.`t_user`
MariaDB [test11]> insert into t_user ( name,age,gender,address) values ( 'lisi',0,0,'afdadfa');
ERROR 4022 (23000): CONSTRAINT `age` failed for `test11`.`t_user`
MariaDB [test11]> update t_user set age =122 where id =1;
ERROR 4022 (23000): CONSTRAINT `age` failed for `test11`.`t_user`
MariaDB [test11]>
###
这个很值得期待的版本,希望官方版本也能及时跟进,纵观MySQL5.6,5.7的很多特性都是借鉴MariaDB的特性
相关文章推荐
- 【Mariadb】centos7搭建mariadb Galera集群
- mariadb用户和权限管理
- 在centOS7 上安装MariaDB
- 三台主机分别部署apache,mariadb和php并实现xcache加速
- MariaDB主从备份记录
- 淘宝内部分享:MySQL & MariaDB性能优化
- nginx as Database Load Balancer for MySQL or MariaDB Galera Cluster
- centos7 下安装mariadb10两种方法
- CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置
- linux 系统下面mariadb启动失败
- Linux mysql mariadb
- MySQL并发复制系列三:MySQL和MariaDB实现对比
- 在 CentOS 7 CPanel 服务器上安装 MariaDB 10
- MariaDB10.2.X-新特性1-支持分析函数
- 燃情7月,ACMUG厦门、上海双城技术沙龙及MySQL/MariaDB创始人Monty中国行活动
- MySQL或者MariaDB里面sql_mode的设置详解
- centos7 mysql: mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
- centos7 安装 mariadb(mysql的一个分支) 的正确命令
- 查看编译完成nginx,apache,mysql,php,mariadb参数
- debian8.4中mariadb数据备份脚本