显示表的数据结构:show create table `t1 \G;
insert into students values(NULL, "王刚", "男", 20, "13811371377");
insert into courses_video(`name`,`lesson_id`) select `name`,`lesson_id` from `courses_video` where id=1;
update students set name='tom',age=12 where id=19 ;
update table_1 t1,table_2 t2 set t1.column = t2.column where t1.id = t2.pid;
select * from students where name like '%tom%' order by age asc; (desc 降序,默认是升序)
mysql> select name,count(*) from students group by name;
+-------+----------+
| name | count(*) |
+-------+----------+
| andi | 1 |
| aTOMb | 1 |
| john | 4 |
| TOM | 4 |
+-------+----------+
4 rows in set
mysql> select name,sum(age) as age_count from students group by name; # 计算age的数值和
+-------+-----------+
| name | age_count |
+-------+-----------+
| andi | 3 |
| aTOMb | 3 |
| john | 9 |
| TOM | 10 |
+-------+-----------+
4 rows in set
加入ROLLUP 计算age的总数
mysql> select name,sum(age) as age_count from students group by name with ROLLUP;
+-------+-----------+
| name | age_count |
+-------+-----------+
| andi | 3 |
| aTOMb | 3 |
| john | 9 |
| TOM | 10 |
| NULL | 25 |
+-------+-----------+
5 rows in set
mysql> select coalesce(name,'总数'),sum(age) as age_count from students group by name with rollup;
+-----------------------+-----------+
| coalesce(name,'总数') | age_count |
+-----------------------+-----------+
| andi | 3 |
| aTOMb | 3 |
| john | 9 |
| TOM | 10 |
| 总数 | 25 |
+-----------------------+-----------+
5 rows in set
group_concat
group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。
mysql> select id,name,lesson_id from `courses_video`;
+----+--------------------------------+-----------+
| id | name | lesson_id |
+----+--------------------------------+-----------+
| 1 | 1.1 交互式解释器1 | 2 |
| 2 | 1.2 算法是什么2 | 2 |
| 3 | 1.3 数和表达式3 | 2 |
| 4 | 2.1 序列概述23 | 3 |
| 5 | 2.2 通用的序列操作24 | 3 |
| 7 | 2.2.1 索引24 | 3 |
| 8 | 2.2.2 切片26 | 3 |
| 9 | 2.2.3 序列相加28 | 3 |
| 10 | 3.1 字符串基本操作4 | 4 |
| 11 | 3.2 设置字符串的格式:精简版41 | 4 |
| 12 | 3.3 设置字符串的格式:完整版43 | 4 |
+----+--------------------------------+-----------+
11 rows in set
mysql> select group_concat(name) from `courses_video`;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| group_concat(name) |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1.1 交互式解释器1,1.2 算法是什么2,1.3 数和表达式3,2.1 序列概述23,2.2 通用的序列操作24,2.2.1 索引24,2.2.2 切片26,2.2.3 序列相加28,3.1 字符串基本操作4,3.2 设置字符串的格式:精简版41,3.3 设置字符串的格式:完整版43 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
mysql> select group_concat(name) from `courses_video` group by `lesson_id`;
+-----------------------------------------------------------------------------------+
| group_concat(name) |
+-----------------------------------------------------------------------------------+
| 1.1 交互式解释器1,1.2 算法是什么2,1.3 数和表达式3 |
| 2.1 序列概述23,2.2 通用的序列操作24,2.2.1 索引24,2.2.2 切片26,2.2.3 序列相加28 |
| 3.1 字符串基本操作4,3.2 设置字符串的格式:精简版41,3.3 设置字符串的格式:完整版43 |
+-----------------------------------------------------------------------------------+
3 rows in set
三表查询
基于上边的group_concat,进行三表查询
select a.name,course_org_id,b.name,group_concat(c.name) from courses_course a ,organization_courseorg b,organization_teacher c where a.course_org_id=b.id and b.id=c.org_id group by a.id;
外键
用于关联2个表,首先创建2个表,class和std3,std3的class_id 用于关联class的id
mysql> create table class(
-> id int not null primary key,
-> name char(16));
Query OK, 0 rows affected (0.02 sec)
CREATE TABLE `std3` (
`id` int(11) NOT NULL auto_increment,
`name` char(16) NOT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_class_key` (`class_id`), # 一定要写int 因为关联的外键是int类型
CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)
);
创建好表后,外键主要有2个特性
mysql> insert into std3(class_id) values(0) ;
1452 - Cannot add or update a child row: a foreign key constraint fails (`testlv`.`std3`, CONSTRAINT `fk_cls_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
mysql> insert into class(id) values(0) ;
Query OK, 1 row affected
mysql> insert into std3(class_id) values(0) ;
Query OK, 1 row affected
2.如果class表有被关联的数据,那么是不能删除class表的这条记录的。(可以记为:老大你不能跑路,你走啦,你的小弟咋办)
mysql> delete from class where id=0 ;
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`testlv`.`std3`, CONSTRAINT `fk_cls_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
mysql> delete from std3 where class_id=0 ;
Query OK, 1 row affected
mysql> delete from class where id=0 ;
Query OK, 1 row affected
<=>: 比较操作符(不同于=运算符),当比较的的两个值为NULL时返回true。
比如:select * from students where weight <=> height; 就是找出weight和height都为null的值
MySQL中处理NULL使用IS NULL和IS NOT NULL运算符。
直接看示例,一目了然,跟pandas的join原理一致
创建2个单列表
mysql> select * from t1;
+----+
| id |
+----+
| 1 |
| 2 |
| 6 |
| 10 |
+----+
4 rows in set
mysql> select * from t2 ;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 6 |
| 7 |
+----+
5 rows in set
inner join 其实就是取交集
mysql> select t1.*,t2.* from t1,t2 where t1.id = t2.id; # 如果想只想取t1的id select t1.id,t2.* from t1,t2 where t1.id=t2.id;
+----+----+
| id | id |
+----+----+
| 1 | 1 |
| 2 | 2 |
| 6 | 6 |
+----+----+
3 rows in set
mysql> select * from t1 inner join t2 on t1.id = t2.id;
+----+----+
| id | id |
+----+----+
| 1 | 1 |
| 2 | 2 |
| 6 | 6 |
+----+----+
3 rows in set
left join
mysql> select * from t1 left join t2 on t1.id = t2.id;
+----+------+
| id | id |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 6 | 6 |
| 10 | NULL |
+----+------+
4 rows in set
right join
mysql> select * from t1 right join t2 on t1.id = t2.id;
+------+----+
| id | id |
+------+----+
| 1 | 1 |
| 2 | 2 |
| NULL | 3 |
| 6 | 6 |
| NULL | 7 |
+------+----+
5 rows in set
full join 下图
select * from t1 FULL JOIN t2 on t1.id = t2.id;
+------+------+
| id | id |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 6 | 6 |
| 10 | NULL |
| NULL | 3 |
| NULL | 7 |
+------+------+
6 rows in set
但是mysql可能不支持full join,但是可以通过以下办法实现相同的功能
mysql> select * from t1 left join t2 on t1.id=t2.id union select * from t1 right join t2 on t1.id=t2.id;
+------+------+
| id | id |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 6 | 6 |
| 10 | NULL |
| NULL | 3 |
| NULL | 7 |
+------+------+
6 rows in set
MySQL 事务主要用于处理操作量大,复杂度高的数据(如果你开始一个事物,修改了字段的属性:比如删除主键操作。那么,接下来的操作即使是数据的增删改,用rollback也不会回滚了,已经写到表里了)。比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事务!
一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性)、Consistency(稳定性)、Isolation(隔离性)、Durability(可靠性)
mysql> select * from t2 ;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 6 |
| 7 |
+----+
5 rows in set
-> begin; # 开始事物
Query OK, 0 rows affected
-> insert into t2(id) values(20);
Query OK, 1 row affected
mysql> insert into t2(id) values(21);
Query OK, 1 row affected
mysql> select * from t2 ;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 6 |
| 7 |
| 20 |
| 21 |
+----+
7 rows in set
mysql> rollback ; # 回滚,就不会写入
Query OK, 0 rows affected
-> select * from t2 ;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 6 |
| 7 |
+----+
5 rows in set
当然最后,如果你确认没问题就可以用commit 进行提交
× 在一个mysql库里备份数据库
1. 先进入mysql创建一个 edu_bak的数据库
2. 进入终端,将educate的数据库复制到edu_bak的数据库中
[lh@izbp177egh3y2vdv1jodhsz ~]$mysqldump educate -u root -p123456 --add-drop-table | mysql edu_bak -u root -p123456
× 将数据库导出到sql文件
[lh@izbp177egh3y2vdv1jodhsz ~]$ mysqldump -u root -p123456 educate > educate.sql
然后将educate.sql备份到新的数据库(test)了
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> source ~/educate.sql
InnoDB:
支持事务处理等MyISAM:
不支持事务,回滚将造成不完全回滚,不具有原子性
不支持外键
不支持外键
支持全文搜索
保存表的具体行数,不带where时,直接返回保存的行数
DELETE 表时,先drop表,然后重建表
MyISAM 表被存放在三个文件 。frm 文件存放表格定义。 数据文件是MYD (MYData) 。 索引文件是MYI (MYIndex)引伸
跨平台很难直接拷贝
MyISAM中可以使AUTO_INCREMENT类型字段建立联合索引
表格可以被压缩
选择:
因为MyISAM相对简单所以在效率上要优于InnoDB.如果系统读多,写少。对原子性要求低。那么MyISAM最好的选择。且MyISAM恢复速度快。可直接用备份覆盖恢复。
如果系统读少,写多的时候,尤其是并发写入高的时候。InnoDB就是首选了。
两种类型都有自己优缺点,选择那个完全要看自己的实际类弄。