Oracle数据库sql语句

1.创建用户、赋权限、删除用户

create user test identified by test 
    default test users
    temporary tablespace temp;
grant connect to test ; 
grant resource to test ;
grant dba to test ;
drop user test cascade;

2.修改用户密码&解除锁定(需要DBA权限)

 

alter user SCOTT identified by tiger;
alter user SCOTT account unlock;

 

3.查询锁表

SELECT'alter system kill session '''|| c.sid ||''||','|| c.serial# ||''';', a.object_id, a.session_id, b.object_name, c.*
     FROM v$locked_object a, dba_objects b, v$session c
    WHERE a.object_id = b.object_id
      AND a.SESSION_ID = c.sid(+)
      AND schemaname ='SCOTT'
     ORDER BY logon_time

4.左右连接去除笛卡尔积

 

 

关于左连接和右连接总结性的一句话:
左连接where只影向右表,右连接where只影响左表。
Left Join
select * from tbl1 Left Join tbl2 where tbl1.ID = tbl2.ID
左连接后的检索结果是显示tbl1的所有数据和tbl2中满足where 条件的数据。

 

5.增加新的主键约束(约束无法直接修改,只能先删后插)

alter table 表名 add constraint 主键名 primary key(字段名);

alter table DEPT  drop constraint PK_DEPT ;
alter table DEPT add constraint PK_DEPT primary key(DEPTNO, DNAME);

6.级联删除外键(删除父表记录时,同时删除子表记录)

ALTER TABLE 子表 ADD CONSTRAINT FK_ACTIVITY_ID FOREIGN KEY (ID) REFERENCES 父表 (ID) ON DELETE CASCADE;

7.修改表名(表名大小写问题)

ALTER TABLE "DEPT" RENAME TO DEPT;

8.将本用户下全部sequence查询出来,并拼成创建语句

select 'create sequence '||sequence_name||   
       ' minvalue '||min_value||   
       ' maxvalue '||max_value||   
       ' start with '||last_number||   
       ' increment by '||increment_by||   
       (case when cache_size=0 then ' nocache' else ' cache '||cache_size end) ||';' 
from user_sequences

 9.将本用户下全部表名与表名的注释查询出来:

select t.TABLE_NAME,c.comments from user_tables t,user_tab_comments c where t.TABLE_NAME=c.table_name order by t.TABLE_NAME asc

 

 

 

常用sql待续,欢迎留言补充

 

你可能感兴趣的:(Oracle数据库sql语句)