MyISAM不支持事务处理等高级处理,而InnoDB类型支持
MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快
InnoDB支持外部键等高级数据库功能
创建索引:alter table tablename add index (“字段名”)
学习日志 LIUGUOFENG
MyISAM不支持事务处理等高级处理,而InnoDB类型支持
MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快
InnoDB支持外部键等高级数据库功能
创建索引:alter table tablename add index (“字段名”)
1. 选取最实用的字段属性,尽可能减少定义字段长度,尽量把字段设置NOT NULL,例如“省份,性别”,最好设置为ENUM
2. 使用连接(JOIN)来代替子查询
3. 使用联合(UNION)来代替手动创建临时表
4. 事务处理
5. 锁表,优化事务处理
6. 使用外键,优化锁定表
7. 建立索引
8. 优化查询语句
进入后台 phpMyAdmin 执行 SQL 语句
UPDATE wp_options SET option_value = replace( option_value, 'http://xinghaizhongxin.com', 'http://xinghaizhongxin.guofeng.io' ) WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET post_content = replace( post_content, 'http://xinghaizhongxin.com', 'http://xinghaizhongxin.guofeng.io' ) ; UPDATE wp_posts SET guid = replace( guid, 'http://xinghaizhongxin.com', 'http://xinghaizhongxin.guofeng.io' ) ;
或进入主题 function.php 添加
update_option('siteurl','http://xinghaizhongxin.guofeng.io'); update_option('home','http://xinghaizhongxin.guofeng.io');
然后进入后台设置-常规, 修改域名
数据库的如下数据表 scores 记录用户得分历史,uid(int) 表示用户 ID,score(int) 表示分数,date(date) 表示日期,每个用户每天会产生多条数据 (示例:uid 3, date 2017-03-17)
现在需要一份用户列表,这些用户在 2017 年 3 月份的 31 天中,至少要有 16 天, 每天得分总和大于 40 分。用一条 SQL 语句表示。
select uid,count(1) from ( select date,uid,sum(score) sum from scores where substr(date,1,7)='2017-03' group by date,uid having sum(score)>40 ) cc group by uid having count(1) >=16
mysql_fetch_row() 以索引数组的方式取查询的结果集
mysql_fetch_array() 以索引数组和关联数组两种方式取查询的结果集
用一条 SQL 语句,查询出每门课都大于80分的学生姓名
name course score
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
select name from table where name not in(select name from table where score<=80);
stu 表:学生信息表
score 表:分数表
1. 写出一条sql语句,查出所有学生信息
select * from stu;
2. 写出一条sql语句,查出表中从第2条到第4条学生的信息
select * from stu limit 1,3;
select * from stu where id between 902 and 904;
3. 写出一条sql语句,查出年龄最小的那个学生的信息
select * from stu order by birth desc limit 1;
4. 写出一条sql语句,查出“计算机系”和“英语系”的所有学生信息
select * from stu where department=”计算机系” or department=”英语系”;
5. 写出一条sql语句,查出每一个系总共有多少学生
select count(*) from stu group by department;
6. 写出一条sql语句,查出每一个系的最高分是多少,包括对应的系名信息
select department,max(sc.grade) from stu left join score as sc on stu_id = sc.stu_id group by stu.department;
7. 写出一条sql语句,查出每一个系的最高分的那个学生的信息
select * from (select a.name,a.id,a.department,b.grade from stu as a join score as b on a.id=b.stu_id) a join (select department,max(grade) grade from stu left join score as sc on stu.id = sc.stu_id group by stu.department) b on a.department=b.department and a.grade=b.grade
数据库中有表 members(id、username、post_number、pass、email) 和表 posts(id、members_id、title、content、add_time)
1. 取出表 members 中发帖数量 (post_number) 前10位的名字
select username,post_number from members order by posts_number desc limit 10;
2. 名字为 join 的最新10个发帖的标题
select title from posts where id=(select id from members where username=”join”) order by add_time desc limit 10;
select username.title from members as m join posts as p on m.id=p.members_id where m.username=”join” order by p.add_time desc limit 10;
MySQL 数据库中的字段类型 varchar 和 char 的主要区别是什么?哪种字段的查询效率高?
varchar 为存储变长字符串,存储单位为字节,不同的编码方式存储的字符长度不一样,并且会单独用一个字符来存储字符长度。
char 为存储定长字符串,存储单位为字符,最多不超过256个字符。
char 的查询效率高,因为 varchar 会单独存储长度,在查询时先查找长度,然后进行数据的提取,比 char 定长类型多了一个步骤,所以效率第一点。
索引的意义
索引用来快速寻找那些具有特定值的记录。
主键索引和唯一索引的区别
1. 主键索引是一种唯一性索引,且必须指定为“PRIMARY KEY” ,每个表只能有一个主键。
2. 唯一索引索引列的值只能出现一次,即必须唯一。
索引的缺点
1. 创建索引和维护索引徐尧耗费时间,这种时间随着数据量的增加而增加。
2. 索引需要占用物理空间,除了数据表占用数据空间之外,每一个索引还要占用一定的物理空间,如果要建立聚簇索引,则需要的空间会更大。
3. 当对数据表中的数据进行增加、删除、修改的时候,索引也要进行动态的维护,这样就降低了数据的维护速度。