Oracle 10g 命令从点滴开始

来源:互联网 发布:痛快的哀艳 知乎 编辑:程序博客网 时间:2024/06/05 00:35

1、解锁一个用户,如过在安装数据库的时候,没有解锁用户,可在sqlplus中解锁,如下图:

      首先使用sys登录,然后使用  alter user 用户 account unlock;

2、表述一张表的结构:desc 表名     tip:varchar2()支持国际上任何国家的语言;

3、select  10*10 from dual; select sysdate from dual; dual这个是空表,用来计算使用。   双引号用来保存格式,oracle会自动把小写转换成大写,用双引号就不会转换了

4、总结select的语法:select deptno, max(sal) from emp

                                         where deptno >10

                                         group by deptno

                                         having max(sal) >1200

                                         order by deptno desc;

5、conn   用户名/密码@数据库名  连接数据库;  举个例子  conn scott/tiger@orcl

      如果少开了服务,就会报错,如下:

解决方法是,打开上面截图的服务即可;

6、edit命令的使用,可简写ed; 如果sql语句较长,你编写的时候出现了错误,使用edit命令,会事半功倍。edit会打开一个记事本,在记事本里面编辑;

编辑后保存并退出,这时候你编辑的内容会在窗口里面显示;加上“/”     回车,就执行了;

 7、join    当进行多表连接查询的时候,建议使用  join on ,使用on连接条件;

     where用来进行对查询结果进行过滤,不用来进行条件的连接, 这样使用on进行多表连接,where用来对结果条件过滤;

    eg    如下所示,后面的语句更清晰,join指定连接条件,而where用来过滤查询结果

select ename,dname from emp,dept where emp.deptno = dept.deptno and dname <> 'research';select ename dname from emp join dept on (emp.deptno = dept.deptno)where dname <>'research';


8、NVL命令      oracle默认不处理null值,对比如下语句;

第一条语句,当comm为null时,comm+sal也为空值;第二条语句会自动判断,如果comm为空,会默认使用0代替

select ename,deptno,comm+sal fromemp;select ename,deptno,sal+nvl(comm,0)from emp;

9、where 1=1      以及      where 1=0 的使用

先看两个sql语句

select ename,deptno from emp where  1=1;/*相当于select ename,deptno from emp*/select ename,deptno from empwhere 1=0;/*相当于 select ename,deptno from 空表*/
create table temp_emp as select * from emp where 1=0;意思是创建一个和emp列相同的表,不包括记录

10、UPDATE的用法      

  1. UPDATE emp SET sal = NULL WHERE deptno= 10;这个是最基本的update用法
  2. update emp@remote set sal = sal*1.1 where deptno = 10;这个是更新远程数据库的emp表
  3. UPDATE sales PARTITION (sales_q1_1999) s SET s.promo_id = 494 WHERE amount_sold > 1000;更新单一区间上的sale表
        

上面3个包括了常用的,以及不常用的,在复杂的就要关联多个表了,举个例子最明确

UPDATE employees a     SET department_id =         (SELECT department_id             FROM departments             WHERE location_id = '2100'),         (salary, commission_pct) =         (SELECT 1.1*AVG(salary), 1.5*AVG(commission_pct)           FROM employees b           WHERE a.department_id = b.department_id)     WHERE department_id IN         (SELECT department_id           FROM departments          WHERE location_id = 2900               OR location_id = 2700); 

11、GRANT 授权命令 ,比如使用oracle自带的用户scott就无法进行创建视图VIEW,因为此用户没有创建视图的权限;如下图

解决办法是使用sys登录,并授权给scott的create view的权限;具体命令如下

conn sys/admin as sysdba;grant create view to scott;


12、truncate、delete、drop的区别

首先truncate和delete的作用是删除表里面的所有数据,它们不会删除表的定义,而drop的作用是删除整个表;下面看看区别

原文:

           Removing rows with the TRUNCATE statement can be more efficient than dropping and re-creating a table. Dropping and re-creating a table invalidates dependent objects of the table, requires you to regrant object privileges on the table, and requires you to re-create the indexes, integrity constraints, and triggers on the table and respecify its storage parameters. Truncating has none of these effects.

   使用truncate删除数据的时候不会删除表的定义,这会比删除并重新创建表的效率更高。删除并重新创建表以及依赖它的对象,需要重新给表分配权限,索引,完整性约束,触发器以及重新设置存储参数。而使用TRUNCATE就不会这么麻烦;

    借鉴一下他人的解释: truncate清除数据,内存中表空间中其被删除数据的表占用的空间会被立即释放,相当于windows中用shift+delete删除数据,不能够恢复!delete删除到回收站了,可以恢复的;

 13、查询当前登录用户下面有什么表,什么视图,什么约束

select table_name from user_tables;select view_name from user_views;select constraint_name from user_contraints;
//查询数据库中有哪些用户 select username from all_users;  select username from dba_users;

14、备份数据库以及恢复用到的exp、imp示例(工作中足够用的参数)

EXP:完全导出该服务下username用户下的所有表结构、数据、视图、job、proc等。

常用的参数还有tables=() where=‘’ 等

exp username/password@service file='d:\expfile.dmp' statistics=none owner=username 
 IMP:导入新建的数据库下,可以实现数据库的转移,相当于恢复

imp username/password@service file='d:\expfile.dmp' full=y

如果想导出部分所有表结构以及所有表的一部分数据,这个好像只能在数据库服务器上实现。

15、为表建立分区可以提高查询速度(为表建立分区)

alter table tablename add PARTITION 8_2012 values less than (TO_DATE(' 2012-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) tablespace TABLESPACE





 

原创粉丝点击