SQnote
永远有多远……
SQnote - java,web优化,python,(www.sqnote.cn)

SELECT * FROM Customers
DELETE FROM Customers WHERE PKId=1
UPDATE Customers SET UserID='sq' WHERE PKId=1
INSERT INTO Customers(UserID,TrueName) VALUES ('shiqiang','宝剑')

 

在oracle8.05中设置访问数据库别名:
WSTXNY.WORLD =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.252)(PORT = 1521))
    (CONNECT_DATA = (SID = WSTXNY))
  )


表4.1 比较运算符

名称 实例
=(等于) select * from scott.emp where job=’MANAGER’;
select * from scott.emp where sal=1100;
!= (不等于) select * from scott.emp where job!=’MANAGER’;
select * from scott.emp where sal!=1100;
^=(不等于) select * from scott.emp where job^=’MANAGER’;
select * from scott.emp where sal^=1100;
<>(不等于) select * from scott.emp where job<>’MANAGER’;
select * from scott.emp where sal<>1100;
<(小于) select * from scott.emp where sal<2000;
select * from scott.emp where job<’MANAGER’;
>(大于) select * from scott.emp where sal>2000;
select * from scott.emp where job>’MANAGER’;
<=(小于等于) select * from scott.emp where sal<=2000;
select * from scott.emp where job<=’MANAGER’;
>=(大于等于) select * from scott.emp where sal>=2000;
select * from scott.emp where job>=’MANAGER’;
in(列表) select * from scott.emp where sal in (2000,1000,3000);
select * from scott.emp where job in (’MANAGER’,’CLERK’);
not in(不在列表) select * from scott.emp where sal not in (2000,1000,3000);
select * from scott.emp where job not in (’MANAGER’,’CLERK’);
between(介于之间) select * from scott.emp where sal between 2000 and 3000;
select * from scott.emp where job between ’MANAGER’ and ’CLERK’;
not between (不介于之间) select * from scott.emp where sal not between 2000 and 3000;
select * from scott.emp where job not between ’MANAGER’ and ’CLERK’;
like(模式匹配) select * from scott.emp where job like ’M%’;
select * from scott.emp where job like ’M__’;
not like (模式不匹配) select * from scott.emp where job not like ’M%’;
select * from scott.emp where job not like ’M__’;
Is null (是否为空) select * from scott.emp where sal is null;
select * from scott.emp where job is null;
is not null(是否为空) select * from scott.emp where sal is not null;
select * from scott.emp where job is not null;

SQLServer和Oracle常用函数对比
数学函数 
  1.绝对值 
  S:select abs(-1) value 
  O:select abs(-1) value from dual 

  2.取整(大) 
  S:select ceiling(-1.001) value 
  O:select ceil(-1.001) value from dual 

  3.取整(小) 
  S:select floor(-1.001) value 
  O:select floor(-1.001) value from dual 

  4.取整(截取) 
  S:select cast(-1.002 as int) value 
  O:select trunc(-1.002) value from dual 

  5.四舍五入 
  S:select round(1.23456,4) value 1.23460 
  O:select round(1.23456,4) value from dual 1.2346 

  6.e为底的幂 
  S:select Exp(1) value 2.7182818284590451 
  O:select Exp(1) value from dual 2.71828182 

  7.取e为底的对数 
  S:select log(2.7182818284590451) value 1 
  O:select ln(2.7182818284590451) value from dual; 1 

  8.取10为底对数 
  S:select log10(10) value 1 
  O:select log(10,10) value from dual; 1 

  9.取平方 
  S:select SQUARE(4) value 16 
  O:select power(4,2) value from dual 16 

  10.取平方根 
  S:select SQRT(4) value 2 
  O:select SQRT(4) value from dual 2 

  11.求任意数为底的幂 
  S:select power(3,4) value 81 
  O:select power(3,4) value from dual 81 

  12.取随机数 
  S:select rand() value 
  O:select sys.dbms_random.value(0,1) value from dual; 

  13.取符号 
  S:select sign(-8) value -1 
  O:select sign(-8) value from dual -1 

  14.圆周率 
  S:SELECT PI() value 3.1415926535897931 
  O:不知道 

  15.sin,cos,tan 参数都以弧度为单位 
  例如:select sin(PI()/2) value 得到1(SQLServer) 

  16.Asin,Acos,Atan,Atan2 返回弧度 

  17.弧度角度互换(SQLServer,Oracle不知道) 
  DEGREES:弧度-〉角度 
  RADIANS:角度-〉弧度 

数值间比较 

  18. 求集合最大值 
  S:select max(value) value from 
  (select 1 value 
  union 
  select -2 value 
  union 
  select 4 value 
  union 
  select 3 value)a 

  O:select greatest(1,-2,4,3) value from dual 

  19. 求集合最小值 
  S:select min(value) value from 
  (select 1 value 
  union 
  select -2 value 
  union 
  select 4 value 
  union 
  select 3 value)a 

  O:select least(1,-2,4,3) value from dual 

  20.如何处理null值(F2中的null以10代替) 
  S:select F1,IsNull(F2,10) value from Tbl 
  O:select F1,nvl(F2,10) value from Tbl 

  21.求字符序号 
  S:select ascii('a') value 
  O:select ascii('a') value from dual 

  22.从序号求字符 
  S:select char(97) value 
  O:select chr(97) value from dual 

  23.连接 
  S:select '11'+'22'+'33' value 
  O:select CONCAT('11','22')  33 value from dual 

23.子串位置 --返回3 
  S:select CHARINDEX('s','sdsq',2) value 
  O:select INSTR('sdsq','s',2) value from dual 

  23.模糊子串的位置 --返回2,参数去掉中间%则返回7 
  S:select patindex('%d%q%','sdsfasdqe') value 
  O:oracle没发现,但是instr可以通过第四个参数控制出现次数 
  select INSTR('sdsfasdqe','sd',1,2) value from dual 返回6 

  24.求子串 
  S:select substring('abcd',2,2) value 
  O:select substr('abcd',2,2) value from dual 

  25.子串代替 返回aijklmnef 
  S:SELECT STUFF('abcdef', 2, 3, 'ijklmn') value 
  O:SELECT Replace('abcdef', 'bcd', 'ijklmn') value from dual 

  26.子串全部替换 
  S:没发现 
  O:select Translate('fasdbfasegas','fa','我' ) value from dual 

  27.长度 
  S:len,datalength 
  O:length 

  28.大小写转换 lower,upper 

  29.单词首字母大写 
  S:没发现 
  O:select INITCAP('abcd dsaf df') value from dual 

  30.左补空格(LPAD的第一个参数为空格则同space函数) 
  S:select space(10)+'abcd' value 
  O:select LPAD('abcd',14) value from dual 

  31.右补空格(RPAD的第一个参数为空格则同space函数) 
  S:select 'abcd'+space(10) value 
  O:select RPAD('abcd',14) value from dual 

  32.删除空格 
  S:ltrim,rtrim 
  O:ltrim,rtrim,trim 

  33. 重复字符串 
  S:select REPLICATE('abcd',2) value 
  O:没发现 

  34.发音相似性比较(这两个单词返回值一样,发音相同) 
  S:SELECT SOUNDEX ('Smith'), SOUNDEX ('Smythe') 
  O:SELECT SOUNDEX ('Smith'), SOUNDEX ('Smythe') from dual 
  SQLServer中用SELECT DIFFERENCE('Smithers', 'Smythers') 比较soundex的差 
  返回0-4,4为同音,1最高 

日期函数 
  35.系统时间 
  S:select getdate() value 
  O:select sysdate value from dual 

  36.前后几日 
  直接与整数相加减 

  37.求日期 
  S:select convert(char(10),getdate(),20) value 
  O:select trunc(sysdate) value from dual 
  select to_char(sysdate,'yyyy-mm-dd') value from dual 

  38.求时间 
  S:select convert(char(8),getdate(),108) value 
  O:select to_char(sysdate,'hh24:mm:ss') value from dual 

