oracle sql使用技巧汇总
1、删除一个表的重复数据 所用的table_name一样
DELETE
FROM table_name a
WHERE rowid > ( SELECT min(rowid)
FROM table_name b
WHERE b.id = a.id and b.name=a.name);
2、导出一个用户的所有信息
exp scott/tiger@orcl file=d:/scott.dmp
3、导入一个用户的所有信息 imp scott/tiger@orcl file=d:/scott.dmp full=y; --or
imp scott/tiger@orcl file=d:/scott.dmp fromuser=scott touser=scott
4、导出一个用户的某几张表,而不是全部
exp scott/tiger@orcl file=d:/scott.dmp tables=(emp,dept)
5、导入一个用户的某几张表,而不是全部
imp scott/tiger@orcl file=d:/scott.dmp tables=(emp,dept)
6、为某个用户赋予dba权限
grant dba to scott;
7、收回某个用户的权限
revoke dba from scott;
8、删除表空间
alter tablespace_name jxfda offline;
drop tablespace tablespace_name including contents and datafiles;
9、删除用户(级联)
drop user user_name cascade;
10、获取某个子字符串
获取t.ename='fhg1',get 'hg'
select substr(t.ename,1,2) from emp t;
11、数据批量更新或插入(merge)
MERGE INTO table_name p -- 待修改表
USING table_other o --所依赖的表(如果是多个表,建立视图View 如(select ...)别名)
ON (p.product_id = np.product_id)
WHEN MATCHED THEN
UPDATE
SET p.product_name = np.product_name, p.category = np.category where ...;
WHEN NOT MATCHED THEN
insert values(...) where ....
12、获取字符串的长度 length()字符数,lengthb()字节数
lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节
length(string)计算string所占的字符长度:返回字符串的长度,单位是字符
对于单字节字符,LENGTHB和LENGTH是一样的.
如可以用length(‘string你好’)=lengthb(‘string你好’)判断字符串是否含有中文。
注:
一个汉字在Oracle数据库里占多少字节跟数据库的字符集有关,UTF8时,长度为三,GBK为2个字节。
select lengthb('哈哈') from dual ;可查询汉字在Oracle数据库里占多少字节