Orcale语句大全

1.desc(描述) emp    描述emp这张表 

2.desc    dept       部门表 

3.desc salgrade      薪水等级 

4.select *from table 查找表中的元素 

5.dual     是系统中的一张空表

6.select *from dual  

7.select sysdate from dual 取出系统时间  

8.select ename,sal*12 "annul sal"(取的别名) from emp; 查找用户姓名和用户的年薪 

9.任何含有空值的数学表达式的值都是空值  select ename,sal*12+comm from emp;  

10.select ename||sal from emp 其中的||相当于将sal全部转化为字符串 

11.表示字符串的方法  select ename ||'ajjf' from emp;  

12.如果其中有一个单引号就用2个单引号来代替他

select ename||'sakj' 'lds'from emp;  

13.select distinct deptno from emp     (去除部门字段中重复的部分,关键字distinct) 

14.select distinct deptno,job from emp;(去除这2个字段中重复的组合)  

15.select *from dept where deptno=10;     取出条件(取出部门编号为10的记录)  

16.select * from emp where ename='CLIRK'; 取出部门中姓名为clirk的记录(注意取出过程中ename用单引号隔开)  

17.select ename,sal from emp where sal>1500; 取出部门中薪水大于1500的人的姓名  

18.select ename,sal,deptno from emp where deptno<> 10 取出部门中的部门号不等于10的  

19.select ename,sal,deptno from emp where ename>'CBA' 取出部门中员工名字大于CBA的员工(实际比较的是ACIIS码)  

20.select ename,sal from emp where sal between 800 and 1500     select ename,sal from emp where sal>=800 and sal<=1500;   (取出800和1500之间的数) 

21.select ename,sal,comm from emp where comm is null (选出其中的空值)    select enmae,sal,comm from emp where comm is not null(选出其中的非空值) 

22.select ename,sal,comm from emp where sal in (800,1500,2000);取出这3者之中的     select ename,sal,comm from emp where ename in('simth');  

23.select ename,sal,hiredate from emp where hiredata>'3-04月-81';宣传符合条件的日期  

24.select ename,sal,from emp where sal>1000 or deptno=10;       找出工资薪水大于1000或者部门号等于10的员工

25.select ename,sal from emp where sal not in(500,1000);        查找薪水不在500到1000的员工姓名和月薪  

26.select ename,sal from emp where ename like '%ALL%';     select ename,sal from emp where ename like '_%A%';         查找姓名中含有ALL的客户信息,一个横线代表一个通配符  

27.select ename,sal from emp where ename like '_%$%%' escape '$'; 自己指定转易字符    select ename,sal from emp where ename like '_%\%%';             查找中间含有%相匹配的客户信息,运用转易字符  

28.select * from dept order by deptno                            对表中元素按部门号排序

select *from dept order by deptno desc                        默认为升序,可以用desc按降序  

29.select ename,sal from emp where sal <>1000 order by sal desc   按照查询条件来查询,并排序(asc升序排列)  

30.select ename,sal*12 from emp where ename not like '_%A%' and sal>800 order by sal desc  

31.select lower(ename) from emp 将ename都转化为小写    lower是函数能将字母转化为小写  

32.select ename from emp where lower(ename) like '_%a%'; 找出ename 中所有的含有a的字符 

33.select substr(ename,2,3) form emp            从第2个字符开始截取3个字符

34.select chr(65) from dual;          将65转化为字符  

35.select ascii('A') from dual         将ACSII码转化为字符串 

36.select round(23.565)from dual     四舍五入  36.select round(23,4565,2)from dual 四舍五入到第二位  

37.select to_char(sal,'$99.999.9999') from emp 按指定格式输出    select to_char(sal,'L99,999,9999') form emp L代表本地字符  

38.select hiredate from emp     select to_char(hiredate,'YYYY-MM-DD HH:MI:SS) from emp;          时间格式的显示    select to_char(sysdate,'YYYY-MM-DD HH:MI:ss) from dual;          十二小时制显示系统时间     select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS) from dual         二四小时制显示系统时间  

