Oracle的增删改查例子
--添加数据
insert into tb_student values(3,'王可儿','男',15,'12344576',null);
select * from tb_student;
--修改数据
update tb_student set sex='女' where id=1;
commit;
--删除数据
delete from tb_student where id=3;
select * from tb_student;
--提交
commit;
主键约束
--主键约束
create sequence my_sequence;
select my_sequence.nextval from dual;
select my_sequence.currval from dual;
insert into tb_student values(my_sequence.nextval ,'王可','男',15,'12344578',null);
select * from tb_student;
COMMIT;
--复制scott用户下的dept表的结构以及它的数据
create table dept as select * from scott.dept;
select * from dept;
create table emp as select * from scott.emp;
select * from emp;
--仅仅复制表结构
create table tb_emp as select * from scott.emp where 1 = 2;
/**
查询语句的语法:
select [all|distinct] *|列名列表|表达式|函数 from [模式名.]表名
[where 条件表达式]
[group by 列名]
[having 条件表达式]
[order by 列名|表达式 [asc|desc] ]
**/
-- 查询所有列 使用*
select * from emp;
-- 查询特定的列
select ename,job from emp;
-- 为列指定别名,as 可以省略
select ename as name , job j from emp e;
-- all和distinct的区别
select all job from emp;
-- distinct 消除重复的查询结果(也就是消除重复的行),所谓查询结果是以行为单位的
select count(distinct job) from emp;
-- 统计函数(聚合函数) count/sum/avg/max/min 聚合函数会忽略null
select count(empno),sum(sal),avg(sal),max(sal),min(sal) from emp;
-- where子句
/**
比较运算符 > < >= <= != <> =
范围 between 下限 and 上限、 in (集合)
模糊查询 like 通配符 %表示0到多个字符 _表示单个字符
是否为null : is null /is not null
逻辑运算符 : and 、 or 、 not
**/
-- 查询名字中包含A的员工信息
-- 查询名字以A开头、工资大于2000,且有奖金的员工信息
-- 查询入职时间在1990-12-20到今天的员工信息
-- 查询工作岗位是SALESMAN的员工信息
-- 子查询:在一个sql语句中包含一个查询语句,那么这个查询语句就是子查询
select * from emp where sal = (select sal from emp where ename = 'ALLEN');
-- mysql中外层sql语句如果delete、update、insert,那么子查询和外层sql语句不能操作相同的表,但是在Oracle中可以
delete from emp where deptno = (select deptno from emp where ename='ALLEN');
-- 查询员工信息,部门名称
select e.*,(select dname from dept d where d.deptno = e.deptno) from emp e ;
-- 查询部门名称是SALES/ACCOUNTING
-- 以下语句错误,因为在子查询结果作为比较运算符的运算条件时,子查询的查询结果不能多于一行
select * from emp where deptno = (select deptno from dept where dname in ('SALES','ACCOUNTING'));
-- 应该修改为
select * from emp where deptno in (select deptno from dept where dname in ('SALES','ACCOUNTING'));
-- 查询工资比smith和allen高的员工信息 通过两种方式解决 1、通过聚合函数max 或者 min 2、通过比较运算符的谓词 all/ any / some
select * from emp where sal > all (select sal from emp where ename = 'SMITH' or ename = 'ALLEN');
-- 查询没有员工的部门
select * from dept d where not exists (select 1 from emp e where d.deptno = e.deptno); --复制scott用户下的dept表的结构以及它的数据
create table dept as select * from scott.dept;
select * from dept;
create table emp as select * from scott.emp;
select * from emp;
--仅仅复制表结构
create table tb_emp as select * from scott.emp where 1 = 2;
/**
查询语句的语法:
select [all|distinct] *|列名列表|表达式|函数 from [模式名.]表名
[where 条件表达式]
[group by 列名]
[having 条件表达式]
[order by 列名|表达式 [asc|desc] ]
**/
-- 查询所有列 使用*
select * from emp;
-- 查询特定的列
select ename,job from emp;
-- 为列指定别名,as 可以省略
select ename as name , job j from emp e;
-- all和distinct的区别
select all job from emp;
-- distinct 消除重复的查询结果(也就是消除重复的行),所谓查询结果是以行为单位的
select count(distinct job) from emp;
-- 统计函数(聚合函数) count/sum/avg/max/min 聚合函数会忽略null
select count(empno),sum(sal),avg(sal),max(sal),min(sal) from emp;
-- where子句
/**
比较运算符 > < >= <= != <> =
范围 between 下限 and 上限、 in (集合)
模糊查询 like 通配符 %表示0到多个字符 _表示单个字符
是否为null : is null /is not null
逻辑运算符 : and 、 or 、 not
**/
-- 查询名字中包含A的员工信息
-- 查询名字以A开头、工资大于2000,且有奖金的员工信息
-- 查询入职时间在1990-12-20到今天的员工信息
-- 查询工作岗位是SALESMAN的员工信
-- 子查询:在一个sql语句中包含一个查询语句,那么这个查询语句就是子查询
select * from emp where sal = (select sal from emp where ename = 'ALLEN');
-- mysql中外层sql语句如果delete、update、insert,那么子查询和外层sql语句不能操作相同的表,但是在Oracle中可以
delete from emp where deptno = (select deptno from emp where ename='ALLEN');
-- 查询员工信息,部门名称
select e.*,(select dname from dept d where d.deptno = e.deptno) from emp e ;
-- 查询部门名称是SALES/ACCOUNTING
-- 以下语句错误,因为在子查询结果作为比较运算符的运算条件时,子查询的查询结果不能多于一行
select * from emp where deptno = (select deptno from dept where dname in ('SALES','ACCOUNTING'));
-- 应该修改为
select * from emp where deptno in (select deptno from dept where dname in ('SALES','ACCOUNTING'));
-- 查询工资比smith和allen高的员工信息 通过两种方式解决 1、通过聚合函数max 或者 min 2、通过比较运算符的谓词 all/ any / some
select * from emp where sal > all (select sal from emp where ename = 'SMITH' or ename = 'ALLEN');
-- 查询没有员工的部门
select * from dept d where not exists (select 1 from emp e where d.deptno = e.deptno);