create database Test创建数据库
use Tset是进入这个数据库
drop table表名 删除一张表
delete *from 表名 删除一条记录
insert into 表名 values()增加一条数据
update 表名 set 列名=新数据 where 列名 = ‘条件’ 更新数据
select top 3 *from emp order by sal
distinct是去重
Union是去重 union all不去重联表
left join和join区别 left是左边即使没有右边对应 也会显示
select e.* from emp e where not exists(
select 1 from hist h where h.eid=e.eid
)
exists返回真假
identity是自动增长 创建主键需要
check是约束
desc是降序
asc是升序
top n是取前n名
like是模糊查询
是先执行order by 再取前三
select * from emp;
-- * 表示所有的
-- from emp 表示从emp表查询
select empno, ename from emp;
select ename, sal from emp;
select ename, sal*12 as "年薪" from emp;
--as 可以省略 记住: "年薪" 不要写成'年薪' 也不要写成 年薪
select ename, sal*12 as "年薪", sal "月薪", job from emp;
select 888 from emp;
--ok
--输出的行数是emp表的行数 每行只有一个字段,值是888
select 5; --ok
--不推荐
select deptno from emp; --14行记录 不是3行记录
去重个查询
select distinct deptno from emp; --distince deptno 会过滤掉重复的deptno
select distinct comm from emp; --distinct也可以过滤掉重复的null 或者说如果有多个null 只输出一个
select distinct comm, deptno from emp;--把comm和deptno的组合进行过滤
select deptno, distinct comm from emp;--error 逻辑上有冲突
select 10000 from emp; --14行记录
between查询
--查找工资在1500到3000之间(包括1500和3000)的所有的员工的信息
select * from emp
where sal>=1500 and sal<=3000
等价于
select * from emp
where sal between 1500 and 3000
--查找工资在小于1500或大于3000之间的所有的员工的信息
select * from emp
where sal<1500 or sal>3000
等价于
select * from emp
where sal not between 1500 and 3000
in查询
select * from emp where sal in (1500, 3000, 5000)
等价于
select * from emp
where sal=1500 or sal=3000 or sal=5000
select * from emp where sal not in (1500, 3000, 5000) --把sal既不是