1.在数据字典查询约束的相关信息: SELECT constraint_name, constraint_type,search_condition FROM user_constraints WHERE table_name = 'EMPLOYEES'; //这里的表名都是大写! 2对表结构进行说明: desc Tablename 3查看用户下面有哪些表 select table_name from user_tables; 4查看约束在那个列上建立: SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'EMPLOYEES'; 10结合变量查找相关某个表中约束的相关列名: select constraint_name,column_name from user_cons_columns where table_name = '&tablename' 12查询数据字典看中间的元素: SELECT object_name, object_type FROM user_objects WHERE object_name LIKE 'EMP%' OR object_name LIKE 'DEPT%' 14查询对象类型: SELECT DISTINCT object_type FROM user_objects ; 17改变对象名:(表名,视图,序列) rename emp to emp_newTable 18添加表的注释: COMMENT ON TABLE employees IS 'Employee Information'; 20查看视图结构: describe view_name 23在数据字典中查看视图信息: select viewe_name,text from user_views 25查看数据字典中的序列: select * from user_sequences 33得到所有的时区名字信息: select * from v$timezone_names 34显示对时区‘US/Eastern’的时区偏移量 select TZ_OFFSET('US/Eastern') from DUAL--dual英文意思是‘双重的’ 显示当前会话时区中的当前日期和时间: ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';--修改显示时间的方式的设置 ALTER SESSION SET TIME_ZONE = '-5:0';--修改时区 SELECT SESSIONTIMEZONE, CURRENT_DATE FROM DUAL;--真正有用的语句! SELECT CURRENT_TIMESTAMP FROM DUAL;--返回的时间是当前日期和时间,含有时区 SELECT CURRENT_TIMESTAMP FROM DUAL;--返回的时间是当前日期和时间,不含有时区!!! 35显示数据库时区和会话时区的值: select datimezone,sessiontimezone from dual; 13普通的建表语句: CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(14), loc VARCHAR2(13)); 15使用子查询建立表: CREATE TABLE dept80 AS SELECT employee_id, last_name, salary*12 ANNSAL, hire_date FROM employees WHERE department_id = 80; 6添加列:// alter table EMP add column (dept_id number(7));错误!! alter table EMP add (dept_id number(7)); 7删除一列: alter table emp drop column dept_id; 8添加列名同时和约束: alter table EMP add (dept_id number(7) constraint my_emp_dept_id_fk references dept(ID)); 9改变列://注意约束不能够修改 的!! alter table dept80 modify(last_name varchar2(30));//这里使用的是modify而不是alter! 24增加一行: insert into table_name values(); 5添加主键: alter Table EMP add constraint my_emp_id_pk primary key (ID); 11添加一个有check约束的新列: alter table EMP add (COMMISSION number(2) constraint emp_commission_ck check(commission>0)) 16删除表: drop table emp; 19创建视图: CREATE VIEW empvu80 AS SELECT employee_id, last_name, salary FROM employees WHERE department_id = 80; 21删除视图: drop view view_name 22找到工资最高的5个人。(top-n分析)(行内视图) select rownum,employee_id from (select employee_id,salary from employees order by salary desc) where rownum<5; 26建立同义词: create synonym 同义词名 for 原来的名字 或者 create public synonym 同义词名 for 原来的名字 27建立序列:(注意,这里并没有出现说是哪个表里面的序列!!) CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE 28使用序列: insert into dept(ID,NAME) values(DEPT_ID_SEQ.nextval,'Administration'); 29建立索引://默认就是nonunique索引,除非使用了关键字:unique CREATE INDEX emp_last_name_idx ON employees(last_name); 30建立用户:(可能有错,详细查看帮助) create user username(用户名) identified by oracle(密码) default tablespace data01(表空间名//默认存在system表空间里面) quota 10M(设置大小,最大为unlimited) on 表空间名//必须分配配额! 31创建角色:create ROLE manager 赋予角色权限:grant create table,create view to manage 赋予用户角色:grant manager to DENHAAN,KOCHHAR( 两个用户) 32分配权限: GRANT update (department_name, location_id) ON departments TO scott, manager; 回收权限 REVOKE select, insert ON departments FROM scott; 36从时间中提取年,月,日:使用函数extract select extract(year from sysdate) year,extract(month from sysdate), extract(day from sysdate) from dual; 37使用函数得到数月之后的日期:to_yminterval(‘01-02’)表示加上1年2月,不能够到天!! select hire_date,hire_date +to_yminterval('01-02') as hire_date_new from employees where department_id=20 得到多少天之后的日期:直接日期加数字! select hire_date +3 from employees where department_id=20 38一般的时间函数: MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')--两个日期之间的月数,返回一个浮点数 ADD_MONTHS ('11-JAN-94',6)--添加月数 NEXT_DAY ('01-SEP-95','FRIDAY') --下一个星期五的日期 LAST_DAY('01-FEB-95')--当月的最后一天! ROUND(SYSDATE,'MONTH') --四舍五入月 ROUND(SYSDATE ,'YEAR') --四舍五入年 TRUNC(SYSDATE ,'MONTH') --阶段月 TRUNC(SYSDATE ,'YEAR') --截断年 39 group语句:和高级的应用语句: SELECT department_id, job_id, SUM(salary), COUNT(employee_id) FROM employees GROUP BY department_id, job_id ; 使用having进行约束: 1.group by rollup:对n列组合得到n+1种情况 SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY ROLLUP(department_id, job_id); 2.group by cube:得到2的n次方种情况 SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY CUBE (department_id, job_id) ; 3.使用grouping得到一行中构成列的情况,只是返回1和0:是空的话就返回1,否则返回0(注意不要弄反了!) SELECT department_id DEPTID, job_id JOB, SUM(salary), GROUPING(department_id) GRP_DEPT, GROUPING(job_id) GRP_JOB FROM employees WHERE department_id < 50 GROUP BY ROLLUP(department_id, job_id); 4.grouping sets:根据需要得到制定的组合情况 SELECT department_id, job_id, manager_id,avg(salary) FROM employees GROUP BY GROUPING SETS ((department_id,job_id), (job_id,manager_id)); 40from中使用子查询:返回每个部门中大于改部门平均工资的与员工信息 SELECT a.last_name, a.salary, a.department_id, b.salavg FROM employees a,--下面的地方就是子查询了,主要返回的是一组数据! (SELECT department_id, AVG(salary) salavg FROM employees GROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary > b.salavg; 41exists语句的使用: SELECT employee_id, last_name, job_id, department_id FROM employees outer--下面的 exists里面的select选择出来的是随便的一个字符或者数字都可以 WHERE EXISTS ( SELECT 'X' FROM employees WHERE manager_id = outer.employee_id); 42厉害的with语句: WITH dept_costs AS (--定义了一个临时的表 SELECT d.department_name, SUM(e.salary) AS dept_total--其间定义了一个临时的列dept_total FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name),/*注意这里有逗号*/ avg_cost AS ( SELECT SUM(dept_total)/COUNT(*) AS dept_avg FROM dept_costs)--这里的第二张临时表里面就引用了前面定义的临时表和之间的列! SELECT * FROM dept_costs WHERE dept_total > (SELECT dept_avg FROM avg_cost) ORDER BY department_name;---最后的查询语句中使用了前面的临时表 43遍历树: SELECT employee_id, last_name, job_id, manager_id FROM employees START WITH employee_id = 101 CONNECT BY PRIOR manager_id = employee_id ;--自底向上的遍历树。 44.更新语句 UPDATE employees SET job_id = 'SA_MAN', salary = salary + 1000, department_id = 120 WHERE first_name||' '||last_name = 'Douglas Grant'; UPDATE TABLE (SELECT projs FROM dept d WHERE d.dno = 123) p SET p.budgets = p.budgets + 1 WHERE p.pno IN (123, 456);
11.导入导出dmp文件:
imp 用户名/密码@数据库 ignore=y file=备份文件 log=D:\DBtest\db_bak\imp.log
exp system/manager@TEST file=d:\daochu.dmp full=y
12.大对象字段blob:查看blob字段的大小:
select dbms_lob.getLength (字段名) from 表名 ;
13.下面收集的是有意思的sql语句,说不定正是你需要的: