mysql、orcal等数据库都是基于客户端-服务器端的数据库
1、数据库-服务端
服务器部分是负责所有的数据访问和处理的一个软件
与数据文件打交道的只有服务器软件,对于数据的增删改查都是这软件做的,比如mysql管理系统
2、数据库-客户端
客户端是与用户打交道的软件,比如命令行、phpmyadmin、workbench等。
如果一个用户需要查询某个数据库的所有信息,那么会把命令输入给这个客户端软件,然后这个软件就向服务器发起了请求,等服务器把结果返回给客户端时,客户端进行显示给,然后用户就看到了
3、mysql服务器使用的端口
既然,mysql是基于c/s架构的,那么服务器端必定需要占用端口,这个端口是3306。
还是来说mysql的基本语句,先新建student表,插入一些数据,方便基本语句的操作,插入后的student表:
distinct 去重问题
1、针对某个字段去重
2、如果有多个列,去重没有效果
3、如果有多个列,distinct去重没有效果,并且distinct只能放在列名最前面,否则报错
order by排序
select * from student order by chinese; #默认是正序排序
select * from student order by chinese desc; #倒序
select * from student order by chinese,math desc; #先按照chinese字段正序显示,然后再按照math倒序显示
select * from student order by chinese desc,math desc; #先按照chinese字段倒序显示,然后再按照math倒序显示
limit分页显示 &count
1、limit后只有一个数字
select * from student limit 3;只显示3个查询的数据
2、limit后有两个数字
select * from student limit 6,3;从第7个位置开始显示,然后显示3个查询的数据
第1个数字,表示开始显示的位置;
第2个数字,表示显示记录的个数;
默认显示的位置是从0开始的。
3、count计算数据库表中的总记录数
select count(*) from student;
结合limit可以计算总页数
null&or¬&and
select * from infolist where phonenum is NULL;
infolist表中查询phonenum字段为null的记录
select * from infolist where phonenum is not NULL;
infolist表中查询phonenum字段不为null的记录
select * from infolist where (name='bohuo' or name='lili') and phonenum is not null;
查询姓名为bohuo或者lili的名字,并且手机号号不能为空的记录
and的优先级要高于or,必需要加括号;
select * from infolist where id not in (1002,1004);
模糊查询like
最常用的通配符是百分号(%),在搜索中,%表示任何字符出现任意次数;
下划线(_)与百分号(%)的作用类似,只不过下划线只能匹配一个字符,而不是多个字符
select * from infolist where name like 'xiao%';
name以xiao开头的数据
select * from infolist where name like '%ao%';
name包含ao的数据
select * from infolist where name like 'xiao____';
name以xiao开头,并且后面只有4位的数据
一般当数据量太大时,不建议将”%”放在开头进行模糊查询,会降低查询效率;
concat链接数据库
select concat(name,’(‘,phonenum,’)’) from infolist where phonenum is not null;
将phonenum不为空的数据,以姓名(手机号)这种形式显示出来;
聚集函数
MySQL常用的聚集函数
如:
select avg(chinese) from student;求学生语文成绩的平均值;
select max(chinese) from student;求学生语文成绩的最大值;
select sum(chinese) from student;求学生语文成绩的总和;
AUTO_INCREMENT
auto_increment用来确定主键是否是自增长,只需要添加为0或者null,那么这个字段就会自动根据上次的值+1。
1、创建数据库表时,将主键设置为自增长,在插入数据时,添加0:
2、插入数据时,添加为null:
3、将auto_increment设置为某个数值,则从该数值开始自增长: