SQL基础复习

博客搬迁至博客园


MySQL,Oracle,SQLserver,postgreSQL

数据库字符客户端
MySQLmysql
Oraclesqlplus
SQLserverADS(Azure Data Studio)
postgreSQL

一、表格创建示例

创建部门表

部门编号deptno部门名称dname部门所在位置location
1技术部一楼A区
2销售部二楼A区
3行政部一楼B区
create table dept(
	deptno int primary key, 
	dname varchar(9),
	loc varchar(10)
);

创建员工表

员工号empno姓名name部门deptno经理manager入职时间hiredate薪水salsry
1张三122011-03-034400.00
create table employees(
	empno int primary key,
	name char(10) not null,
	deptno int,
	manager int,
	hiredate date,
	salary numeric(7,2)
);

创建经理表

员工号头衔
2技术部经理
4销售部经理
5行政部经理
99总裁
create table managers(
	empno int primary key,
    title vaechar(16)
);

二、常用数据类型

数据类型说明
int整数类型
char(n)字符/字符串,固定长度n
varchar(n)字符/字符串,可变长度,最大长度n
numeric(a,b)精确数值,总位数a,小数点后位数b
date存储年、月、日的值

三、语句操作

insert插入

insert into 表名 values (字段1的值,字段2的值,....);

insert into 表名 (字段1,字段2,...) values (字段1的值,字段2的值,....);

select查询

select * from 表名;

select 字段1,字段2 from 表名;

distinct去重复值

select distinct 字段名 from 表名;

where条件过滤

select 字段名 from 表名 where 字段 运算符 值;

where子句中的运算符

运算符说明
=等于
<>或! =不等于
>大于
<小于
>=大于等于
<=小于等于
between在某个范围内
like搜索匹配的字符串模式

and和or运算符

select 字段名 from 表名 where 字段n 运算符 值n and(or) 字段m 运算符 值m;

select * from employees where deptno=3 and salary>=5000;

like搜索匹配的字符串

select 字段名 from 表名 where 字段 like 字符串;

select * from dept where loc like ‘一楼%;

in匹配多个值

select 字段名 from 表名 where 字段名 in (值1,值2,...);

select name from employees where deptno in (select deptno from dept where loc like ‘一楼%’);

between指定范围

select 字段名 from 表名 where 字段名 between1 and2;

select name from employees where hiredate not between '2013-01-01' and '2013-12-21';

order by排序

升序asc、降序desc

select 字段名 from 表名 order by 字段1,字段2,...asc;

select name,salary from employees order by salary desc;

update更新数据

update 表名 set 字段1=值1,字段2=值2,...where子句;

update employees set deptno=2,manager=4 where empno=4;

delete删除记录

delete from 表名 where子句;

delete from employees where empno=9;

index索引

create index 索引名 on 表名(字段1,字段2,...)
create index in_name on employees(name);

view视图

create view 视图名 as select语句;

create view employees_2015 as select name,salary from employees where hiredate < '2015-01-01';
select * from employees_2015;

null值

字段值是否是NULL的判断不能用=或者<>进行,要用is null或者is not null进行

字段和表的别名

别名有以下应用场景:
字段名长或可读性差时
涉及多个表,而且多个表中有相同的字段名时
代表查询函数的结果

别名的语法:
	select 字段名 as 别名 from 表名 as 别名;
	as可以省略
	
select name 姓名, salary 工资 from employees e;

join连接

内连接(inner join) :列出两个表中都存在的记录
左连接(left join) :即使没有匹配也列出左表中的所有记录
右连接(right join) :即使没有匹配也列出右表中的所有记录

select 字段名 as from1 join2 where子句;

select name,title from employees inner join managers on employees.empno=managers.empno;

SubQuery子查询

子查询也称嵌套查询,是一种嵌套在其他SQL语句的WHERE子句中的查询。
子查询用于为主查询返回其所需数据,或者对检索数据进行进一步的限制。

select 字段1,字段2,...from 表名 where 字段名 操作符 (子查询);

select name from employees where deptno in (select deptno from dept where loc like '二楼%');

select * from managers where empno not in (select empno from employees);

常用函数

count函数

count函数统计符合条件的记录数

count(*)统计表中的记录总数

count(字段名)统计指定字段不为null的记录数

下面的SQL语句统计员工表中的所有员工:
select count(*) from employees;

下面的SQL语句统计员工表中所有部门号不为null的员工:
select count(deptno) from employees;

下面的SQL语句查询员工表中部门号为nul的员工:
select * from employees where deptno is null;

max、min、avg、sum函数

max(字段名),最大
min(字段名),最小
avg(字段名),平均
sum(字段名),总值

select min(salaey) from employees;

group by分组

group by语句用于结合统计函数,根据一个或多个列对结果集进行分组。

select 字段名,统计函数 from 表名 where子句 group by 字段名;

select deptno,avg(salary) from employees group by deptno;
select deptno,avg(salary) from employees group by deptno having avg(salary)<4500;

having过滤分组

having子句和where子句类似,都是对查询的结果集进行过滤。它们的过滤对象不同,
where子句对被选择的列进行过滤,而having子句则对group by子句所产生的组进行过滤。

select 字段名,统计函数 from 表名 where子句 group by 字段名 having 统计函数 运算符 值;

select deptno,avg(salary) from employees group by deptno having avg(salary)<4500;

var code = “64d3f2f1-ff8c-43f1-9873-e6feca601ee0”

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Swaynie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值