数据库学习:第二天

DQL查询语句

***1. 排序查询***-----------------------------------------

   * 语法:order by ( 默认为升序 )
     * order by 排序字段1,排序方式1, 排序字段2 排序方式2...
     * 排序方式:
       * 1. ASC:升序 (默认)
       * 2. DESC:降序
      * 按照数学升序,如果一样,则按照英语升序
        select * from student order by math asc ,english ASC;
      注意:如果有多个排序条件,则当前面的条件值一样时,才会判断第二条件
        
***2. 聚合函数***-----------------------------------------

   将一列数据作为一个整体,进行纵向的计算
   * 1. count:表示计算个数
		select count(name) from student;
		  1.一般选择非空的列:主键
   * 2. max  : 计算最大值
		select max(math) from student;
   * 3. min  : 计算最小值
		select min(math) from student;
   * 4. sum  : 计算和
		select sum(math) from student;
   * 5. avg  : 计算平均值 
		select avg(math) from student;
   * 6. 注意:聚合函数的计算会排除null值
			解决方案:
				1.选择不包含非空的列
				2.ifnull 函数替换
				
***3. 分组查询***-----------------------------------------

	* 语法 group by
	* 注意
		1.分组之后查询的字段只能为:分组字段,聚合函数
		  eg:select sex,avg(math) from student group by sex;
		2.where 和 having的区别
		  1. where在分组之前进行限定,如果不满足条件,不参与分组。having在分组之后进行限定,如果不满足结果,则不会被查询出来。
		  2. where后不可以跟聚合函数,having可以跟聚合函数的判断。
			   select sex,avg(math),count(id) from student where math > 70 group by sex having count(id) > 2;
			  可改为: select sex,avg(math),count(id) as counts from student where math > 70 group by sex having counts > 2;
		
***4. 分页查询***-----------------------------------------

	* 1. 语法:limit 开始的索引,每页查询的条数;
	* 2. 公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数
	  *  select * from student limit 0,3; --第一页
	  *  select * from student limit 3,3; --第二页
	  *  select * from student limit 6,3; --第三页
	* 3. limit是一个'方言',即 limit 只能在 mysql 中用 

约束

* 概念:对表中数据进行限定,保证数据的正确性,有效性和完整性
* 分类:
    * 1. 主键约束:primary key
    * 2. 非空约束:not null
    * 3. 唯一约束:unique
    * 4. 外键约束 :foreign key


* 非空约束:not null 值不能为空

	1. 创建表时进行约束
	    如:name varchar(20) not null;
	2. 创建完表后,添加非空约束
		alter table stu modify name varchar(20) not null;
	3. 删除name的非空约束
		alter table stu modify name varcher(20);
		
* 唯一约束:unique 值是惟一的

	1. 创建表时进行约束
		name varchar(20) unique;
	2. 创建完表后,添加唯一约束
		alter table stu modify name varchar(20) unique;
	3. 删除唯一约束
		alter table stu drop index name;
		
* 主键约束:primary key

  1. 注意
      1.含义:非空且唯一
      2.一张表只能有一个字段为主键
      3.主键就是表中记录的唯一标识
  2. 在创建表时添加约束
		ID int(10) primary key;
  3. 删除主键
		alter table stu drop primary key;
  4. 创建完表后,添加约束
		alter table stu modify ID int primary key;
  5. 自动增长:(一般配合int主键使用)
		* 概念:如果某一列是数值类型的,使用auto_increment 可以完成值的自动增长
		* 1.ID int(10) primary key auto_increment;
		* 2.alter table stu modify id int ;
		* 3.alter table stu modify id int auto_increment;
* 4. 外键约束 :foreign key,使表与表之间产生关系,保证数据正确
		* 在创建表时添加外键
			1.语法:添加外键,设置级联更新
			  create table 表名(
			   ...
			   外键列
			   constraint 外键名称 foreign key 外键列名称 references 从表名称(从表列名称) on update cascade;
			   );
			2.删除外键
			  alter table 表名 drop foreign key 外键名称;
			3.创建表之后,添加外键
			  alter table 表名 add constraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称)on update cascade;
			4.级联操作  :谨慎使用
				创外键后加上 on update cascade;级联更新
				外键后加上 on delete cascade;级联删除

数据库的设计

1.多表之间的关系
	1. 一对一:
	 * 如:人和身份证之间的关系
	2. 一对多或多对一
	 * 如:部门和员工
	3. 多对多
	 * 如:学生和课程
2.数据库设计的范式
 * 概念:设计数据库时,需要遵循的一些规范。要遵循后面范式,必须满足前面所有的范式。
 * 分类:
   * 1.第一范式(1NF):在关系数据库中,每张表的属性应该具有原子性。
   * 2.第二范式(2NF):在1NF基础上消除非主属性对主码的部分函数依赖,非码属性必须完全依赖于候选码。
   * 3.第三范式 (3NF):在2NF基础上消除传递依赖,任何非主属性不依赖于其它非主属性。
发布了6 篇原创文章 · 获赞 3 · 访问量 195

猜你喜欢

转载自blog.csdn.net/weixin_44951165/article/details/102772624