博客搬迁至博客园
目录
MySQL,Oracle,SQLserver,postgreSQL
数据库 | 字符客户端 |
---|---|
MySQL | mysql |
Oracle | sqlplus |
SQLserver | ADS(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 | 张三 | 1 | 2 | 2011-03-03 | 4400.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 字段名 between 值1 and 值2;
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 from 表1 join 表2 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”