例句 例句解释
connect scott/tiger 登录scott用户,密码是tiger

select ename from emp; 从emp表中查出ename列
select * from emp; 从emp表中查出所有列
select ename,sal from emp where ename=’SCOTT’; 从emp表中查出SCOTT的名字和工资
select distinct deptno from emp; 显示emp表中的部门编号,去掉重复的
“select ename,sal from emp
where sal between 1000 and 2000;” “显示emp表中工资在1000到2000之间
的员工姓名和工资”

“select ename,sal from emp
where sal=3000 or sal=5000;” “显示emp表中工资是3000或者工资是5000
的员工姓名和工资”
set linesize 200 sqlplus命令,设置行宽为200
create table t(id int); 创建一个名字叫t的表,有一列id列,类型是整数类型
create table t1(name varchar2(10)); 创建一个名字叫t1的表
alter table t add(age number(3)); 给表t增加一个名字叫age的字段,类型是number类型
drop table t; 删除表t
truncate table t; 清空表t中的数据
insert into t values(1); 向t表中插入一行数据
insert into t values(1); 向t表中插入一行值是1的数据
update t set name=’scott’ where id=1; 把t表中id=1的这条记录的name列改成scott
delete from t where id=1; 把t表中id=1的数据删除;
commit; 提交前面的事务
rollback 回滚前面的事务
create table t (name char(10)); 建一张表t,name列,字符类型,长度是10
create table t (name varchar(10)); 建一张表t,name列,可变字符串类型,长度是10
create table t (id number(10)); 建一张表t,id列,数值类型,长度是10
create table t (birthday date); 建一张表t,birthday列,日期类型
create table t (ctime timstamp); 建一张表t,ctime列,时间戳类型
select to_char(sysdate,’yyyy’) from dual; 显示当前年份
select to_char(sysdate,’month’) from dual; 显示当前月份
select to_char(sysdate,’dd’) from dual; 显示今天是几号
select to_char(sysdate,’hh24:mi:ss’) from dual; 显示现在是几点(24小时制)几分几秒

select to_char(sysdate,’day’) from dual; 显示今天周几
select length(ename) from emp; 显示员工名字的长度
“select ename,sal,
case
when deptno = 10 then ‘会计部’
when deptno = 20 then ‘研究部’
when deptno = 30 then ‘销售部’
else ‘其他部门’
end 部门
from emp;” “显示员工姓名,工资,部门,
如果部门编号是10,显示成’会计部’,
如果部门编号是20,显示成’研究部’,
如果部门编号是30,显示成’销售部’,
否则显示为’其它部门'”

“select ename,
sal,
decode(deptno, 10, ‘会计部’, 20, ‘研究部’, 30, ‘销售部’, ‘其他部门’) “”部门””
from emp;” 意思同上
select round(5.6) from dual; 5.6四舍五入成整数,结果6
alter table t modify column id number(20); 把表T的id列改成number类型,长度10
alter table t add(age number(3)); 给表t增加一个名字叫age的字段,类型是number类型
select next_day(sysdate,’星期三’) from dual; 从当前时间开始算起,下个周三是几月几号
select * from emp order by comm nulls first; null空值排在前面
select * from emp order by comm nulls last; 空值排在后面
select trim(‘ abc ‘) from dual; 去除字符串两边的空格
create table t(id int unique); 建表t,id列是整数类型,加唯一约束
create table t(id int primary key); 建表t,id列是主键列

“create table t(age number(3),
check(age >0 and age <200));” 建表t,age列大于0,小于200

create table t(id int constraint uk_t_id unique); 建表t,id列加唯一约束,约束名字叫uk_t_id

“merge into t
using t1
on (t.id=t1.id)
when matched then
update t.sal=t1.sal” 参考t1表,当两张表的id列相匹配时,将t.sal改为t1.sal的值

select ename,sal from emp order by sal; 显示员工的名字和工资,按工资从小到大排序
select deptno,count(*) from emp group by deptno; 按部门编号分组,求每个部门的人数
“select deptno,count(*) from emp group by deptno
having count(*)>2;” 按部门编号分组,求部门人数大于2个的部门和人数
select ename from emp where ename like ‘E\_%’ escape ‘\’; 查以E_开头的员工的名字,\是转义字符
“select e.ename,d.dname
from emp e left join dept d
where e.deptno=d.deptno” emp表左关联dept表,查员工名字和部门名字
“select e.ename,d.dname
from emp e right join dept d
where e.deptno=d.deptno” emp表右关联dept表,查员工名字和部门名字
“select e.ename,d.dname
from emp e join dept d
where e.deptno=d.deptno” emp表内连接dept表,查员工名字和部门名字
“select ename from emp
union
select name from t;” 合并emp表的ename列和t表的name列,去重
“select ename from emp
minus
select name from t;” 从emp表的ename列中减去emp表的ename列和t表的name中重复的部分
“select ename from emp
union all
select name from t;” 合并emp表的ename列和t表的name列,不去重
“select ename,sal from emp
where sal>any(select sal from emp where deptno=10);” 查工资大于部门10的任何一个工资的人