1、oracle建立索引
例如:一个表 student ,系统会经常通过 表中三个字段(name,sex,grade)进行搜索,即select * from student where name='xx' and sex='xx' and grade='xx' . 所以要给这三个字段都建立索引,请问怎么建立索引? 有什么要注意的地方么?
对三个字段建立索引:
create index Stuname on student(name);
create index Stusex on student(sex);
create index Stugrade on student(grade);
注意的问题,考虑是不是要建立唯一索引(unique),如果有学号的话,可以考虑建立唯一索引引。
再就是对经常查询,但又相对稳定的可以建立聚簇索引,提高查询效率
2. Oracle 中 decode 函数用法
含义解释:
decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
该函数的含义如下:
IF 条件=值1 THEN
RETURN(翻译值1)
ELSIF 条件=值2 THEN
RETURN(翻译值2)
......
ELSIF 条件=值n THEN
RETURN(翻译值n)
ELSE
RETURN(缺省值)
END IF
decode(字段或字段的运算,值1,值2,值3)
这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回值3
当然值1,值2,值3也可以是表达式,这个函数使得某些sql语句简单了许多
使用方法:
1、比较大小
select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值
sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1
例如:
变量1=10,变量2=20
则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。
2、此函数用在SQL语句中,功能介绍如下:
Decode函数与一系列嵌套的 IF-THEN-ELSE语句相似。base_exp与compare1,compare2等等依次进行比较。如果base_exp和 第i 个compare项匹配,就返回第i 个对应的value 。如果base_exp与任何的compare值都不匹配,则返回default。每个compare值顺次求值,如果发现一个匹配,则剩下的compare值(如果还有的话)就都不再求值。一个为NULL的base_exp被认为和NULL compare值等价。如果需要的话,每一个compare值都被转换成和第一个compare 值相同的数据类型,这个数据类型也是返回值的类型。
Decode函数在实际开发中非常的有用
结合Lpad函数,如何使主键的值自动加1并在前面补0
select LPAD(decode(count(记录编号),0,1,max(to_number(记录编号)+1)),14,'0') 记录编号 from tetdmis
eg:
select decode(dir,1,0,1) from a1_interval
dir 的值是1变为0,是0则变为1
比如我要查询某班男生和女生的数量分别是多少?
通常我们这么写:
select count(*) from 表 where 性别 = 男;
select count(*) from 表 where 性别 = 女;
要想显示到一起还要union一下,太麻烦了
用decode呢,只需要一句话
select decode(性别,男,1,0),decode(性别,女,1,0) from 表
3,order by对字符列进行特定的排序
大家还可以在Order by中使用Decode。
例:表table_subject,有subject_name列。要求按照:语、数、外的顺序进行排序。这时,就可以非常轻松的使用Decode完成要求了。
select * from table_subject order by decode(subject_name, '语文', 1, '数学', 2, , '外语',3)
常用语句:
ALTER TABLE TABLENAME RENAME COLUMN OLDFIELDNAME TO NEWFIELDNAME
RENAME OLDTABLENAME TO NEWTABLENAME
ALTER TABLE TABLENAME DROP COLUMN FIELDNAME
ALTER TABLE TABLENAME ADD PRIMARY KEY CUST_ID TAG CUST_ID ALTER TABLE TABLENAME ALTER COLUMN CUST_ID C(5) PRIMARY KEY
---------------------------------------------------------------------------------------------------------
给某人授权一张表中的某个字段
grant obj_privilege(column_list) on object_name to user_name;
GRANT INSERT(ID) ON test TO xiaoliu;
GRANT UPDATE(ID) ON test TO xiaoliu;
--授予权限
grant insert,select,update,delete on table to user;
grant dba,resource to ytpbl;
grant create any sequence to ytpbl;
--撤销
revoke
--Oracle查询用户,表相关信息...
--用户
SELECT * FROM DBA_USERS;
SELECT * FROM ALL_USERS;
SELECT * FROM USER_USERS;
--系统权限
SELECT * FROM DBA_SYS_PRIVS;
SELECT * FROM SESSION_PRIVS;
SELECT * FROM USER_SYS_PRIVS;
--角色
SELECT * FROM USER_ROLE_PRIVS;
SELECT * FROM DBA_ROLES;
--对象权限
select * from user_tab_privs where table_name like '%T_MDM_SMS_MESSAGE_TYPE%';
select * from dba_tab_privs;
select * from all_tab_privs;
--表查询
select * from user_tab_comments;
select * from user_col_comments
--查询索引
select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = '要查询的表';
如何在oracle中查询所有用户表的表名、主键名称、索引、外键等
1、查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查询的表
2、查找表的主键(包括名称,构成列): select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = 要查询的表
3、查找表的唯一性约束(包括名称,构成列): select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查询的表
4、查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询): select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表 查询外键约束的列名: select * from user_cons_columns cl where cl.constraint_name = 外键名称 查询引用表的键的列名: select * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名
5、查询表的所有列及其属性 select t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c.table_name and t.column_name = c.column_name and t.table_name = 要查询的表
---------------------------------------------------------------------------------------------------------------
1.当前用户下是否有某个表
select count(*) from user_tables where table_name = 'TABLE_NAME';
注意,一般情况下,引号里面的table_name要全部用大写。
如果结果为1,表示有这个表,为0表示没有这个表。
2.某个用户下是否有某个表?
select count(*) from dba_tables where owner = 'USER_NAME' and table_name = 'TABLE_NAME';
3.多行合并一行
SELECT WMSYS.WM_CONCAT(FIELD_NAME) FROM TABLE_NAME