`

Oracle常用SQL及命令

阅读更多

1.启动和关闭监听lsnrctl start 和 lsnrctl stop

2.启动和关闭数据库  startup 和shutdown

3.启动sqlplus   sqlplus /nolog  退出  quit

4.登陆oracle conn/connect username/password [as sysdba]

5.新建或者修改视图 create or replace view v_xxx as select a,b,c from t_xxx;

6.重命名列 alter table t_xxx rename COLUMN XXX_1 TO XXX_2;

7.删除用户 drop user xxx cascade;

8.创建用户并指定表空间 create user xxx identified by <password> default tablespace ts_xxx temporary tablespace ts_xx;

  如果该用户需要在该表空间创建表等操作还需要授权。  grant unlimited tablespace to xxx;否则会报ORA-01950: 对表空间‘ts_xxx’无权限

9.授权给用户xxx:  grant connect/dba/resource to xxx;取消授权用户:revoke connect/dba/resource from xxx;

10.指定.sql文件 start "d:/xxx.sql" 或者 @"d:/xxx.sql"

11.授权给用户  grant create session,create any table,create any index, create any view, create any procedure ,

    alter any table, alter any procedure,

    drop any table, drop any procedure, DROP ANY VIEW, DROP ANY INDEX, DROP ANY PROCEDURE,

    select any table, insert any table, update any table, delete any table to xxx;

12.查看所用用户 select * from DBA_USERS;

                SELECT * FROM ALL_USERS;

  SELECT * FROM USER_USERS;

13.创建表并指定表空间 CREATE TABLE LM_CITY 

     ID  NUMBER,

     CITY_ID NUMBER,

     CITY_NAME VARCHAR2(50),

     CITY_NAME_PINYIN VARCHAR2(100),

     UPDATE_TIME TIMESTAMP DEFAULT SYSDATE

)

TABLESPACE ZW1840;

14.在某个表中创建唯一索引:create unique index index_xxx on t_xxx(column_xxx);

15.在某个表中创建主键:  alter table t_xxx add constraint pk_xxx primary key (column_xxx);一个表只能有一个主键,也只能有一个LONG类型字段。(主键和unique都会顺带建立unique索引)

16.在某个表中添加外键:  alter table t_xxx add constraint fk_xxx foreign key (column_xxx) references tn_xxx (column_xx) [on delete set null/cascade];

17.在某个表中添加unique: alter table t_xxx add constraint uk_xxx unique (column_xxx);

18.默认时间sysdate;  date to char: to_char(date,'格式'),e.g. to_char(sysdate,'YYYY-MM-DD HH24:MI:SS');char to date to_date(txt,'格式')

19.修改某个表某一列的默认值: alter table t_xxx modify column_xxx default xxx; e.g. alter table LM_CITY MODIFY UPDATE_TIME DETAULT SYSDATE;

20.某个表中增加一列:alter table T_XXX add COL_XXX number(20);

21.查看一个表的表结构DESCRIBE T_XXX; DESCRIBE 为命令,不是SQL关键字,如在command window 下面执行 describe user_source。

22.用户创建的存储过程或者函数均在该用户下的USER_SOURCE表中。查看存储过程的代码 SELECT text from user_source where name = 'SP_XXX';

22.更改某一字段的类型:alter table t_xxx modify col_xxx type;需要说明的是,clob和varchar不能直接更改,需要新增中间字段,重命名的方式实现。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics