《SQL必知必会》读书笔记,30分钟入门SQL!(二)

简介: 本篇文章是 《SQL 必知必会》 的读书笔记,SQL必知必会的英文名叫做 Sams Teach Yourself in 10 Minutes。但是,我肯定是不能够在10分钟就能学会本书所有涉及到的sql,所以就起个名字叫30分钟学会SQL语句。目前手边的数据库是 mysql,所以以下示例均是由 mysql 演示。由于现在大部分工具都支持语法高亮,所以以下关键字都使用小写。

数据过滤

# 找到学号为1的学生
select * from student where number = 1;
# 找到学号为在 [1, 10] 的学生(闭区间)
select * from student where number between 1 and 10;
# 找到未设置电子邮箱的学生
# 注意不能使用 =
select * from student where email is null;
# 找到一班中大于23岁的学生
select * from student where class_id = 1 and age > 23;
# 找到一班或者大于23岁的学生
select * from student where class_id = 1 or age > 22;
# 找到一班与二班的学生
select * from student where class_id in (1, 2);
# 找到不是一班二班的学生
select * from student where class_id not in (1, 2);


计算字段

CONCAT

select concat(name, '(', age, ')') as nameWithAge from student;
select concat('hello', 'world') as helloworld;


Math

select age - 18 as relativeAge from student;
select 3 * 4 as n;


更多函数可以查看 API 手册,同时也可以自定义函数(User Define Function)。

可以直接使用 select 调用函数

select now();
select concat('hello', 'world');


数据汇总

聚集函数,一些对数据进行汇总的函数,常见有 COUNTMINMAXAVGSUM 五种。

# 统计1班人数
select count(*) from student where class_id = 1;


数据分组

使用 group by 进行数据分组,可以使用聚合函数对分组数据进行汇总,使用 having 对分组数据进行筛选。

# 按照班级进行分组并统计各班人数
select class_id, count(*) from student group by class_id;
# 列出大于三个学生的班级
select class_id, count(*) as cnt from student
group by class_id having cnt > 3;


子查询

# 列出软件工程班级中的学生
select * from student where class_id in (
  select id from class where class_id = '软件工程'
);


关联联接

虽然两个表拥有公共字段便可以创建联接,但是使用外键可以更好地保证数据完整性。比如当对一个学生插入一条不存在的班级的时候,便会插入失败。一般来说,联接比子查询拥有更好的性能。

# 列出软件工程班级中的学生
select * from student, class
where student.class_id = class.id and class.name = '软件工程';


内联接

内联接又叫等值联接。

# 列出软件工程班级中的学生
select * from student
inner join class on student.class_id = class.id
where class.name = '软件工程';


自联接

# 列出与张三同一班级的学生
select * from student s1
inner join student s2 on s1.class_id = s2.class_id
where s1.name = '张三';


外联接

--列出每个学生的班级,弱没有班级则为null
select name, class.name from student
left join class on student.class_id = class.id;


插入数据

可以采用以下方法插入一条数据,不过严重依赖表中列的顺序关系,推荐指定列名插入数据,并且可以插入部分列。

# 插入一条数据
insert into student values(8, '陆小凤', 24, 1, 3);
insert into student(name, age, sex, class_id)
values(9, '花无缺', 25, 1, 3);


修改数据

更新

# 修改张三的班级
update student set class_id = 2 where name = '张三';


删除

# 删除张三的数据
delete from student where name = '张三';
# 删除表中所有数据
delete from student;
# 更快地删除表中所有数据
truncate table student;


创建表与更新表

# 创建学生表
create table student (
  id int(11) not null auto_increment,
  name varchar(50) not null,
  age smallint default 20,
  sex enum('male', 'famale'),
  score tinyint comment '入学成绩',
  class_id int(11),
  createTime timestamp default current_timestamp,
  primary key (id),
  foreign key (class_id) references class (id)
);
# 根据旧表创建新表
create table student_copy as select * from student;
# 删除 age 列
alter table student drop column age;
# 添加 age 列
alter table student add column age smallint;
# 删除学生表
drop table student;
相关文章
|
14天前
|
SQL 存储 数据管理
SQL数据库的使用指南:从入门到精通
随着信息技术的飞速发展,数据库已成为各类企业和组织不可或缺的一部分。作为最流行的数据库管理系统之一,SQL数据库广泛应用于各种场景,如数据存储、数据管理、数据分析等。本文将详细介绍SQL数据库的使用方法,帮助初学者快速入门,并帮助有经验的开发者深化理解。一、SQL数据库基础首先,我们需要理解SQL数
64 2
|
14天前
|
SQL 存储 数据库
初识SQL数据库教程——从入门到精通
随着信息技术的飞速发展,数据库技术已成为计算机领域的重要组成部分。作为最流行的数据库管理系统之一,SQL数据库广泛应用于各类企业和组织的数据管理中。本文将带领读者从入门到精通,学习SQL数据库的相关知识。一、SQL数据库概述SQL(StructuredQueryLanguage)是一种用于管理关系数
31 2
|
16天前
|
SQL 安全 关系型数据库
SQL自动化注ru-SQLmap入门操作(一)
SQL自动化注ru-SQLmap入门操作(一)
|
16天前
|
SQL 安全 关系型数据库
SQL自动化注茹-SQLmap入门操作(二)
SQL自动化注茹-SQLmap入门操作(二)
|
1月前
|
SQL 安全 数据库
从入门到精通:Python Web安全守护指南,SQL注入、XSS、CSRF全防御!
【9月更文挑战第13天】在开发Python Web应用时,安全性至关重要。本文通过问答形式,详细介绍如何防范SQL注入、XSS及CSRF等常见威胁。通过使用参数化查询、HTML转义和CSRF令牌等技术,确保应用安全。附带示例代码,帮助读者从入门到精通Python Web安全。
66 6
|
2月前
|
Java 数据库连接 数据库
告别繁琐 SQL!Hibernate 入门指南带你轻松玩转 ORM,解锁高效数据库操作新姿势
【8月更文挑战第31天】Hibernate 是一款流行的 Java 持久层框架,简化了对象关系映射(ORM)过程,使开发者能以面向对象的方式进行数据持久化操作而无需直接编写 SQL 语句。本文提供 Hibernate 入门指南,介绍核心概念及示例代码,涵盖依赖引入、配置文件设置、实体类定义、工具类构建及基本 CRUD 操作。通过学习,你将掌握使用 Hibernate 简化数据持久化的技巧,为实际项目应用打下基础。
117 0
|
2月前
|
SQL 关系型数据库 数据挖掘
SQL 基础入门简直太重要啦!从零开始,带你轻松掌握数据查询与操作,开启数据世界大门!
【8月更文挑战第31天】在数字化时代,数据无处不在,而 SQL(Structured Query Language)则是开启数据宝藏的关键钥匙。无论你是编程新手还是数据处理爱好者,掌握 SQL 都能帮助你轻松提取和分析信息。SQL 简洁而强大,像一位魔法师,能从庞大数据库中迅速找到所需数据。从查询、条件筛选到排序、分组,SQL 功能多样,还能插入、更新和删除数据,助你在数据海洋中畅游无阻。
38 0
|
存储 安全 编译器
[笔记]读书笔记 C++设计新思维《一》基于策略的类设计(下)
[笔记]读书笔记 C++设计新思维《一》基于策略的类设计(下)
|
存储 算法 编译器
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
67 1
|
存储 算法 Java
[笔记]读书笔记 C++设计新思维《二》技术(Techniques)(二)
[笔记]读书笔记 C++设计新思维《二》技术(Techniques)(二)