39.取日期时间的其他部分 
  S:DATEPART 和 DATENAME 函数 (第一个参数决定) 
  O:to_char函数 第二个参数决定 

  参数---------------------------------下表需要补充 
  year yy, yyyy 
  quarter qq, q (季度) 
  month mm, m (m O无效) 
  dayofyear dy, y (O表星期) 
  day dd, d (d O无效) 
  week wk, ww (wk O无效) 
  weekday dw (O不清楚) 
  Hour hh,hh12,hh24 (hh12,hh24 S无效) 
  minute mi, n (n O无效) 
  second ss, s (s O无效) 
  millisecond ms (O无效) 
  ---------------------------------------------- 

  40.当月最后一天 
  S:不知道 
  O:select LAST_DAY(sysdate) value from dual 

  41.本星期的某一天(比如星期日) 
  S:不知道 
  O:SELECT Next_day(sysdate,7) vaule FROM DUAL; 

  42.字符串转时间 
  S:可以直接转或者select cast('2004-09-08'as datetime) value 
  O:SELECT To_date('2004-01-05 22:09:38','yyyy-mm-dd hh24-mi-ss') vaule FROM DUAL; 

  43.求两日期某一部分的差(比如秒) 
  S:select datediff(ss,getdate(),getdate()+12.3) value 
  O:直接用两个日期相减(比如d1-d2=12.3) 
  SELECT (d1-d2)*24*60*60 vaule FROM DUAL; 

  44.根据差值求新的日期(比如分钟) 
  S:select dateadd(mi,8,getdate()) value 
  O:SELECT sysdate+8/60/24 vaule FROM DUAL; 

  45.求不同时区时间 
  S:不知道 
  O:SELECT New_time(sysdate,'ydt','gmt' ) vaule FROM DUAL; 

  -----时区参数,北京在东8区应该是Ydt------- 
  AST ADT 大西洋标准时间 
  BST BDT 白令海标准时间 
  CST CDT 中部标准时间 
  EST EDT 东部标准时间 
  GMT 格林尼治标准时间 
  HST HDT 阿拉斯加?夏威夷标准时间 
  MST MDT 山区标准时间 
  NST 纽芬兰标准时间 
  PST PDT 太平洋标准时间 
  YST YDT YUKON标准时间
 

学习sql有一段时间了,发现在我建了一个用来测试的表(没有建索引)中出现了许多的重复记录。后来总结了一些删除重复记录的方法,在Oracle中,可以通过唯一rowid实现删除重复记录;还可以建临时表来实现...这个只提到其中的几种简单实用的方法,希望可以和大家分享(以表employee为例)。

SQL> desc employee

 Name                                      Null?    Type
 ----------------------------------------- -------- ------------------

emp_id                                                NUMBER(10)
emp_name                                           VARCHAR2(20)

salary                                                  NUMBER(10,2)

 

 

可以通过下面的语句查询重复的记录:

SQL> select * from employee;

 

    EMP_ID EMP_NAME                                  SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         1 sunshine                                      10000

         2 semon                                         20000

         2 semon                                         20000

         3 xyz                                           30000

         2 semon                                         20000

 


SQL>
select distinct * from employee;

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         2 semon                                         20000

         3 xyz                                             30000

SQL>  select * from employee group by emp_id,emp_name,salary having count (*)>1

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         2 semon                                          20000


SQL>
select * from employee e1

where rowid in (select max(rowid) from employe e2
 
where e1.emp_id=e2.emp_id and

  e1.emp_name=e2.emp_name and e1.salary=e2.salary);

 

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         3 xyz                                             30000

         2 semon                                         20000

 

 

2. 删除的几种方法:

 

1)通过建立临时表来实现

SQL>create table temp_emp as (select distinct * from employee) 

SQL> truncate table employee; (清空employee表的数据)

SQL> insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

 

( 2)通过唯一rowid实现删除重复记录.Oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中那些具有最大或最小rowid的就可以了,其余全部删除。

SQL>delete from employee e2 where rowid not in (
       
select max(e1.rowid) from employee e1 where

        e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--这里用min(rowid)也可以。

 

SQL>delete from employee e2 where rowid <(
       
select max(e1.rowid) from employee e1 where
        e1.emp_id
=e2.emp_id and e1.emp_name=e2.emp_name and

                  e1.salary=e2.salary);

 

3)也是通过rowid,但效率更高。

