当前位置: 代码迷 >> SQL >> oracle sql 话语(二)
  详细解决方案

oracle sql 话语(二)

热度:29   发布时间:2016-05-05 14:21:57.0
oracle sql 语句(二)
  1. 27建立序列:(注意,这里并没有出现说是哪个表里面的序列!!) ??
  2. ??CREATE?SEQUENCE?dept_deptid_seq ??
  3. ????????????????INCREMENT?BY?10 ??
  4. ????????????????START?WITH?120 ??
  5. ????????????????MAXVALUE?9999 ??
  6. ????????????????NOCACHE ??
  7. ????????????????NOCYCLE?? ??
  8. 28使用序列: ??
  9. ????????insert?into?dept(ID,NAME)?values(DEPT_ID_SEQ.nextval,'Administration'); ??
  10. 29建立索引://默认就是nonunique索引,除非使用了关键字:unique??
  11. ????????CREATE?INDEX?emp_last_name_idx?ON?employees(last_name); ??
  12. 30建立用户:(可能有错,详细查看帮助) ??
  13. ????????create?user??username(用户名) ??
  14. ????????identified?by?oracle(密码) ??
  15. ????????default?tablespace??data01(表空间名//默认存在system表空间里面) ??
  16. ????????quota?10M(设置大小,最大为unlimited)??on?表空间名//必须分配配额! ??
  17. 31创建角色:create?ROLE?manager ??
  18. ????赋予角色权限:grant?create?table,create?view?to?manage ??
  19. ????赋予用户角色:grant?manager?to?DENHAAN,KOCHHAR(?两个用户) ??
  20. 32分配权限: ??
  21. ????????GRANT??update?(department_name,?location_id) ??
  22. ????????ON?????departments ??
  23. ????????TO?????scott,?manager; ??
  24. ????回收权限 ??
  25. ????????REVOKE??select,?insert??
  26. ????????ON??????departments ??
  27. ??????
  28. 36从时间中提取年,月,日:使用函数extract ??
  29. ????????select?extract(year?from?sysdate)?year,extract(month?from?sysdate), ??
  30. extract(day?from?sysdate)?from?dual; ??
  31. 37使用函数得到数月之后的日期:to_yminterval(‘01-02’)表示加上1年2月,不能够到天!! ??
  32. ????????select?hire_date,hire_date?+to_yminterval('01-02')?as?hire_date_new?from?employees?where?department_id=20 ??
  33. ????得到多少天之后的日期:直接日期加数字! ??
  34. ????????select?hire_date?+3?from?employees?where?department_id=20 ??
  35. 38一般的时间函数: ??
  36. ????????MONTHS_BETWEEN?('01-SEP-95','11-JAN-94')--两个日期之间的月数,返回一个浮点数 ??
  37. ????????ADD_MONTHS?('11-JAN-94',6)--添加月数 ??
  38. ??????????NEXT_DAY?('01-SEP-95','FRIDAY')?--下一个星期五的日期 ??
  39. ????????LAST_DAY('01-FEB-95')--当月的最后一天! ??
  40. ????????ROUND(SYSDATE,'MONTH')?????????--四舍五入月 ??
  41. ????????ROUND(SYSDATE?,'YEAR')???????--四舍五入年 ??
  42. ????????TRUNC(SYSDATE?,'MONTH')????????--阶段月 ??
  43. ?????????TRUNC(SYSDATE?,'YEAR')????????--截断年?? ??
  44. 39?group语句:和高级的应用语句: ??
  45. ????????SELECT???department_id,?job_id,?SUM(salary),??COUNT(employee_id)?FROM?????employees ??
  46. ????????????????GROUP?BY?department_id,?job_id?; ??
  47. ????????使用having进行约束: ??
  48. ????????1.group?by?rollup:对n列组合得到n+1种情况 ??
  49. ????????SELECT???department_id,?job_id,?SUM(salary)????????FROM?????employees??WHERE????department_id?<?60?GROUP?BY?ROLLUP(department_id,?job_id); ??
  50. ????????2.group?by?cube:得到2的n次方种情况 ??
  51. ????????SELECT???department_id,?job_id,?SUM(salary)?FROM?????employees??WHERE????department_id?<?60?GROUP?BY?CUBE?(department_id,?job_id)?; ??
  52. ????????3.使用grouping得到一行中构成列的情况,只是返回1和0:是空的话就返回1,否则返回0(注意不要弄反了!) ??
  53. ????????SELECT???department_id?DEPTID,?job_id?JOB,??SUM(salary),????GROUPING(department_id)?GRP_DEPT,????GROUPING(job_id)?GRP_JOB ??
  54. ????????????????FROM?????employees?WHERE????department_id?<?50?GROUP?BY?ROLLUP(department_id,?job_id); ??
  55. ????????4.grouping?sets:根据需要得到制定的组合情况 ??
  56. ????????SELECT???department_id,?job_id,?manager_id,avg(salary)?FROM?????employees?GROUP?BY?GROUPING?SETS?((department_id,job_id),?(job_id,manager_id)); ??
  57. 40from中使用子查询:返回每个部门中大于改部门平均工资的与员工信息 ??
  58. ????????SELECT??a.last_name,?a.salary,?a.department_id,?b.salavg??FROM????employees?a,--下面的地方就是子查询了,主要返回的是一组数据! ??
  59. ?????????(SELECT???department_id,?AVG(salary)?salavg???FROM?????employees??GROUP?BY?department_id)?b ??
  60. ????????WHERE???a.department_id?=?b.department_id ??
  61. ????????AND?????a.salary?>?b.salavg; ??
  62. 41exists语句的使用: ??
  63. ????????SELECT?employee_id,?last_name,?job_id,?department_id ??
  64. ????????FROM???employees?outer--下面的?exists里面的select选择出来的是随便的一个字符或者数字都可以 ??
  65. ????????WHERE??EXISTS?(?SELECT?'X'???FROM???employees?WHERE??manager_id?=??outer.employee_id); ??
  66. 42厉害的with语句: ??
  67. ????????WITH? ??
  68. ?????????dept_costs??AS?(--定义了一个临时的表 ??
  69. ???????????????????SELECT??d.department_name,?SUM(e.salary)?AS?dept_total--其间定义了一个临时的列dept_total ??
  70. ???????????????????FROM????employees?e,?departments?d ??
  71. ???????????????????WHERE???e.department_id?=?d.department_id ??
  72. ???????????????????GROUP?BY?d.department_name),/*注意这里有逗号*/ ??
  73. ????????avg_cost????AS?( ??
  74. ???????????SELECT?SUM(dept_total)/COUNT(*)?AS?dept_avg ??
  75. ???????????FROM???dept_costs)--这里的第二张临时表里面就引用了前面定义的临时表和之间的列! ??
  76. ????????SELECT?*??FROM???dept_costs??WHERE??dept_total?>??(SELECT?dept_avg???FROM?avg_cost)?ORDER?BY?department_name;---最后的查询语句中使用了前面的临时表 ??
  77. 43遍历树: ??
  78. ????????SELECT?employee_id,?last_name,?job_id,?manager_id ??
  79. ????????FROM???employees ??
  80. ????????START??WITH??employee_id?=?101 ??
  81. ????????CONNECT?BY?PRIOR?manager_id?=?employee_id?;--自底向上的遍历树。 ??
  82. ??
  83. 44.更新语句 ??
  84. UPDATE?employees?SET? ??
  85. ????job_id?=?'SA_MAN',?salary?=?salary?+?1000,?department_id?=?120? ??
  86. ????WHERE?first_name||'?'||last_name?=?'Douglas?Grant';? ??
  87. ??
  88. UPDATE?TABLE?(SELECT?projs? ??
  89. ???????????FROM?dept?d?WHERE?d.dno?=?123)??p? ??
  90. ??SET?p.budgets?=?p.budgets?+?1? ??
  91. ??WHERE?p.pno?IN?(123,?456);??
  92. FROM????scott; ??
1.在数据字典查询约束的相关信息:SELECT constraint_name, constraint_type,search_conditionFROM        user_constraints WHERE        table_name = 'EMPLOYEES';        //这里的表名都是大写!2对表结构进行说明:   desc Tablename3查看用户下面有哪些表   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_typeFROM     user_objectsWHERE    object_name LIKE 'EMP%'     OR       object_name LIKE 'DEPT%'14查询对象类型:SELECT DISTINCT object_type FROM           user_objects ;17改变对象名:(表名,视图,序列)  rename  emp to emp_newTable18添加表的注释:  COMMENT ON TABLE employees IS 'Employee Information';20查看视图结构:   describe view_name23在数据字典中查看视图信息:  select viewe_name,text from user_views25查看数据字典中的序列:  select * from user_sequences33得到所有的时区名字信息:        select  * from v$timezone_names34显示对时区‘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_name22找到工资最高的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=2038一般的时间函数:        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 [email protected] ignore=y file=备份文件 log=D:\DBtest\db_bak\imp.log

?

exp [email protected] file=d:\daochu.dmp full=y

?

?12.大对象字段blob:查看blob字段的大小:

?? select ?dbms_lob.getLength (字段名)? from 表名?;?

?

13.下面收集的是有意思的sql语句,说不定正是你需要的:

?

Java代码 复制代码
  1. --创建一个只允许在工作时间访问的视图 ??
  2. create?or?replace?view?newviewemp ??
  3. as ??
  4. select?*?from?表名 ??
  5. ?where?exists(select?1?from?dual?where?sysdate?>= ??
  6. ???????to_date(to_char(sysdate,?'yyyy-mm-dd?')?||?'08:00:00',?'yyyy-mm-dd?hh24:mi:ss') ??
  7. ???and?sysdate?< ??
  8. ???????to_date(to_char(sysdate,?'yyyy-mm-dd?')?||?'18:00:00',?'yyyy-mm-dd?hh24:mi:ss')) ??
  9. ?
  相关解决方案