生活随笔
收集整理的這篇文章主要介紹了
笔记(数据库)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2008-10-11?Saturday1.?導(dǎo)入數(shù)據(jù)時(shí)間分析用amsConn->ExecSql(str);方式?插入5543行記錄用時(shí)13.391秒用???CRecords?cRecord(*amsConn,?1);????????cRecord.Query(str);方式,插入5543行記錄用時(shí)26.61秒用表類的Insert命令時(shí)用有線時(shí)OK55434.953用時(shí)4.953秒體現(xiàn)出表類操作的優(yōu)勢(shì),一次提交,節(jié)省時(shí)間2.?ORACLE基礎(chǔ)在命令行中操作C:/Documents?and?Settings/sun>sqlplus/nologSQL>?conn?/as?sysdba已連接。SQL>?startupORACLE?例程已經(jīng)啟動(dòng)。SQL>?select?*?from?student;SNO??????????????SNAME???????????????????????????????????????????AGE----------------?----------------------------------------?----------001??????????????Mary?????????????????????????????????????????????19?????????????????Tom??????????????????????????????????????????????21SQL>?shutdown數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉。已經(jīng)卸載數(shù)據(jù)庫(kù)。ORACLE?例程已經(jīng)關(guān)閉。3.?sqlloader使用創(chuàng)建用戶創(chuàng)建表空間查找的資料1、在Oracle中按照導(dǎo)入數(shù)據(jù)的格式建立一個(gè)空表2、編寫一個(gè)loader.ctl文件,內(nèi)容如下load?data??infile?'/backup/incoming/SDSS.csv'?into?table?SDSS??????fields?terminated?by?","(?RA,?DEC,?MAG?)????操作分類:?????????a、insert,為缺省方式,在數(shù)據(jù)裝載開始時(shí)要求表為空????????b、append,在表中追加新記錄????????c、replace,刪除舊記錄,替換成新裝載的記錄????????d、truncate,同上3、在CMD中執(zhí)行以下語句:????????sqlldr?sss/oracle?control=loader.ctl?4、自動(dòng)在當(dāng)前目錄下生成兩個(gè)文件????????默認(rèn)日志文件名為:loader.log?????????默認(rèn)壞記錄文件為:SDSS.bad?注意事項(xiàng):1、必須在服務(wù)器本機(jī)上操作,不能在只安裝Oracle客戶端的機(jī)器上使用。2、CSV文件的內(nèi)容是以“,“分隔的,如果數(shù)據(jù)在結(jié)尾沒有逗號(hào)導(dǎo)入時(shí)就會(huì)出錯(cuò)。可以利用UltraEdit等文本編輯器處理一下,將?^p?替換為?,^p?就可以在每行的末尾加上一個(gè)逗號(hào)?了。3、數(shù)據(jù)導(dǎo)入需要較長(zhǎng)的時(shí)間。4、導(dǎo)入不同的表需要使用不同的.ctl文件==========================?????實(shí)例D盤中input.ctlload?datainfile?'data.txt'append?into?table?STUDENTfields?terminated?by?','(SNO,?SNAME)===========================?????D盤中data.txt041,Jack,042,,===========================????命令行命令C:/Documents?and?Settings/sun>d:D:/>sqlldr?ams1@hadev/ams1?control=input.ctlSQL*Loader:?Release?10.2.0.1.0?-?Production?on?星期六?10月?11?21:41:05?2008Copyright?(c)?1982,?2005,?Oracle.??All?rights?reserved.達(dá)到提交點(diǎn)?-?邏輯記錄計(jì)數(shù)?1達(dá)到提交點(diǎn)?-?邏輯記錄計(jì)數(shù)?2D:/>=============================?????導(dǎo)入時(shí)可以設(shè)置格式,日期型數(shù)據(jù)則必須要進(jìn)行設(shè)置http:load?data??????????????????????????????????????????infile?'test.txt'??????????????????????????????????append?into?table?CC???????????????????????????????fields?terminated?by?','???????????????????????????(???????ACCT_BALANCE_ID,????????????????????????????ACCT_ID,???????????????????????????????????EFF_DATE?Date?"yyyymmdd",??????????????????EXP_DATE?Date?"yyyymmdd",??????????????????OPT_DATE?Date?"yyyymmddhh24miss",??????????PAYMENT_ID??????????????????????)??????????????????????這種方式進(jìn)行數(shù)據(jù)導(dǎo)入效率極高,立刻完成經(jīng)過時(shí)間為:?00:?00:?00.81CPU?時(shí)間為:?00:?00:?00.23???????????????????????????2008-10-12?Sunday1.?sqlloader的篩選技巧Load?datainfile?'data.txt'Append?into?table?studentfields?terminated?by?','(????SNO,????x?filler,????SNAME)使用filler進(jìn)行過濾2.?outlook設(shè)置可以接收郵件,但是不能發(fā)送修改設(shè)置?工具----帳戶-----屬性----高級(jí)-----服務(wù)器延時(shí)???時(shí)間拉長(zhǎng)就可以了10月?第3周2008-10-13?Monday1.?sscanf16進(jìn)制轉(zhuǎn)換為10進(jìn)制int?main()?{?????const?char*?a="0xff";?????int?i;?????sscanf(a,?"%x",?&i);?????printf("%d",?i);?????return?0;?}?2.?SQL語句批量執(zhí)行http:如批量的更新或插入放在一個(gè)文件中,如:c:/test.sql?則在pl/sql或sqlplus下僅需執(zhí)行:@c:/test.sqlSQL>?@d:/see.sql或者SQL>?start?d:/see.sql附:sqlplus?的使用C:/Documents?and?Settings/sun>sqlplus/nologSQL*Plus:?Release?10.2.0.1.0?-?Production?on?星期一?10月?13?19:57:22?2008Copyright?(c)?1982,?2005,?Oracle.??All?rights?reserved.SQL>?conn請(qǐng)輸入用戶名:??ams1@hadev輸入口令:ams1??注意:這里的輸入不會(huì)顯示出來已連接。3.?SQL存儲(chǔ)過程C:/Documents?and?Settings/sun>sqlplusSQL*Plus:?Release?10.2.0.1.0?-?Production?on?星期五?10月?24?14:49:57?2008Copyright?(c)?1982,?2005,?Oracle.??All?rights?reserved.請(qǐng)輸入用戶名:??sun輸入口令:連接到:Oracle?Database?10g?Express?Edition?Release?10.2.0.1.0?-?ProductionSQL>??create?PROCEDURE?in_stu(no?char,?name?char)??2???IS??3???BEGIN??4???INSERT?INTO?STUDENT?VALUES(no,?name);??5???COMMIT;??6???END;??7???/過程已創(chuàng)建。SQL>?execute?in_stu('099','Monday')PL/SQL?過程已成功完成。SQL>SQL>?DROP?PROCEDURE?in_stu;過程已刪除。4.?PL/SQL導(dǎo)入工具很好的一個(gè)工具,使用方便,當(dāng)然是會(huì)用了之后,可以任意實(shí)現(xiàn)列的對(duì)應(yīng)Tools-----??Text?Importer先導(dǎo)入數(shù)據(jù),預(yù)覽,再做設(shè)置,有疑問點(diǎn)擊Help,講的很清楚2008-10-14?Tuesday1.?批量執(zhí)行導(dǎo)入數(shù)據(jù)寫一個(gè)程序,用sprintf把數(shù)據(jù)寫入到sql文件中,再在sqlplus中執(zhí)行2.?SQL語句http:例如?表a?字段??r0??r1??r2???????20??-30??50????如何取該行的最大值?SELECT????(????????SELECT????????????MAX(col)?????????FROM(????????????SELECT?col?=?r0?UNION?ALL????????????SELECT?col?=?r1?UNION?ALL????????????SELECT?col?=?r2????????)A????)FROM?表a2008-10-15?Wednesday1.?sprintf #include?<iostream.h> int?main(){????char?a[10]?=?"12345";????char?buf[10];????sprintf(buf,?"_%s_",?a?+?1);????cout?<<?buf?<<?endl;????return?0;}結(jié)果_2345_2.?PLSQL中的命令行File-??New???Command?Window就可以執(zhí)行命令行語句了3.?備份表的程序編寫仿照上次寫的程序,基本上沒有出現(xiàn)什么問題問題:函數(shù)如何組織起來,分寫多個(gè)函數(shù),還是寫一個(gè)函數(shù),傳入?yún)?shù)實(shí)現(xiàn)多功能在ams1的用戶中,可以通過ams2.tableName的方式去訪問ams2帳戶的表2008-10-16?Thursday1.?關(guān)閉系統(tǒng)自動(dòng)更新第一,“我的電腦”點(diǎn)右鍵,選“屬性”,“自動(dòng)更新”把有關(guān)自動(dòng)更新的選項(xiàng)去掉。?第二,“我的電腦”點(diǎn)右鍵,選“管理”,點(diǎn)左邊“服務(wù)和應(yīng)用程序”旁的加號(hào),展開,點(diǎn)“服務(wù)”。?或者直接在“運(yùn)行”中輸入“Services.msc”打開服務(wù)設(shè)置窗口。?在“服務(wù)”列表中,找到“AutomaticUpdates”這一項(xiàng),雙擊,彈出的屬性窗口中,“啟動(dòng)類型”設(shè)置為“已禁用”,確定即可。?“AutomaticUpdates”這個(gè)進(jìn)程的作用是:自動(dòng)從WindowsUpdate網(wǎng)絡(luò)更新補(bǔ)丁。利用WindowsUpdate功能進(jìn)行升級(jí)。2.?字符串連接update?tem_a?set?table_Name?=?table_name||'_G'||?用于實(shí)現(xiàn)字符串的連接3.?刪除數(shù)據(jù)今天在csdn上看到,才知道這個(gè)問題使用:truncate?table?表名;?它可以釋放占用的數(shù)據(jù)塊表空間,但此操作不可回退。?使用delete刪除記錄不能釋放Oracle里被占用的數(shù)據(jù)塊表空間,它只把那些被刪除的數(shù)據(jù)塊標(biāo)成unused。4.?注意COMMIT在數(shù)據(jù)庫(kù)的操作中,如用for?update?之后,要及時(shí)的commit,如果出現(xiàn)問題,可以用rollback來取消修改;2008-10-17?Friday1.?判斷表是否為空select?count(*)?from?res.SERVE_REPAIR_INFO?where?rownum?=?1不用全表掃描,只要找出1行記錄就可以,如果僅僅是要看有無記錄2.?控制臺(tái)實(shí)現(xiàn)輸入密碼不顯示來自csdn #include?<iostream>? using?namespace?std;? #include?<string>? #include?<conio.h>? int?main()?{?string?password;?char?a;?while((a=_getch())?!=?13)?{?password?+=?a;?putc('*',stdout);?}?cout?<?<password.c_str();?return?0;?}3.?rownum的應(yīng)用可以用于取出表中的前若干行數(shù)據(jù)4.?create?as創(chuàng)建表create?table?tt(aa,cc?)?as?select?sno,?'0'?from?student?union?all?select?sno,?'1'?from?temselect?1?from?studentselect?*?from?ttdrop?table?tt2008-10-18?Saturday1.?Insert?into?批量插入今天才得知,以前忽略了insert?into?tt?select?sno?,?1?from?studentinsert?into?可以插入?查詢結(jié)果如果是varchar2?型的數(shù)據(jù),其長(zhǎng)度可以不相等如果是char型的數(shù)據(jù),長(zhǎng)度必須要相等2.?truncate?刪除表內(nèi)容Truncate?和?delete的區(qū)別清除了內(nèi)容,而delete只是設(shè)置了標(biāo)記2008-10-19?Sunday改程序2008-10-20?Monday1.?編譯問題程序在更新后,連接編譯出現(xiàn)了嚴(yán)重的問題解決:Project?->?Settings?????C/C++?選項(xiàng)卡????use?run-time?library選擇?Debug?MultithreadedTools?–>?Options????Show?directories?for??查看里面的各項(xiàng)F:/products/HABOSS/haboss_acct/bin/createtable.exeF:/products/HABOSS/haboss_acct/bin/createtree.exe簡(jiǎn)直讓我崩潰了Project????Set?active?Project?還是不清楚原因,怎么突然就編譯出錯(cuò),后來不知道怎么又可以了2008-10-21?Tuesday1.?Outlook設(shè)置其實(shí)上次還是沒有解決找到原因了在用戶屬性—>?服務(wù)器?中發(fā)送郵件服務(wù)器我的服務(wù)器要求身份驗(yàn)證???要選中使用安全密碼驗(yàn)證登錄???不能選2.?SQL?SERVER選指定行ORACLE中有rownum這個(gè)方法Select?no=Identity(int,1,1),*?Into?#temptable?From?dbo.teacher_info?order?by?teacher_name--利用Identity函數(shù)生成記錄序號(hào)?Select?*?From?#temptable?Where?no>=10?And?no?<?20?Drop?Table?#temptable--用完后刪除臨時(shí)表?3.?公交查詢SQL語句直達(dá)查詢SELECT?a.rout?from?rout_stop?a,?rout_stop?b?where?a.rout?=?b.rout?and?a.stop?=?'3'?and?b.stop?=?'6'轉(zhuǎn)乘1次select?distinct?a.stop?v?from?rout_stop?a?where?????(select?rout?from?rout_stop?where?stop?=?'1')?????????in????????(select?rout??from?rout_stop?where?stop?=?a.stop)?????and????(select?rout?from?rout_stop?where?stop?=?'11')?in?(select?rout??from?rout_stop?where?stop?=?a.stop)在csdn中可以對(duì)問題關(guān)鍵詞進(jìn)行搜索查詢2008-10-22?Wednesday1.?連接數(shù)據(jù)庫(kù)????j?=?DbFactory.GetConnection(DB_INDEX_SOBAK,?g_struGlobalParam.szCenter,?soConn);找到配置文件中表,從表中找到一個(gè)連接,連接到一個(gè)庫(kù)中,就可以對(duì)這個(gè)庫(kù)的的所有用戶進(jìn)行訪問了2.?Oracle?創(chuàng)建用戶http:TAB:CREATE?USER鑒于用戶空間分配和使用問題,建議在創(chuàng)建用戶的時(shí)候就為用戶指定缺省的表空間。比較完善的創(chuàng)建用戶的語句如下:CREATE?USER?<username>?IDENTIFIED?BY?<password>DEFAULT?TABLESPACE?<tablespace_name>TEMPORARY?TABLESPACE?<tablespace_name>;以創(chuàng)建katrina用戶為例:SQL>?CREATE?USER?katrina?IDENTIFIED?BY?iloveyou2?DEFAULT?TABLESPACE?users3?TEMPORARY?TABLESPACE?temp;User?created.更改缺省數(shù)據(jù)表空間的語法為:alter?database?default?tablespace?<tablespace_name>;更改缺省臨時(shí)表空間的語法為:alter?database?default?temporary?tablespace?<tablespace_name>;配置文件[ORACLE]ConnectString?=?XEPassWord?=sunUserName?=sun這樣就可以實(shí)現(xiàn)程序和本地?cái)?shù)據(jù)庫(kù)的連接了程序:int?main(){????long?j?=?Login("ORACLE");????if?(j?<=?0)????{????????cout?<<?"數(shù)據(jù)庫(kù)連接失敗"?<<?endl;????????return?-1;????}????CRecords?record(DefaultConnect,?1);????char?str[256];????int?i?=?0;????strcpy(str,?"select?*?from?student");????j?=?record.Query(str);????if?(j?<=?0)????{????????cout?<<?DefaultConnect.GetMsg()?<<?endl;????????cout?<<?"Query"?<<?endl;????}????do?{????????i++;????????cout?<<?record.Field(0).Char()?<<?endl;????}?while(record.Next()?>?0);????cout?<<?i?<<?endl;????Logout();????????return?0;}2008-10-23?Thursday1.?C程序void?foo(int?n)?{?????if?(n--?>?0)?????{??????printf("hello?world!/n");??????*(&n-1)?-=?5;????}??}void?main(){????foo(7);}構(gòu)成了循環(huán)2.?PL/SQL語言簡(jiǎn)單的如begin?????insert?into?student?values('002',?'Mary');?????commit;end;循環(huán)的練習(xí)declare???--?Local?variables?here??i?integer;??v_counter?binary_integer:=?1;begin??--?Test?statements?here??while?v_counter?<?3?loop????????insert?into?student?values('005',?'Jack');????????v_counter?:=?v_counter?+?1;???end?loop;??commit;end;3.?execute?immediatehttp:begin?????execute?immediate?'insert?into?student?values(:1,:2)'?????using?'003','Jam';?????commit;end;可以用來實(shí)現(xiàn)動(dòng)態(tài)語句2008-10-24?Friday1.?狀態(tài)篩選法http:有1000瓶白酒,其中有1瓶是毒酒,現(xiàn)在用10個(gè)耗子去試酒,耗子喝道毒酒后2個(gè)小時(shí)后毒性發(fā)作。?現(xiàn)在要在3個(gè)小時(shí)內(nèi),設(shè)計(jì)程序,盡可能多的找出沒有問題的白酒來。1000瓶酒從1到1000編號(hào),?10只老鼠以喝和不喝為兩種狀態(tài),?這樣1000瓶酒可以用10只老鼠的不同組合代表.? 老鼠編號(hào) 9 8 7 6 5 4 3 2 1 0?酒編號(hào)?1 1?2 1 0?3 1 1?4 1 0 0?5 1 0 1?6 1 1 0?7 1 1 1?8 1 0 0 0?9 1 0 0 1?10 1 0 1 0?11 1 0 1 1?12 1 1 0 0?13 1 1 0 1?14 1 1 1 0?15 1 1 1 1?16 1 0 0 0 0? ...?999 1 1 1 1 1 0 0 1 1 1?1000 1 1 1 1 1 0 1 0 0 02.?判斷表是否存在select?count(*)?from?user_tables?where?table_name?=?'STUDENT'--alltables從表名中進(jìn)行查詢做事情要提高效率,盡快去解決,抓緊時(shí)間,珍惜時(shí)間3.?公交查詢?cè)O(shè)計(jì)查詢站點(diǎn)5可以直達(dá)的所有站點(diǎn)SQL>?select?distinct?stop?from?rout_stop?where?rout?in2???(select?distinct?rout?from?rout_stop?where?stop?=?'5');應(yīng)該敢于嘗試寫程序的時(shí)候不清晰,很猶豫,對(duì)整體沒有把握清楚2008-10-25?Saturday1.?公交查詢查詢1條線路經(jīng)過的站點(diǎn)select?s.stop_name,?b.order_no?from?stop_infor?s,?bus_infor?b?????????where??s.stop_no?=?b.stop_no?and?b.rout?=?'9'?order?by?b.order_no轉(zhuǎn)乘一次??select?distinct?a.stop_no?v?from?bus_infor?a?where?--a.stop_no?=?b.stop_no?and????(select?rout?from?bus_infor?where?stop_no?=?'1')?????????in????????(select?rout??from?bus_infor?where?stop_no?=?a.stop_no?)????and????(select?rout?from?bus_infor?where?stop_no?=?'9')?in?(select?rout??from?bus_infor?where?stop_no?=?a.stop_no)2.?編程問題出現(xiàn)了很多問題,名稱寫錯(cuò),不易發(fā)現(xiàn)的錯(cuò)誤設(shè)計(jì)思路不清晰,寫程序時(shí)出現(xiàn)偏差注意:查詢語句應(yīng)該大寫,尤其是列名Alter?table?tname?add?colname?varchar2(2);?在表中增加列2008-10-26?Sunday1.?遠(yuǎn)程連接運(yùn)行???mstsc域?HNYD6-D3527AEFF2.?EXECUTE?Immediate當(dāng)執(zhí)行SQL語句時(shí),不要用分號(hào),當(dāng)執(zhí)行PL/SQL塊時(shí),在其尾部用分號(hào).declare???l_cnt?????varchar2(20);begin???execute?immediate?'select?count(1)?from?emp'?????into?l_cnt;??--into?在單引號(hào)’’的外面???dbms_output.put_line(l_cnt);end;注意這個(gè)寫法,細(xì)節(jié)練習(xí)declare????v_count?number?:=?0;begin????execute?immediate?'select?count(*)?from?user_tables?where?table_name?=?''STUDENT'''?–‘要用2個(gè)????into?v_count;????if?v_count?=?1?then????????dbms_output.put_line('找到');????else????????dbms_output.put_line('不存在');????end?if;end;/3.?游標(biāo)可以通過游標(biāo)操縱數(shù)據(jù)庫(kù),對(duì)游標(biāo)所指的行進(jìn)行update,?delete?declare????v_no????student.sno%type;????v_name??student.sname%type;????cursor?s_cur?is?select?sno,?sname?from?student?for?update?of?sname;begin????for?s_record?in?s_cur?loop????????fetch?s_cur?into?v_no,v_name;????????dbms_output.put_line(v_no||'?'||v_name);--??????dbms_output.put_line(v_name||'?');????????if?v_no?=?'005'?then????????????????????update?student?set?sname?=?'HH'?where?current?of?s_cur;????????end?if;????end?loop;????commit;end;/有問題游標(biāo)的for循環(huán)中,不再需要用fetch了2008-10-27?Monday1.?PL/SQL塊調(diào)用過程C:/Documents?and?Settings/sun>sqlplusSQL*Plus:?Release?10.2.0.1.0?-?Production?on?星期一?10月?27?14:04:09?2008Copyright?(c)?1982,?2005,?Oracle.??All?rights?reserved.請(qǐng)輸入用戶名:??sun輸入口令:連接到:Oracle?Database?10g?Express?Edition?Release?10.2.0.1.0?-?ProductionSQL>?set?serveroutput?onSQL>?start?D:/sql001?Tom002?Mary005?HH005?HH003?Jam006?HackPL/SQL?過程已成功完成。2.?塊中的輸出????dbms_output.put_line(to_char(5));????dbms_output.put_line(3);2008-10-28?Tuesday1.?訪問表每一行的存儲(chǔ)過程Testfor.sqlcreate?or?replace?procedure?testforisbegin????for?c?in?(select?*?from?student)?loop????????dbms_output.put_line(c.SNO?||?'??'?||?c.SNAME);????end?loop;end?testfor;/SQL>?set?serveroutput?onSQL>?start?D:/testfor過程已創(chuàng)建。SQL>?execute?testfor001??Tom002??Mary005??HH005??HH003??Jam006??HackPL/SQL?過程已成功完成。execute?testfor??直接調(diào)用過程通過for?c?in?(select?*?from?student)?loop這種循環(huán)方式達(dá)到游標(biāo)循環(huán)的效果,比用游標(biāo)要方便修改數(shù)據(jù)update的時(shí)候create?or?replace?procedure?testforisbegin????for?c?in?(select?*?from?student)?loop????????dbms_output.put_line(c.SNO?||?'??'?||?c.SNAME);????????????????if?c.SNO?=?'005'?THEN????????????update?student?set?SNAME?=?'MM'?where?SNO?=?'005';????????????dbms_output.put_line('if?go');????????????dbms_output.put_line(c.SNO?||?'??'?||?c.SNAME);????????end?if;????????????end?loop;????commit;end?testfor;/Update要全表掃描,沒有游標(biāo)那樣的單行處理功能2.?獲取列名SQL>?select?column_name?from?all_tab_columns?where?table_name?='STUDENT';COLUMN_NAME------------------------------------------------------------SNOSNAMEselect?column_name?from?all_tab_columns?where?table_name?='STUDENT';?select?column_name?from?user_tab_cols?t?where?t.table_name=?'STUDENT'SQL>?show?error顯示錯(cuò)誤信息3.?存儲(chǔ)過程練習(xí)編譯出現(xiàn)問題,要多嘗試create?or?replace?procedure?GetColList(tname?in?varchar2)--,?list?in?out?varchar2)is????c_list?varchar2(1000)?:=?'';begin????for?c?in?(select?column_name?from?all_tab_columns?where?table_name?=?tname)?loop????????c_list?:=?c_list?||?c.COLUMN_NAME?||?',';????end?loop;????????dbms_output.put_line(c_list);end?GetColList;/SQL>?start?D:/testfor過程已創(chuàng)建。SQL>?execute?GetColList('STUDENT')SNO,SNAME,PL/SQL?過程已成功完成。????dbms_output.put_line(c_list);SQL>?execute?GetColList('STUDENT')SNO,SNAME
總結(jié)
以上是生活随笔為你收集整理的笔记(数据库)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。