ALTER SEQUENCE <sequence_name> INCREMENT BY<integer>; ALTER SEQUENCE seq_inc_by_ten INCREMENT BY10;
改变序列的最大值
1 2
ALTER SEQUENCE <sequence_name> MAXVALUE <integer>; ALTER SEQUENCE seq_maxval MAXVALUE 10;
设置序列循环或不循环
1 2
ALTER SEQUENCE <sequence_name><CYCLE| NOCYCLE>; ALTER SEQUENCE seq_cycle NOCYCLE;
配置序列以缓存值
1 2
ALTER SEQUENCE <sequence_name> CACHE <integer>| NOCACHE; ALTER SEQUENCE seq_cache NOCACHE;
设置是否按顺序返回值
1 2 3
ALTER SEQUENCE <sequence_name><ORDER| NOORDER>; ALTER SEQUENCE seq_order NOORDER; ALTER SEQUENCE seq_order;
从字符串生成查询
有时需要从字符串创建查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
PROCEDURE oracle_runtime_query_pcd IS TYPE ref_cursor ISREFCURSOR; l_cursor ref_cursor;
v_query varchar2(5000); v_name varchar2(64); BEGIN v_query :='SELECT name FROM employee WHERE employee_id=5'; OPEN l_cursor FOR v_query; LOOP FETCH l_cursor INTO v_name; EXIT WHEN l_cursor%NOTFOUND; END LOOP; CLOSE l_cursor; END;
这是一个如何完成动态查询的非常简单的示例
字符串操作
1
length( string1 );
1
SELECT length('hello world') FROM dual;
这将返回 11,因为参数由 11 个字符组成,包括空格
1 2 3 4
SELECT lengthb('hello world') FROM dual; SELECT lengthc('hello world') FROM dual; SELECT length2('hello world') FROM dual; SELECT length4('hello world') FROM dual;
SELECT table_name, constraint_name, constraint_type FROM user_constraints;
选择参照约束
以下语句显示了源和目标表/列对的所有引用约束(外键):
1 2 3 4 5 6 7 8 9 10 11 12
SELECT c_list.CONSTRAINT_NAME as NAME, c_src.TABLE_NAME as SRC_TABLE, c_src.COLUMN_NAME as SRC_COLUMN, c_dest.TABLE_NAME as DEST_TABLE, c_dest.COLUMN_NAME as DEST_COLUMN FROM ALL_CONSTRAINTS c_list, ALL_CONS_COLUMNS c_src, ALL_CONS_COLUMNS c_dest WHERE c_list.CONSTRAINT_NAME = c_src.CONSTRAINT_NAME AND c_list.R_CONSTRAINT_NAME = c_dest.CONSTRAINT_NAME AND c_list.CONSTRAINT_TYPE ='R'
CREATE INDEX customer_idx ON supplier (customer_name, country);
以下内容在创建索引时收集统计信息:
1 2 3
CREATE INDEX customer_idx ON supplier (customer_name, country) COMPUTE STATISTICS;
创建基于函数的索引
在 Oracle 中,您不仅限于在列上创建索引。您可以创建基于函数的索引
创建基于函数的索引的语法是:
1 2 3
CREATE [UNIQUE] INDEX index_name ON table_name (function1, function2, . function_n) [ COMPUTE STATISTICS ];
例如:
1 2 3
CREATE INDEX customer_idx ON customer (UPPER(customer_name)); -- 已创建基于 customer_name 字段的大写评估的索引
为确保 Oracle 优化器在执行 SQL 语句时使用此索引,请确保 UPPER(customer_name) 的计算结果不为 NULL 值。 为确保这一点,请将 UPPER(customer_name) IS NOT NULL 添加到 WHERE 子句中,如下所示:
1 2 3 4
SELECT customer_id, customer_name, UPPER(customer_name) FROM customer WHEREUPPER(customer_name) ISNOTNULL ORDERBYUPPER(customer_name);
重命名索引
重命名索引的语法是:
1 2
ALTER INDEX index_name RENAME TO new_index_name;
例如:
1 2
ALTER INDEX customer_id RENAME TO new_customer_id;
在此示例中,customer_id 重命名为 new_customer_id
收集索引的统计信息
如果您需要在索引首次创建后收集统计信息或者您想要更新统计信息,您总是可以使用 ALTER INDEX 命令来收集统计信息。 您收集统计信息以便 oracle 可以有效地使用索引。 这将重新计算表大小、行数、块数、段数并更新字典表,以便 oracle 在选择执行计划时可以有效地使用数据。
收集索引统计信息的语法是:
1 2
ALTER INDEX index_name REBUILD COMPUTE STATISTICS;
例如:
1 2
ALTER INDEX customer_idx REBUILD COMPUTE STATISTICS;
在此示例中,为名为 customer_idx 的索引收集统计信息
删除索引
删除索引的语法是:
1
DROP INDEX index_name;
例如:
1
DROP INDEX customer_idx;
在此示例中,删除了 customer_idx
DBA 相关
创建用户
创建用户的语法是:
1 2
CREATEUSER username IDENTIFIED BY password;
例如:
1
CREATEUSER brian IDENTIFIED BY brianpass;
授予特权
授予权限的语法是:
1
GRANT privilege TOuser;
例如:
1
GRANT dba TO brian;
更改密码
更改用户密码的语法是:
1
ALTERUSER username IDENTIFIED BY password;
例如:
1
ALTERUSER brian IDENTIFIED BY brianpassword;
查看表空间的名称以及大小
1 2 3 4 5 6
SELECT t.table_name, ROUND(SUM(bytes / (1024*1024)), 0) AS ts_size FROM dba_tablespaces t, dba_data_files d WHERE t.table_name = d.table_name GROUPBY t.table_name;