SQL>delete from employee where rowid not in (
       
select max(t1.rowid) from employee t1 group by

         t1.emp_id,t1.emp_name,t1.salary);--这里用min(rowid)也可以。

 

 

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         3 xyz                                             30000

         2 semon                                         20000

 

 

 

 

SQL> desc employee

 Name                                      Null?    Type
 ----------------------------------------- -------- ------------------

emp_id                                                NUMBER(10)
emp_name                                           VARCHAR2(20)

salary                                                  NUMBER(10,2)

 

 

可以通过下面的语句查询重复的记录:

SQL> select * from employee;

 

    EMP_ID EMP_NAME                                  SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         1 sunshine                                      10000

         2 semon                                         20000

         2 semon                                         20000

         3 xyz                                           30000

         2 semon                                         20000

 


SQL>
select distinct * from employee;

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         2 semon                                         20000

         3 xyz                                             30000

SQL>  select * from employee group by emp_id,emp_name,salary having count (*)>1

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         2 semon                                          20000


SQL>
select * from employee e1

where rowid in (select max(rowid) from employe e2
 
where e1.emp_id=e2.emp_id and

  e1.emp_name=e2.emp_name and e1.salary=e2.salary);

 

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         3 xyz                                             30000

         2 semon                                         20000

 

 

2. 删除的几种方法:

 

1)通过建立临时表来实现

SQL>create table temp_emp as (select distinct * from employee) 

SQL> truncate table employee; (清空employee表的数据)

SQL> insert into employee select * from temp_emp;  (再将临时表里的内容插回来)

 

( 2)通过唯一rowid实现删除重复记录.Oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中那些具有最大或最小rowid的就可以了,其余全部删除。

SQL>delete from employee e2 where rowid not in (
       
select max(e1.rowid) from employee e1 where

        e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--这里用min(rowid)也可以。

 

SQL>delete from employee e2 where rowid <(
       
select max(e1.rowid) from employee e1 where
        e1.emp_id
=e2.emp_id and e1.emp_name=e2.emp_name and

                  e1.salary=e2.salary);

 

3)也是通过rowid,但效率更高。

SQL>delete from employee where rowid not in (
       
select max(t1.rowid) from employee t1 group by

         t1.emp_id,t1.emp_name,t1.salary);--这里用min(rowid)也可以。

 

 

    EMP_ID EMP_NAME                                     SALARY

---------- ---------------------------------------- ----------

         1 sunshine                                      10000

         3 xyz                                             30000

         2 semon                                         20000



在INFORMIX里执行后出现: 
Cannot modify table or view used in subquery.
:(
CSDN 网友 ( 2005-10-10)
sql server中你可以加一个自增长的字段
rmuyu ( 2005-09-16)
很好,这两天我正在为这个问题烦恼,谢谢楼主
sqlsea ( 2005-09-16)
sqlserver里这样:
http://blog.itpub.net/post/1626/8887
lllllllllluoyi ( 2005-09-15)
我的做法:
一个表abc结构:
  aa   bb   cc  //字段
-------------------- //值
 aa    bb   cc1
 aa    bb   cc2
 aa    bb   cc3
....................
从表结构可以看出有CC字段的数据是不同的,这个很重要。
在oracle中:
delete from abc where cc in (select cc from abc where aa = 'aa' minus (select cc from abc where aa = 'aa' and rownum <= 1)) and aa = 'aa';
在sql server中:
delete from abc where cc in (select cc from abc where aa = 'aa' and cc not in(select top 1 cc from abc where aa = 'aa')) and aa = 'aa';
这种情况是删除字段aa相同而其他某个字段不同的方法,如果是每个字段都相同多条记录的删除方法又该是怎么呢?
oracle做法:
delete from abc t
where rowid not in (select max(rowid) from abc t1 where t.aa = t1.aa and t.bb = t1.bb and t.cc 

= t1.cc)
主要运用了rowid关键字



posted on 2005-11-01 11:11  SQnote·www.SQnote.cn  阅读(394)  评论(0)    收藏  举报