一、相对于表结构上的语法
创建数据库名称为"MysqlTest",分别创建课目表 “topic’,学生表"student”,教师表"teacher",分数表"score"。
(一)库
1,创建数据库
create database test;
create database MysqlTest;
2、使用数据库:use MysqlTest;
3、删除库
drop database test;
(二)表
1、分别创建表
create table topic(
`id` int(11) auto_increment not null,
`course` char(32) not null,
`t_id` tinyint(4) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
create table student(
`id` int(11) auto_increment not null,
`s_name` char(50) not null,
`sex` char(16) not null,
`s_id` int(11) not null,
primary key(`id`),
)engine=innodb default charset=utf8;
create table teacher(
`id` int(11) auto_increment not null,
`t_name` char(50) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
create table score(
`id` int(11) auto_increment not null,
`s_name` char(50) not null,
`course` int(11) not null,
`number` int(11) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
create table test(
`id` int(11) auto_increment not null,
`s_test` char(50) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
2、修改表名称
rename table test to new_test;
2、删除一张表
drop table new_test;
3、增加一个字段
alter table student add column `test` int(11) default '0' comment "测试字段" not null;
4、修改字段的类型,修改字段的名称,删除一个字段。
alter table student modify `test` char(32) not null;
alter table student change `test` `new_test` varchar(55) not null;
alter table student drop `new_test` ;
分别运行展示:
4、索引。
普通索引是可以重复的,唯一索引是不能重复。语句可以用creat或者alter。
- 创建索引
alter table student add index `index_s_name` (`s_name`);
create unique index `index_s_id` on student(`s_id`);
- 删除索引
drop index `index_s_name` on student;
alter table student drop index `index_s_id`;
5、主键。
主键是唯一的,不可重复。
其实还是可以修改的,之所以不能删除是因为id设置了自增,这里可以先修改Id的类型,然后在删除。
alter table student modify `id` int(11);
alter table student drop primary key;
alter table student add primary key(`s_id`);
假如把新的主键设置自增,那么可以
alter table student modify `s_id` int(11) auto_increment;
二、相对与数据上的语法。
1、增加数据
insert into student (`s_name`,`sex`,`s_id`)values("张三","男",123),("王二","男",124),("麻子","男",125),("小美","女",126),("小倩","女",127),("小夏","女",128);
2、修改一条数据
update student set s_name="kangkang" where s_id = 123;
3、删除一条数据
delete student where s_id = 123;
4、查询
select s_name from student where s_id > 126;
select s_name,sex from student where s_id > 126;
另外还有一些组合:
去重:select distinct 字段 from 表名 where 条件
逻辑条件: and or
比较条件:< , <=, >, >=, <>, between value1 and value2
判断空:
1)判断null: is null
2)判断空字符串: ="" / <>""
模糊条件:like
%:替换任意长度字符
_:替换单个字符
分页查询,limit 起始行,查询行数
排序:order by 字段 asc/desc
asc:升序
desc:降序
分组:group by 字段
三、经典面试例题
(一)
表如下
1、查询两门以上不及格同学的名字,不及格科目名称及分数。
select sc.s_name as "名字",sc.number as "分数",tp.course as "课目"
from score sc,topic tp
where sc.number < 60 and tp.id = sc.course and sc.s_name
in(select sc.s_name from score sc where number < 60 group by s_name having count(*) >= 2);
简单说明一下,group by:
从字面上的意思理解 group是分组,by后面写字段名,表示根据哪个字段分组,group by 必须根据聚合函数来写:
- count() 计数
- sun() 求和
- avg() 平均数
- max() 最大值
- min() 最小值
思考:请查询每科的平均成绩?
select tp.course,AVG(sc.number) from topic tp ,score sc
where tp.id = sc.course group by sc.course;
2、查询每科成绩最好的前两名的学生。
select a.s_name,a.number,tp.course from score a,topic tp
where tp.id = a.course and
(select count(*) from score b where a.course = b.course and b.number >= a.number) <= 2
order by a.course, a.number desc;
3、查询每科最好成绩的学生姓名,课目,分数
select sc1.s_name, sc1.number, tp.course from score sc1,
(select course, Max(number)as maxnumber from score group by course) sc, topic tp
where sc1.number=sc.maxnumber and sc.course=sc1.course and sc.course=tp.id;
各科成绩统计参考连接[link:] https://www.cnblogs.com/lsgcoder101/p/6011059.html
四、mysql存储引擎,常用配置参数,主从配置
1、存储引擎
在mysql客户端,使用命令 show engines; 查看MYSQL支持引擎。
最常用的两种存储引擎:
- Myisam是Mysql的默认存储引擎。当create创建新表时,未指定新表的存储引擎时,默认使用Myisam。每个MyISAM在磁盘上存储成三个文件。文件名都和表名相同,扩展名分别是.frm(存储表定义)、.MYD (MYData,存储数据)、.MYI (MYIndex,存储索引)。数据文件和索引文件可以放置在不同的目录,平均分布io,获得更快的速度。
- InnoDB存储引擎提供了具有提交、回滚和崩溃恢复能力的事务安全。但是对比Myisam的存储引擎,InnoDB写的处理效率差一些并且会占用更多的磁盘空间以保留数据和索引
2、常用配置参数
datadir:指定mysql的数据目录位置,用于存放mysql数据库文件、日志文件等。
配置示例:datadir=D:/wamp/mysqldata/Data
default-character-set:mysql服务器默认字符集设置。
配置示例:default-character-set=utf8
skip-grant-tables:当忘记mysql用户密码的时候,可以在mysql配置文件中配置该参数,跳过权限表验证,不需要密码即可登录mysql。
3、主从配置
-
比如从库一个表不存在导致整个从库的同步都卡住等,可以配置从库的my.cnf,添加以下配置 :
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
这样出现这些错误代码时主从复制依然会跳过错误继续同步下一条记录 -
主库上有个数据库不需要同步
如果主库上有个数据库不需要同步,可以在主库的配置文件添加
binlog-ignore-db = testdb -
只同步某一个数据库
如果只想同步某一个数据库,可以在配置文件添加
binlog-do-db =testdb
如果二者都不存在,那么所有数据库都会被同步,如果二者都存在则以binlog-do-db 优先,如果有多个数据库,那么填写多条记录即可 -
某一个表不被同步
如果只想某一个表不被同步,那么配置文件添加
replicate-ignore-table=testdb.test_table即可 -
保留7天二进制日志
因为主从复制必须开启binlog,而binlog很可能日积月累非常大写爆磁盘,所以可以配置
expire-logs-days = 7
只保留7天的二进制日志 -
限制binlog空间大小
relay-log-space-limit = 16G
限制binlog的空间最大使用16G,以防磁盘被日志占满