数据库工程师:SQL语法总结

数据库系统工程师 责任编辑:咖啡不甜 2015-10-17

添加老师微信

备考咨询

加我微信

摘要:SID不对的话会抛出异常:java.sql.SQLException:Io异常:Connectionrefused(DEION=(TMP=)(VSNNUM=153092352)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)

    SQL语法总结

    1.按条件读取字段,定义某个字段只取规定的几个值,或一个值

    selectos.*fromblis_ordero,blis_orderserviceoswhereo.orderid=os.orderidando.ordertypeidnotin(4,8,10,11)ando.statusin(‘New','Accepted','Pending-apPRoval','Pending-effective','Pending-correction’)andsnp.status='Active'andb.entityid=1

    2.去掉重复(互异)的字段distinct

    selectdistinctop.name,op.fromblis_chargeactivationca,blis_orderparameteropwhereop.mastertype='charge'andca.chargeactivationid=op.masteridandca.parentcodelike'%NBRStorageCharge%'

    3.某个字段不可为null

    selectos.orderserviceid,os.orderofferid,o.ordertypeid,o.statusfromBlis_Ordero,Blis_Orderserviceoswhereo.orderid=os.orderidandos.orderofferidisnotnull

    4.删除满足某个条件的记录

    deletefromblis_bstoffermigplanbsfwherebsf.keyid='110206'

    5.取name_a字段,放入字段别名name_b

    selectbsf.keyidsubcode,bsf.bstoffermigplanidfromblis_bstoffermigplanbsf

    这里取的是keyid字段,显示的为subcode字段。

    6.connectionrollbackcommit

    rollback就是把在内存中做的行为取消,不持久化到数据库中,commit就是把内存中做的行为持久化到数据库中。

    7.在Oracle中使用Dual。Dual是Oracle一个特有的虚拟表,Oracle中很多系统的sequence(序列),sequence一般和表建立了一一对应关系,但是要编程插入的话,必须手工指定,比如增加条account数据,相应的值插入SEQ_ACCOUNT.nextval,变量和函数都可以通过Dual中获得

    S:selectgetdate();

    O:selectsysdatefromdual;

    selectSEQ_INTEGRATIONTASK.NEXTVALfromDUAL

    8.(PK)主键(PK)(fordatabase)

    9.排序(数字,字母由大到小)

    selectbsf.*fromblis_bstoffermigplanbsforderbybsf.ordertypeiddesc

    10.插入一条记录

    insertintoblis_bstoffermigplan(bstoffermigplanid,entityid,keyid,subioncode,ordertypeid,type,templatecode,currencycode,exceptioncount,lastexception,att1,att2,att3,att4,att5,offercode,status,createdby,creationdate,lastmodifiedby,lastmodifieddate)values(seq_bstoffermigplan.nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate,?,sysdate)

    11,更新一条记录

    updateoffermigplan.db_table_namesetentityid=?,keyid=?,subioncode=?,ordertypeid=?,type=?,templatecode=?,currencycode=?,exceptioncount=?,lastexception=?,att1=?,att2=?,att3=?,att4=?,att5=?,offercode=?,status=?,createdby=?,lastmodifiedby=?,lastmodifieddate=sysdatewherebstoffermigplanid=?

    12.插入数据量比较大的类型clob

    13.查询日期

    下列两条语句功能相同

    select*fromblis_accountaccwhereto_char(acc.lastmodifieddate,'YYYYMMDD‘)>'20050101'

    select*fromblis_accountaccwhereacc.lastmodifieddate>to_date('2005-01-01','yyyy-mm-dd’)

    14.找出根据某个字段中的值重复的记录

    比如找出chargeactivationid有相同值的blis_usageaccess记录

    select*fromblis_usageaccesswherechargeactivationidin(selectchargeactivationidfromblis_usageaccessgroupbychargeactivationidhavingcount(*)>1)

    USAGEACCESSIDCHARGEACTIVATIONIDSERVICEACCESSCODE

    292518148701AUDIO-BROADCAST@

    292517148701VOip@

    292516148701CALLIN-DID@

    292515148701CALLBACK-INTL@

    292512148701CALLIN@

    292513148701CALLIN-TOLLFREE@

    292514148701CALLBACK@

    292478147945AUDIO-BROADCAST@

    292477147945VOIP@

    292475147945CALLBACK-INTL@

    292476147945CALLIN-DID@

    292472147945CALLIN@

    15.通过查询获得某个字段的合计值,如果这个值位null将给出一个预设的默认值

    selectnvl(ob.bookingvalue,0)bookingvaluefromblis_ordero,blis_orderbookingobwhereo.orderid=ob.orderidando.orderid=125034andob.bookingtypeid=215andob.status='Active'

    这里关心nvl的用法,nvl(arg,value)代表如果前面的arg的值为null那么返回的值为后面的value

    16.知道一个column的名字,但不清楚它属于哪张table时,可以使用

    select*fromuser_col_commentsuccwhereucc.column_name='column_name'

    比如:select*fromuser_col_commentsuccwhereucc.column_name='ORDERID'就会查出一系列有ORDERID字段的表。

    17.遍历两字段排列

    select(pf.offername||''||cur.name)offercodefromblis_packageofferpf,blis_currencycurwherecur.status='Active'andpf.status='Active'

    结果如下:

    offercode

    a1b1

    a1b2

    a2b1

    a2b2

    18.条件判断

    casewhenpc.provisioningby='BPS'then'True'

    else'False'end

    selectsos.Sosorderserviceid,st.sosprovisionticketid,

    (casewhenpc.provisioningby='BPS'then'True'

    else'False'end)isConnector

    fromblis_sosprovisionticketst,blis_sosorderformsof,

    blis_sosorderservicesos,blis_packagecomponentpc

    wheresof.sosorderformid=sos.sosorderformid

    andsos.sosorderserviceid=st.sosorderserviceid

    andsos.status='Active'andst.status='Active'

    andpc.tagname(+)=st.servicetagandpc.provisioningby

    andsof.sosorderformid=104789

    19.pc.tagname(+)=st.servicetag

    当pc.tagname存在值,st.servicetag不存在值的话,记录也可以检索出来。

    20.让表可以手工编辑

    selectrowid,st.*fromblis_sosprovisionticketstwherest.sosprovisionticketid=102508

    用classes12.zip还是会抛出classNotFoundException:oracle.jdbc.driver.OracleDriver,换用class12.jar就正常了,classes12.zip或class12.jar是JDBCoracle驱动类

    创建数据库:

    查看所有表:select*fromdba_all_tables

    查看所有用户:select*fromall_users

    查看所有DBA用户:select*fromdba_users

    创建role:createroleBLIS_ADMIN_ROLE;

    创建新用户:createuserusernameidentifiedbypassWord

    授予表空间使用权:grantresourcetousername

    授予创建表权限:grantcreatetabletousername

    授予连接数据库的权限:grantcreatesessiontousername

    查看所有表空间:select*fromdba_tablespaces

    把任何表授予某用户:grantcreateanytabletoBLIS_ADMIN_ROLE;

    授予某用户检索功能:grantcreateanyindextoBLIS_ADMIN_ROLE;

    授予某用户对某表有检索,插入,更新,删除功能:grantselect,insert,update,deleteonBLIS_ACCAGENCYCOMMISSIONtoBLIS_ADMIN_ROLE;

    导出数据库:比如:expblis/blis@dblsfull=yfile=d:1.dmp

    连接ORACLE数据库的字符串格式是

    jdbcracle:thin主机:端口:SID

    注意是SID而不是数据库名

    SID不对的话会抛出异常:java.sql.SQLException:Io异常:Connectionrefused(DEION=(TMP=)(VSNNUM=153092352)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)

    更多资料请登录希赛软考网


相关推荐:

      数据库系统工程师该怎么发展?

      怎样获得数据库系统工程师认证?

      数据库系统工程师的基本要求与发展


更多资料
更多课程
更多真题
温馨提示:因考试政策、内容不断变化与调整,本网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准!

软考备考资料免费领取

去领取

!
咨询在线老师!