39.select ename,hiredate from emp where hiredate > to_date('2005-2-3 12:32:23','YYYY-MM-DD HH:MI:SS'); 

40 select sal from emp where sal>to_number('$1,250.00','$9,999.99'); 取出比它大的一切字符串(把特定格式的数字转化成字符)  

41 select ename,sal+nvl(comm,0) from emp;     讲comm值为空的用0来替换,单行函数(以一条记录为条件)一条对一条 

42.select Max(sal) from emp;    select Min(sal) from emp;    select avg(sal) from emp;     select sum(sal) from emp;     select count(*) from emp;                          查看表中一共有多少条记录    select count(*) from emp where deptno=10;          查找部门10一共有多少人; 

43.select avg(sal),deptno from emp group by deptno;   按部门号进行分组     select deptno,job,max(sal) from emp group by job,deptno; 按工作和部门号进行分组;  

44.select ename from emp where sal=(select max(sal) from emp); 子查询,查找部门中薪水最高的员工姓名

45.group by 注意:出现在select列表中的字段,如果没有出现在组函数中必须出现在group by子句中  

46.select avg(sal),deptno from emp group by deptno having avg(sal)>2000;    选出部门中平均薪水大于2000的部门,  

47.select * from emp where sal>100 group by deptno having ..........order by........    先取数据--过滤数据------分组----对分组限制-------排序  48.select avg(sal) from emp where sal>2000 group by deptno having avg(sal)>1500 order by

 

登录乐搏学院官网http://www.learnbo.com/

或关注我们的官方微博微信,还有更多惊喜哦~

转载于:https://my.oschina.net/learnbo/blog/910207

