mysql 流程函数 存储引擎 InnoDB简单特性
建表及插入数据语句:
mysql> create table salary(userid int,salary decimal(9,2));
Query OK, 0 rows affected (0.11 sec)
mysql> insert into salary values(1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,
null);
Query OK, 6 rows affected (0.13 sec)
Records: 6 Duplicates: 0 Warnings: 0
1.if(value,t,f)函数:这里觉得高于2000就是'high',其它都是low
mysql> select if(salary>2000,'high','low') from salary;
+------------------------------+
| if(salary>2000,'high','low') |
+------------------------------+
| low |
| low |
| high |
| high |
| high |
| low |
+------------------------------+
6 rows in set (0.00 sec)
2.ifnull(value1,value2)函数:这个函数能够用来将NULL值换成0
mysql> select ifnull(salary,0) from salary;
+------------------+
| ifnull(salary,0) |
+------------------+
| 1000.00 |
| 2000.00 |
| 3000.00 |
| 4000.00 |
| 5000.00 |
| 0.00 |
+------------------+
6 rows in set (0.00 sec)
3.case when [value1] then [result]...else [default] end函数:
mysql> select case when salary<=2000 then 'low' else 'high' end from salary;
+---------------------------------------------------+
| case when salary<=2000 then 'low' else 'high' end |
+---------------------------------------------------+
| low |
| low |
| high |
| high |
| high |
| high |
+---------------------------------------------------+
6 rows in set (0.00 sec)
4.case [expr] when [value1] then [result] ... else [default] end函数:
mysql> select case salary when 1000 then 'low' when 2000 then 'mid' else 'high'e
nd from salary;
+----------------------------------------------------------------------+
| case salary when 1000 then 'low' when 2000 then 'mid' else 'high'end |
+----------------------------------------------------------------------+
| low |
| mid |
| high |
| high |
| high |
| high |
+----------------------------------------------------------------------+
6 rows in set (0.02 sec)
5.关于mysql存储引擎的一些东西:
存储引擎是mysql不同于其它数据库的一个重要特性。用户能够依据实际须要利用这个特性定制自己的存储引擎.
mysql的引擎有:
mysql> show engines \G;
*************************** 1. row ***************************
Engine: MyISAM
Support: YES
Comment: Default engine as of MySQL 3.23 with great performance
*************************** 2. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
*************************** 3. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
*************************** 4. row ***************************
Engine: BerkeleyDB
Support: NO
Comment: Supports transactions and page-level locking
*************************** 5. row ***************************
Engine: BLACKHOLE
Support: NO
Comment: /dev/null storage engine (anything you write to it disappears)
*************************** 6. row ***************************
Engine: EXAMPLE
Support: NO
Comment: Example storage engine
*************************** 7. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
*************************** 8. row ***************************
Engine: CSV
Support: NO
Comment: CSV storage engine
*************************** 9. row ***************************
Engine: ndbcluster
Support: NO
Comment: Clustered, fault-tolerant, memory-based tables
*************************** 10. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
*************************** 11. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
*************************** 12. row ***************************
Engine: ISAM
Support: NO
Comment: Obsolete storage engine
12 rows in set (0.00 sec)
或者使用这个cmd:
mysql> show variables like 'have%';
+-----------------------+----------+
| Variable_name | Value |
+-----------------------+----------+
| have_archive | YES |
| have_bdb | NO |
| have_blackhole_engine | NO |
| have_compress | YES |
| have_crypt | NO |
| have_csv | NO |
| have_example_engine | NO |
| have_federated_engine | NO |
| have_geometry | YES |
| have_innodb | YES |
| have_isam | NO |
| have_ndbcluster | NO |
| have_openssl | DISABLED |
| have_query_cache | YES |
| have_raid | NO |
| have_rtree_keys | YES |
| have_symlink | YES |
+-----------------------+----------+
17 rows in set (0.00 sec)
disabled说明mysql支持该engine。可是启动的时候被禁用.
创建表的时候,能够使用enginekeyword指定该表使用哪个engine:
mysql> create table ai(i bigint(20) NOT NULL AUTO_INCREMENT,PRIMARY KEY(I)) ENGI
NE=MyISAM DEFAULT CHARSET=GBK;
Query OK, 0 rows affected (0.03 sec)
也能够改动表的引擎:
mysql> alter table ai engine=innodb;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table ai \G;
*************************** 1. row ***************************
Table: ai
Create Table: CREATE TABLE `ai` (
`i` bigint(20) NOT NULL auto_increment,
PRIMARY KEY (`i`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
常见的存储引擎有:
MyISAM,InnoDB,MEMORY,MERGE,NDB
上述引擎中仅仅有InnoDB支持外键。
mysql的默认存储引擎是MyISAM
每一个MyISAM在磁盘上存储成3个文件:
.frm(存储表定义)
.MYD(MYData,存储数据)
.MYI(MYIndex,存储索引)
关于InnoDB的一些特性:
a.自己主动增长字段:
mysql> create table autoincre(i smallint not null auto_increment,name varchar(20
),primary key(i))engine=innodb;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into autoincre values(1,'1'),(2,'2'),(null,'3');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from autoincre;
+---+------+
| i | name |
+---+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+---+------+
3 rows in set (0.00 sec)
向自增长字段插入记录后。不影响该字段自己添加值.
对于InnoDB表。自己主动增长字段必须是索引,假设是组合索引也必须是组合索引的第一个列.
可是对于MyISAM表,自增长字段能够不是组合索引的第一个列,能够作为第二个列出现:
mysql> create table autoincre_demo(d1 smallint not null auto_increment,d2 smalli
nt not null,name varchar(10),index(d2,d1))engine=myisam;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into autoincre_demo(d2,name)values(2,'2'),(3,'3'),(4,'4'),(2,'2'),
(3,'3'),(4,'4');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from autoincre_demo;
+----+----+------+
| d1 | d2 | name |
+----+----+------+
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 1 | 4 | 4 |
| 2 | 2 | 2 |
| 2 | 3 | 3 |
| 2 | 4 | 4 |
+----+----+------+
6 rows in set (0.00 sec)
自增长字段d1作为组合索引在第二列中出现,自增长字段的记录依照组合索引d2进行排序后递增.
b.mysql的存储引擎中仅仅有InnoDB支持fk:
建表语句:
mysql> create table country(country_id smallint unsigned not null auto_increment
,country varchar(50) not null,primary key(country_id))
-> engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.14 sec)
mysql> create table city(
-> city_id smallint unsigned not null auto_increment,
-> country_id smallint unsigned not null,
-> primary key(city_id),
-> foreign key(country_id) references country(country_id)
-> on delete restrict on update cascade
-> engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.05 sec)
插入记录:
mysql> insert into country(country) values('china');
Query OK, 1 row affected (0.03 sec)
mysql> select * from country;
+------------+---------+
| country_id | country |
+------------+---------+
| 1 | china |
+------------+---------+
1 row in set (0.00 sec)
mysql> insert into city(country_id) values(1);
Query OK, 1 row affected (0.11 sec)
mysql> select * from city;
+---------+------------+
| city_id | country_id |
+---------+------------+
| 1 | 1 |
+---------+------------+
1 row in set (0.00 sec)
city表依赖country表的country_id字段,删除会出错:
mysql> delete from country where country_id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constrai
nt fails (`tom1/city`, CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFER
ENCES `country` (`country_id`) ON UPDATE CASCADE)
如今更新country表中的country_id字段。city表的country_id字段也会被同步更新。这是由于在创建city表的最后加了:on update cascade。即:更新时做级联操作
mysql> update country set country_id=1000 where country_id=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from city;
+---------+------------+
| city_id | country_id |
+---------+------------+
| 1 | 1000 |
+---------+------------+
1 row in set (0.00 sec)
mysql 流程函数 存储引擎 InnoDB简单特性的更多相关文章
- MySQL两大存储引擎InnoDB与MyISAM
1.InnoDB存储引擎 MySQL5.5中InnoDB成为默认的存储引擎.InnoDB是事务型存储引擎,被设计用来处理大量的短期事务.它的性能和自动崩溃恢复特性,使得它在非事务场景中也很流行. 所以 ...
- MySQL学习笔记——存储引擎的索引特性
- mysql数据库之 存储引擎、事务、视图、触发器、存储过程、函数、流程控制、数据库备份
目录 一.存储引擎 1.什么是存储引擎? 2.mysql支持的存储引擎 3. 使用存储引擎 二.事务 三.视图 1.什么是视图 2.为什么要用视图 3.如何用视图 四.触发器 为何要用触发器 创建触发 ...
- 常用Mysql存储引擎--InnoDB和MyISAM简单总结
常用Mysql存储引擎--InnoDB和MyISAM简单总结 2013-04-19 10:21:52| 分类: CCST|举报|字号 订阅 MySQL服务器采用了模块化风格,各部分之间保持相 ...
- 浅谈MySQL存储引擎-InnoDB&MyISAM
存储引擎在MySQL的逻辑架构中位于第三层,负责MySQL中的数据的存储和提取.MySQL存储引擎有很多,不同的存储引擎保存数据和索引的方式是不同的.每一种存储引擎都有它的优势和劣势,本文只讨论最常见 ...
- MySql的多存储引擎架构, 默认的引擎InnoDB与 MYISAM的区别(滴滴)
1.存储引擎是什么? MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术, ...
- MySQL - 两种存储引擎 (MyISAM PK InnoDB) + MVCC
总结 1.两种存储引擎各有各的有点,MyISAM专注性能,InnoDB专注事务.两者最大的区别就是InnoDB支持事务,和行锁. 2.InnoDB采用MVCC(Multi-Version Concur ...
- 转!!MySQL中的存储引擎讲解(InnoDB,MyISAM,Memory等各存储引擎对比)
MySQL中的存储引擎: 1.存储引擎的概念 2.查看MySQL所支持的存储引擎 3.MySQL中几种常用存储引擎的特点 4.存储引擎之间的相互转化 一.存储引擎: 1.存储引擎其实就是如何实现存储数 ...
- MySQL存储引擎 InnoDB/ MyISAM/ MERGE/ BDB 的区别
MyISAM:默认的MySQL插件式存储引擎,它是在Web.数据仓储和其他应用环境下最常使用的存储引擎之一.注意,通过更改 STORAGE_ENGINE 配置变量,能够方便地更改MySQL服务器的默认 ...
随机推荐
- Stirling数,Bell数,Catalan数,Bernoulli数
组合数学的实质还是DP,但是从通式角度处理的话有利于FFT等的实现. 首先推荐$Candy?$的球划分问题集合: http://www.cnblogs.com/candy99/p/6400735.ht ...
- JNI介绍
JNI是在学习Android HAL时必须要面临一个知识点,如果你不了解它的机制,不了解它的使用方式,你会被本地代码绕的晕头转向,JNI作为一个中间语言的翻译官在运行Java代码的Android中有着 ...
- 哈希表(散列表)—Hash表解决地址冲突 C语言实现
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...
- noip模拟 五子棋
递推+模拟.在读取数据时,我们建4个图,分别代表这个图中横.纵.左斜右斜的连续长度.例如heng[i][j]代表ij这个点所在的横着一条线的长度. 然后搜索,对于一个空点,如果他的上下都>=4那 ...
- mysql_server安装
https://blog.csdn.net/wz1226864411/article/details/76146180
- Android Studio安装后提示No JVM installation found解决办法
Android Studio安装后提示No JVM installation found解决办法 问题描述:Android Studio安装完毕,打开时出现提示"No JVM install ...
- timeago.js-时间显示插件
注意事项: 1. 时间格式 = “2018-03-02 17:13:00”时,动态获取的时间无法通过 拼接字符串的方法 添加到 dom元素的 datetime属性上,结果为 <div class ...
- Ubuntu 16.09下iptables通过raw表实现日志输出和调试
1.先配置好raw表日志打点功能 参考:http://www.cnblogs.com/EasonJim/p/8413563.html 2.配置好messages文件 参考:http://www.cnb ...
- POJ 3237 Tree (树链剖分)
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 2825 Accepted: 769 Description ...
- [置顶] linux学习之静态库和动态库的制作与使用
linux中静态库和动态库的制作与使用 一.静态链接库 1.首先编写模块程序example.c.example.h 2.使用gcc -c example.c -o example.o编译example ...