生活随笔
收集整理的這篇文章主要介紹了
笔记(数据库)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2008-10-11?Saturday1.?導入數據時間分析用amsConn->ExecSql(str);方式?插入5543行記錄用時13.391秒用???CRecords?cRecord(*amsConn,?1);????????cRecord.Query(str);方式,插入5543行記錄用時26.61秒用表類的Insert命令時用有線時OK55434.953用時4.953秒體現出表類操作的優勢,一次提交,節省時間2.?ORACLE基礎在命令行中操作C:/Documents?and?Settings/sun>sqlplus/nologSQL>?conn?/as?sysdba已連接。SQL>?startupORACLE?例程已經啟動。SQL>?select?*?from?student;SNO??????????????SNAME???????????????????????????????????????????AGE----------------?----------------------------------------?----------001??????????????Mary?????????????????????????????????????????????19?????????????????Tom??????????????????????????????????????????????21SQL>?shutdown數據庫已經關閉。已經卸載數據庫。ORACLE?例程已經關閉。3.?sqlloader使用創建用戶創建表空間查找的資料1、在Oracle中按照導入數據的格式建立一個空表2、編寫一個loader.ctl文件,內容如下load?data??infile?'/backup/incoming/SDSS.csv'?into?table?SDSS??????fields?terminated?by?","(?RA,?DEC,?MAG?)????操作分類:?????????a、insert,為缺省方式,在數據裝載開始時要求表為空????????b、append,在表中追加新記錄????????c、replace,刪除舊記錄,替換成新裝載的記錄????????d、truncate,同上3、在CMD中執行以下語句:????????sqlldr?sss/oracle?control=loader.ctl?4、自動在當前目錄下生成兩個文件????????默認日志文件名為:loader.log?????????默認壞記錄文件為:SDSS.bad?注意事項:1、必須在服務器本機上操作,不能在只安裝Oracle客戶端的機器上使用。2、CSV文件的內容是以“,“分隔的,如果數據在結尾沒有逗號導入時就會出錯。可以利用UltraEdit等文本編輯器處理一下,將?^p?替換為?,^p?就可以在每行的末尾加上一個逗號?了。3、數據導入需要較長的時間。4、導入不同的表需要使用不同的.ctl文件==========================?????實例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.達到提交點?-?邏輯記錄計數?1達到提交點?-?邏輯記錄計數?2D:/>=============================?????導入時可以設置格式,日期型數據則必須要進行設置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??????????????????????)??????????????????????這種方式進行數據導入效率極高,立刻完成經過時間為:?00:?00:?00.81CPU?時間為:?00:?00:?00.23???????????????????????????2008-10-12?Sunday1.?sqlloader的篩選技巧Load?datainfile?'data.txt'Append?into?table?studentfields?terminated?by?','(????SNO,????x?filler,????SNAME)使用filler進行過濾2.?outlook設置可以接收郵件,但是不能發送修改設置?工具----帳戶-----屬性----高級-----服務器延時???時間拉長就可以了10月?第3周2008-10-13?Monday1.?sscanf16進制轉換為10進制int?main()?{?????const?char*?a="0xff";?????int?i;?????sscanf(a,?"%x",?&i);?????printf("%d",?i);?????return?0;?}?2.?SQL語句批量執行http:如批量的更新或插入放在一個文件中,如:c:/test.sql?則在pl/sql或sqlplus下僅需執行:@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請輸入用戶名:??ams1@hadev輸入口令:ams1??注意:這里的輸入不會顯示出來已連接。3.?SQL存儲過程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.請輸入用戶名:??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???/過程已創建。SQL>?execute?in_stu('099','Monday')PL/SQL?過程已成功完成。SQL>SQL>?DROP?PROCEDURE?in_stu;過程已刪除。4.?PL/SQL導入工具很好的一個工具,使用方便,當然是會用了之后,可以任意實現列的對應Tools-----??Text?Importer先導入數據,預覽,再做設置,有疑問點擊Help,講的很清楚2008-10-14?Tuesday1.?批量執行導入數據寫一個程序,用sprintf把數據寫入到sql文件中,再在sqlplus中執行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;}結果_2345_2.?PLSQL中的命令行File-??New???Command?Window就可以執行命令行語句了3.?備份表的程序編寫仿照上次寫的程序,基本上沒有出現什么問題問題:函數如何組織起來,分寫多個函數,還是寫一個函數,傳入參數實現多功能在ams1的用戶中,可以通過ams2.tableName的方式去訪問ams2帳戶的表2008-10-16?Thursday1.?關閉系統自動更新第一,“我的電腦”點右鍵,選“屬性”,“自動更新”把有關自動更新的選項去掉。?第二,“我的電腦”點右鍵,選“管理”,點左邊“服務和應用程序”旁的加號,展開,點“服務”。?或者直接在“運行”中輸入“Services.msc”打開服務設置窗口。?在“服務”列表中,找到“AutomaticUpdates”這一項,雙擊,彈出的屬性窗口中,“啟動類型”設置為“已禁用”,確定即可。?“AutomaticUpdates”這個進程的作用是:自動從WindowsUpdate網絡更新補丁。利用WindowsUpdate功能進行升級。2.?字符串連接update?tem_a?set?table_Name?=?table_name||'_G'||?用于實現字符串的連接3.?刪除數據今天在csdn上看到,才知道這個問題使用:truncate?table?表名;?它可以釋放占用的數據塊表空間,但此操作不可回退。?使用delete刪除記錄不能釋放Oracle里被占用的數據塊表空間,它只把那些被刪除的數據塊標成unused。4.?注意COMMIT在數據庫的操作中,如用for?update?之后,要及時的commit,如果出現問題,可以用rollback來取消修改;2008-10-17?Friday1.?判斷表是否為空select?count(*)?from?res.SERVE_REPAIR_INFO?where?rownum?=?1不用全表掃描,只要找出1行記錄就可以,如果僅僅是要看有無記錄2.?控制臺實現輸入密碼不顯示來自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的應用可以用于取出表中的前若干行數據4.?create?as創建表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?可以插入?查詢結果如果是varchar2?型的數據,其長度可以不相等如果是char型的數據,長度必須要相等2.?truncate?刪除表內容Truncate?和?delete的區別清除了內容,而delete只是設置了標記2008-10-19?Sunday改程序2008-10-20?Monday1.?編譯問題程序在更新后,連接編譯出現了嚴重的問題解決:Project?->?Settings?????C/C++?選項卡????use?run-time?library選擇?Debug?MultithreadedTools?–>?Options????Show?directories?for??查看里面的各項F:/products/HABOSS/haboss_acct/bin/createtable.exeF:/products/HABOSS/haboss_acct/bin/createtree.exe簡直讓我崩潰了Project????Set?active?Project?還是不清楚原因,怎么突然就編譯出錯,后來不知道怎么又可以了2008-10-21?Tuesday1.?Outlook設置其實上次還是沒有解決找到原因了在用戶屬性—>?服務器?中發送郵件服務器我的服務器要求身份驗證???要選中使用安全密碼驗證登錄???不能選2.?SQL?SERVER選指定行ORACLE中有rownum這個方法Select?no=Identity(int,1,1),*?Into?#temptable?From?dbo.teacher_info?order?by?teacher_name--利用Identity函數生成記錄序號?Select?*?From?#temptable?Where?no>=10?And?no?<?20?Drop?Table?#temptable--用完后刪除臨時表?3.?公交查詢SQL語句直達查詢SELECT?a.rout?from?rout_stop?a,?rout_stop?b?where?a.rout?=?b.rout?and?a.stop?=?'3'?and?b.stop?=?'6'轉乘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中可以對問題關鍵詞進行搜索查詢2008-10-22?Wednesday1.?連接數據庫????j?=?DbFactory.GetConnection(DB_INDEX_SOBAK,?g_struGlobalParam.szCenter,?soConn);找到配置文件中表,從表中找到一個連接,連接到一個庫中,就可以對這個庫的的所有用戶進行訪問了2.?Oracle?創建用戶http:TAB:CREATE?USER鑒于用戶空間分配和使用問題,建議在創建用戶的時候就為用戶指定缺省的表空間。比較完善的創建用戶的語句如下:CREATE?USER?<username>?IDENTIFIED?BY?<password>DEFAULT?TABLESPACE?<tablespace_name>TEMPORARY?TABLESPACE?<tablespace_name>;以創建katrina用戶為例:SQL>?CREATE?USER?katrina?IDENTIFIED?BY?iloveyou2?DEFAULT?TABLESPACE?users3?TEMPORARY?TABLESPACE?temp;User?created.更改缺省數據表空間的語法為:alter?database?default?tablespace?<tablespace_name>;更改缺省臨時表空間的語法為:alter?database?default?temporary?tablespace?<tablespace_name>;配置文件[ORACLE]ConnectString?=?XEPassWord?=sunUserName?=sun這樣就可以實現程序和本地數據庫的連接了程序:int?main(){????long?j?=?Login("ORACLE");????if?(j?<=?0)????{????????cout?<<?"數據庫連接失敗"?<<?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);}構成了循環2.?PL/SQL語言簡單的如begin?????insert?into?student?values('002',?'Mary');?????commit;end;循環的練習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;可以用來實現動態語句2008-10-24?Friday1.?狀態篩選法http:有1000瓶白酒,其中有1瓶是毒酒,現在用10個耗子去試酒,耗子喝道毒酒后2個小時后毒性發作。?現在要在3個小時內,設計程序,盡可能多的找出沒有問題的白酒來。1000瓶酒從1到1000編號,?10只老鼠以喝和不喝為兩種狀態,?這樣1000瓶酒可以用10只老鼠的不同組合代表.? 老鼠編號 9 8 7 6 5 4 3 2 1 0?酒編號?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從表名中進行查詢做事情要提高效率,盡快去解決,抓緊時間,珍惜時間3.?公交查詢設計查詢站點5可以直達的所有站點SQL>?select?distinct?stop?from?rout_stop?where?rout?in2???(select?distinct?rout?from?rout_stop?where?stop?=?'5');應該敢于嘗試寫程序的時候不清晰,很猶豫,對整體沒有把握清楚2008-10-25?Saturday1.?公交查詢查詢1條線路經過的站點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轉乘一次??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.?編程問題出現了很多問題,名稱寫錯,不易發現的錯誤設計思路不清晰,寫程序時出現偏差注意:查詢語句應該大寫,尤其是列名Alter?table?tname?add?colname?varchar2(2);?在表中增加列2008-10-26?Sunday1.?遠程連接運行???mstsc域?HNYD6-D3527AEFF2.?EXECUTE?Immediate當執行SQL語句時,不要用分號,當執行PL/SQL塊時,在其尾部用分號.declare???l_cnt?????varchar2(20);begin???execute?immediate?'select?count(1)?from?emp'?????into?l_cnt;??--into?在單引號’’的外面???dbms_output.put_line(l_cnt);end;注意這個寫法,細節練習declare????v_count?number?:=?0;begin????execute?immediate?'select?count(*)?from?user_tables?where?table_name?=?''STUDENT'''?–‘要用2個????into?v_count;????if?v_count?=?1?then????????dbms_output.put_line('找到');????else????????dbms_output.put_line('不存在');????end?if;end;/3.?游標可以通過游標操縱數據庫,對游標所指的行進行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;/有問題游標的for循環中,不再需要用fetch了2008-10-27?Monday1.?PL/SQL塊調用過程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.請輸入用戶名:??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.?訪問表每一行的存儲過程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過程已創建。SQL>?execute?testfor001??Tom002??Mary005??HH005??HH003??Jam006??HackPL/SQL?過程已成功完成。execute?testfor??直接調用過程通過for?c?in?(select?*?from?student)?loop這種循環方式達到游標循環的效果,比用游標要方便修改數據update的時候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要全表掃描,沒有游標那樣的單行處理功能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顯示錯誤信息3.?存儲過程練習編譯出現問題,要多嘗試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過程已創建。SQL>?execute?GetColList('STUDENT')SNO,SNAME,PL/SQL?過程已成功完成。????dbms_output.put_line(c_list);SQL>?execute?GetColList('STUDENT')SNO,SNAME
總結
以上是生活随笔為你收集整理的笔记(数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。