紧接上条,只是学习笔记,方便以后查找,不喜勿喷
无法添加数据库则退出再用此mysql -u root -p 管理员登录
exit 退出
cd /d 复制链接
i. show databases;(查看)
ii. create database 数据库名;(添加)
iii. drop database 数据库名; (删除)
use 数据库名;(使用数据库)
【create database test;
create database student;
use student;
show tables;
create table test(id int,name varchar(20));
show tables;】
字段名/类型
Field/Type
day 02:
单表查询:
查询所有字段:select * from [tb_name];
例:select * from [s_info];
查询部分字段:select 字段1,字段2 from [tb_name] ;
例:select [sno,sname] from [s_info];
限定条件查询
1.where:符合限定条件的行
2.关系运算符:
>(大于) <(小于) >=(大于等于) <=(小于等于) =(等于) !=(不等于) <>(大于不等于)
使用地方:跟在where后面
格式:select * from [tb_name] where sno<4;
例:select * from [s_info] where sno<4;
3.IN关键字
查询限定范围的行
格式:select * from [tb_info]
where 字段名 IN(取值1,取值2);
举例:select * from s_info
where ssex in('m','f');
4.between 关键字 and 关键字;
查询字段值介于值1与值2之间的行,>=值1并且<=值2
格式:select * from [s_info]
where 字段名 between 值1 and 值2;
举例:select * from [s_info]
where sbirth between '1998-1-1' and '1998-5-5';
5.is null/is not null 关键字
6.and(并且)、or(或者)、not关键字
逻辑运算符 并且、或者、否定
格式:select * from [tb_name]
where 字段名1 限定条件 and|or 字段名2 限定条件
举例:select * from [tb_name]
where sname='fbb' and sbirth='2000-1-1';
7.like 关键字
模糊限行条件查询
_:匹配任意一个字符
%:匹配0个或1个、多个字符
格式:select * from [tb_name]
where 字段名 like '_字符串',
举例:select * from [tb_name]
where sname like '_x';名字为两个字符,且以x结尾
排序
按顺序排序。升序ASC 降序DESC
格式:select * from [tb_name]
order by 字段名1 [ASC|DESC],字段名2 [ASC|DESC]
举例:select * from [tb_name]
order by sno DESC
单行函数查询
count()查询符合条件的行数
格式:select count(*) from [tb_name]
[where 条件]
举例:select count(*) from score
where cname='yw' and score<60;
sum(字段名) 查询对应字段的累加和
格式:select sum(字段名) from [tb_name]
[where 条件]
举例:select sum(score)from s_score;
where cname='yw';
avg(字段名) 查询对应字段的平均值
min(最小数) max(最大数) 查询对应字段最小值 最大值
对字符串作处理
length()-长度
Concat()-拼接
Substr()-截取
Trim()-去左右空格
对数字作处理
Abs(x)取绝对值
Round(x)四舍五入
对日期和时间作处理
Curdate()-返回当前日期
Now()-返回当前日期和时间
Date_format()-格式化时间
分组查询格式
select *|统计函数 from tb_name
where 字段名条件
group by 分组字段名
having 条件
odery by 字段名 desc|asc
约束条件 --建表语句中
primary key --主键,不重复
not null --不允许为空
unique --唯一
auto increment --自增
default --设置默认值
foreign key --外键
create table emp(id int null primary key)
举例:
create table student(id int not null primary key,
name varchar(20)default 'zs');
统计10以上的
select id_xi,count(*) from linxiao
group by id_xi
having count(*)>=10;
1.表约束--字段
用法:在创建表时使用,放在字段名,类型后面
a)primary key --主键约束=唯一(不重复)+不为空
举例:create table test(id int primary key)
b)Not null --不为空
NULL --不知道,≠0≠‘’
create table test(name varchar(20) not null);
c)default --设置默认值
d)
e)
2.分组查询
步骤:
1)显示的数据是什么? --select 后面
2)从哪个表去查 from 后面
3)限定条件 where 后面
4)以什么单位进行统计数据的 group by 后面
5)对统计数据进行筛选 having
where和having区别:
where是针对原表进行筛选
having是针对统计数据进行筛选
3.外键
a)概念 数据表(从表)中的一个字段数据来源于另一个表(主表)的字段(字段必须是主键)
b)语法
alter table 从表名 add constraint 约束名 foreign key (从表字段) references 主表(主键)
c)关系 s.id=s_score.sid
d)完整性
成绩表中的学号只能从学生表中来,如果不是,报错
如果成绩表中存在学号 n,不能删除学生表中学号为n的学生
e)删除约束
alter table 表名 drop foreign key 外建名
4.多表查询
5.子查询
===嵌套查询
from
where
having
查询语文成绩为100的学生姓名
select name from s_info
where sno=(select sno from score
where cname='yw' and score=100);
1)in
2)使用exists如果正确才能执行,如果不正确就不会执行。;==in exists 效率更高
3)any --满足其中一个条件
4)all --满足所有条件