数据库基础学习2——数据查询

数据库基础学习2——数据查询

查询时的运算符

比较运算符

l  等于=

l  大于>

l  大于等于>=

l  小于<

l  小于等于<=

l  不等于!=或<>

逻辑运算符

l  and

l  or

l  not

模糊查询

l  like

l  %表示任意多个任意字符

l  _表示一个任意字符

范围查询

l  in表示在一个非连续的范围内

l  between ... and ...表示在一个连续的范围内

select * from students where id in(1,3,8);

select * from students where id between 3and 8;

空判断

l  is null

select * from students where hometown isnull;

外键的引入

外键的使用条件

①两个表必须是InnoDB表,MyISAM表暂时不支持外键

②外键列必须建立了索引,MySQL 4.1.2以后的版本在建立外键时会自动创建索引,但如果在较早的版本则需要显式建立;

③外键关系的两个表的列必须是数据类型相似,也就是可以相互转换类型的列,比如int和tinyint可以,而int和char则不可以;

外键引入的好处:

1、      在删除的时候通过外键可以进行级联删除。

2、       

需要注意的点:

RE关系为1对N时,我们将外键设置在N的一端。

RE关系为N:N时,我们需要另外建一个关系表来作为中间映射表

建表的时候引入

create table 表名(

       字段1  属性约束,

       字段2  属性约束,

       字段3  属性约束 ,

       PRIMARYKEY (字段名),

       KEY索引名  (字段名),

      CONSTRAINT 外键名 FOREIGN KEY (字段名)REFERENCES test (字段名)

)

eg:

create table papa(

       idint(10) NOT NULL AUTO_INCREMENT,

       s1varchar(10),

       s2varchar(10),

       fkint(11) unsigned,

       PRIMARYKEY (id),

       KEYfk (fk),

      CONSTRAINT fk_id_pa FOREIGN KEY (fk)REFERENCES test (id)

)

建表之后引入

步骤:

1、      设置表1的索引

alter table 表1 add index 索引名(字段名);

解释为:为表1添加一个建立在字段名上的索引,名字为索引名

2、      设置表2的索引

解释为:为表2添加一个建立在字段名上的索引,名字为索引名

3、      将表2的索引设置为表1的外键

alter table 表1  add constraint  外键名

foreign key(字段名)

references 表2(字段名);

为表1添加一个外键名字叫外键名,将表1的字段名与表2的字段名进行关联。

删除外键

 alter table book drop foreign key 外键名 ; 

级联操作

restrict(限制):默认值,抛异常(删除的时候若有级联就抛异常)

cascade(级联):如果主表的记录删掉,别从表中县关联的记录都被删除

set nll:将外键设置为null

no action:什么都不做

聚合

用于快速统计数据:

1、count(*)表示计算总行数

select count(*) from students;

2、max(列)表示求此列的最大值

select max(id) from students wheregender=0;

3、min(列)表示求此列的最小值

select min(id) from students wheregender=0;

4、sum(列)表示求此列的和

select sum(id) from students wheregender=1;

5、avg(列)表示求此列的平均值

select avg(id) from students whereisdelete=0 and gender=0;


分组

GROUP BY 语句根据一个或多个列对结果集进行分组。

目的是显示某个属性的统计结果

select 列1,列2,聚合... from 表名

group by 列1,列2,列3...

having 列1,...聚合...

eg:

select fk_class_id as 班级,count(*)from students group by fk_class_id having count(*)>2;

按照班级进行分组;

注意:这里having后面选择的字段,只能是group by 中的字段

排序

select * from 表名

order by 列1 asc|desc,列2asc|desc,...

注意:若同时有多个排序,则先按照第一个排,若有重复的数据再按照第二个排。

分页

为了防止查询数据过多,我们就一段一段地查询:

select * from 表名

limit start,count

eg:

select * from students

where isdelete=0

limit (n-1)*m,m

连接查询

select  students.sname, subjects.title, scores.scorefrom scores

  innerjoin students on scores.stuid=students.id

  inner join subjects onscores.stubid=subjects.id;

通过连接查询,我们查询外键对应表的数据。

我们可以对结果集进行处理:

selectstudents.sname,subjects.title,scores.score from scores

       innerjoin students on scores.stuid=students.id

       innerjoin subjects on scores.stubid=subjects.id

       wherestudents.sname="test";

注意:

这里谁jion谁并不是很重要,重要的是后面要描述清楚他们之间的关系。

Inner join:两个表都能匹配上的结果集查询出来

left  join: 以左边表为准,若没有匹配到,则用null填充

right join:以右边表为准,若没有匹配到,则用null填充

猜你喜欢

转载自blog.csdn.net/u011486491/article/details/79066028