Oracle基本语句 1 进入界面 在cmd里面进入oracle的sqlplus界面:sqlplus scott/orcl@orcl 2 连接管理 连接命令 conn[ect] sys/orcl@orcl as sysdba 断开连接 disc[onnect] 修改密码 psssw[ord] 显示用户 show user 退出界面 exit 3 执行编辑sql语句 执行sql语句 start D:\1.sql 或者 @ D:\1.sql 编辑sql语句 edit D:\1.sql www.2cto.com 截取屏幕上的内容 spool D:\1.sql(开始截取) spool off(停止截取) 4 用户管理 创建用户 create user zhu identified by zhu 修改密码 alter user zhu identified by orcl 删除用户 drop user zhu(cascade) cascade代表删除这个用户对应的所有对象 赋予权限 grant create session to zhu grant all on emp to zhu 权限传递 grant all on emp to zhu with grant option(对象权限) 根表有关的权限 grant create session to zhu with admin option(系统权限)其他的权限 收回权限 revoke all on emp to zhu(株连制度) 5 用户口令管理profile 6 表操作 创建表 create table student(SNo number(4),Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) 修改表 添加一个字段 alter table student add(Address nvarchar2(100) [default value][null/not null]); 修改一个字段的长度 alter table student modify(Name nvarchar2(10),Address nvarchar2(10)); 修改一个字段的类型 alter table student modify(Name varchar2(10)); 修改一个字段的名称 alter table student rename column Name to Name2; 删除一个字段 alter table student drop column Salary; 修改表的名字 rename student to stu; 删除表 drop table student; delete from student;删除所有记录,表结构还在,写日志,可以恢复的,速度慢,Delete 的数据可以恢复。 查看表字段结构 desc student; 7 增删改查 增 insert into student(SNo,Name,Sex,Birthday,Salary) values(1002,'朱文锋','男','01-5月-12',2000); 修改日期的默认格式(临时修改,数据库重启后仍为默认;如要修改需要修改注册表) alter session set nls_date_format='yyyy-mm-dd'; insert into student(SNo,Name,Sex,Birthday,Salary) values(1002,'朱文锋','男','2010-12-12',2000); 插入部分字段和空值 快速加大表中数据 insert into student(SNo,Name,Sex,Birthday,Salary) select * from student; www.2cto.com 改 update student set Name='陈慧琳',Sex='女' where SNo=1002; 删 savepoint a; delete from student where SNo=1003; rollback to a; 总结:删除表的三种方式 delete from student; 删除所有记录,表结构还在,写日志,可以恢复的,速度慢。 truncate table student; 删除表中的所有记录,表结构还在,不写日志,无法找回删除的记录,速度快。 drop table student; 删除表的结构和数据。 查 开启计时 set timing on; 取消重复行 select distinct * from emp; 空值计算 select sal*13+nvl(comm,0)*13 as 年薪 from emp; 子查询(嵌套查询) 单行子查询 多行子查询 多列子查询 分页查询 合并查询 8 数据备份和恢复 备份(多表多文件加上大括号) 导出整个数据库 exp userid=system/orcl@orcl file=d:\all.dmp full=y log=d:\all.log 导出自己的方案 exp userid=scott/orcl@orcl owner=scott file=d:\scott.dmp log=d:\scott.log www.2cto.com 导出其它方案 exp userid=system/orcl@orcl owner=scott file=d:\scott2.dmp log=d:\scott2.log 导出自己的表 exp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp log=d:\emp.log 导出其它方案的表 exp userid=system/orcl@orcl tables=scott.emp file=d:\emp.dmp log=d:\emp.log 导出表的结构 exp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp rows=n log=d:\emp.log 使用直接导出方式 exp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp direct=y log=d:\emp.log 这种方式比默认的常规方式速度要快,当数据量大时,可以考虑使用这样的方法。 这时需要数据库的字符集要与客户端字符集完全一致,否则会报错 恢复(多表多文件加上大括号) 导入整个数据库 imp userid=system/orcl@orcl file=d:\all.dmp full=y log=d:\allimp.log ignore=y 导入自己的方案 imp userid=scott/orcl@orcl file=d:\emp.dmp log=d:\empimp.log 导入其它方案 imp userid=system/orcl@orcl file=d:\emp.dmp fromuser=system touser=scott log=d:\empimp.log 导入自己的表 imp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp 导入表到其它用户 imp userid=system/orcl@orcl tables=emp file=d:\emp.dmp fromuser=system touser=scott log=d:\empimp.log 导入表的结构 imp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp rows=n log=d:\empimp.log 导入数据 如果对象(如比表)已经存在可以只导入表的数据 imp userid=scott/orcl@orcl tables=emp file=d:\emp.dmp ignore=y log=d:\empimp.log 注意formuser是表本来属于哪个用户 touser现在传递给哪个用户 9 表空间 www.2cto.com 创建表空间 create tablespace zhu datafile 'C:\oracle\product\10.2.0\oradata\zhu.dbf' size 50m autoextend on next 50m maxsize unlimited extent management local; create user zhu identified by zhu default tablespace zhu; create table student(SNo number(4),Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) tablespace zhu; 知道表空间名,显示该表空间包括的所有表 select * from all_tables where tablespace_name='zhu'; 知道表名,查看该表属于那个表空间 select tablespace_name, table_name from user_tables where table_name='emp'; 删除表空间 drop tablespace zhu including contents and datafiles cascade constraints; 10 约束 not null unique primary key foreign key check alter table class add constraint class_key primary key (classid); 11 主键 自动增长 先创建一个表 create table student(SNo number(4) primary key,Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) 自定义一个sequence create sequence student_sequence increment by 1 start with 1 nomaxvalue nocycle nocache; www.2cto.com 创建一个触发器 create trigger student_trigger before insert on student for each row when(new.SNo is null) begin select student_sequence.nextval into:new.SNo from dual;end;/ 最近插入一行数据 insert into student(Name,Sex,Birthday,Salary) values('朱文锋','男','01-5月-12',2000); GUID 先创建一个表 create table student2(SNo char(32) primary key,Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) 然后插入一行数据 insert into student2(SNo,Name,Sex,Birthday,Salary) values(sys_guid(),'朱文锋','男','01-5月-12',2000); 在sql server中是newid()函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值