笔记(数据库)
来源:互联网 发布:搜狗输入法 mac 设置 编辑:程序博客网 时间:2024/06/06 09:16
- 2008-10-11 Saturday
- 1. 导入数据时间分析
- 用amsConn->ExecSql(str);方式 插入5543行记录用时13.391秒
- 用 CRecords cRecord(*amsConn, 1);
- cRecord.Query(str);
- 方式,插入5543行记录用时26.61秒
- 用表类的Insert命令时
- 用有线时
- OK
- 5543
- 4.953
- 用时4.953秒
- 体现出表类操作的优势,一次提交,节省时间
- 2. ORACLE基础
- 在命令行中操作
- C:/Documents and Settings/sun>sqlplus/nolog
- SQL> conn /as sysdba
- 已连接。
- SQL> startup
- ORACLE 例程已经启动。
- SQL> select * from student;
- SNO SNAME AGE
- ---------------- ---------------------------------------- ----------
- 001 Mary 19
- Tom 21
- SQL> 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.ctl
- load data
- infile 'data.txt'
- append into table STUDENT
- fields terminated by ','
- (SNO, SNAME)
- ===========================
- D盘中data.txt
- 041,Jack,
- 042,,
- ===========================
- 命令行命令
- C:/Documents and Settings/sun>d:
- D:/>sqlldr ams1@hadev/ams1 control=input.ctl
- SQL*Loader: Release 10.2.0.1.0 - Production on 星期六 10月 11 21:41:05 2008
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- 达到提交点 - 逻辑记录计数 1
- 达到提交点 - 逻辑记录计数 2
- D:/>
- =============================
- 导入时可以设置格式,日期型数据则必须要进行设置
- 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.81
- CPU 时间为: 00: 00: 00.23
- 2008-10-12 Sunday
- 1. sqlloader的筛选技巧
- Load data
- infile 'data.txt'
- Append into table student
- fields terminated by ','
- (
- SNO,
- x filler,
- SNAME
- )
- 使用filler进行过滤
- 2. outlook设置
- 可以接收邮件,但是不能发送
- 修改设置
- 工具----帐户-----属性----高级-----服务器延时 时间拉长就可以了
- 10月 第3周
- 2008-10-13 Monday
- 1. sscanf
- 16进制转换为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.sql
- SQL> @d:/see.sql
- 或者
- SQL> start d:/see.sql
- 附:sqlplus 的使用
- C:/Documents and Settings/sun>sqlplus/nolog
- SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 10月 13 19:57:22 2008
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- SQL> conn
- 请输入用户名: ams1@hadev
- 输入口令:ams1 注意:这里的输入不会显示出来
- 已连接。
- 3. SQL存储过程
- C:/Documents and Settings/sun>sqlplus
- SQL*Plus: Release 10.2.0.1.0 - Production on 星期五 10月 24 14:49:57 2008
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- 请输入用户名: sun
- 输入口令:
- 连接到:
- Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
- SQL> 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 Tuesday
- 1. 批量执行导入数据
- 写一个程序,用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 表a
- 2008-10-15 Wednesday
- 1. 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 Thursday
- 1. 关闭系统自动更新
- 第一,“我的电脑”点右键,选“属性”,“自动更新”把有关自动更新的选项去掉。
- 第二,“我的电脑”点右键,选“管理”,点左边“服务和应用程序”旁的加号,展开,点“服务”。
- 或者直接在“运行”中输入“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 Friday
- 1. 判断表是否为空
- 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 tem
- select 1 from student
- select * from tt
- drop table tt
- 2008-10-18 Saturday
- 1. Insert into 批量插入
- 今天才得知,以前忽略了
- insert into tt select sno , 1 from student
- insert into 可以插入 查询结果
- 如果是varchar2 型的数据,其长度可以不相等
- 如果是char型的数据,长度必须要相等
- 2. truncate 删除表内容
- Truncate 和 delete的区别
- 清除了内容,而delete只是设置了标记
- 2008-10-19 Sunday
- 改程序
- 2008-10-20 Monday
- 1. 编译问题
- 程序在更新后,连接编译出现了严重的问题
- 解决:Project -> Settings C/C++ 选项卡 use run-time library
- 选择 Debug Multithreaded
- Tools –> Options Show directories for 查看里面的各项
- F:/products/HABOSS/haboss_acct/bin/createtable.exe
- F:/products/HABOSS/haboss_acct/bin/createtree.exe
- 简直让我崩溃了
- Project Set active Project
- 还是不清楚原因,怎么突然就编译出错,后来不知道怎么又可以了
- 2008-10-21 Tuesday
- 1. 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 Wednesday
- 1. 连接数据库
- 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 iloveyou
- 2 DEFAULT TABLESPACE users
- 3 TEMPORARY TABLESPACE temp;
- User created.
- 更改缺省数据表空间的语法为:alter database default tablespace <tablespace_name>;
- 更改缺省临时表空间的语法为:alter database default temporary tablespace <tablespace_name>;
- 配置文件
- [ORACLE]
- ConnectString = XE
- PassWord =sun
- UserName =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 Thursday
- 1. 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 immediate
- http:
- begin
- execute immediate 'insert into student values(:1,:2)'
- using '003','Jam';
- commit;
- end;
- 可以用来实现动态语句
- 2008-10-24 Friday
- 1. 状态筛选法
- 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 0
- 2. 判断表是否存在
- select count(*) from user_tables where table_name = 'STUDENT'
- --alltables
- 从表名中进行查询
- 做事情要提高效率,尽快去解决,抓紧时间,珍惜时间
- 3. 公交查询设计
- 查询站点5可以直达的所有站点
- SQL> select distinct stop from rout_stop where rout in
- 2 (select distinct rout from rout_stop where stop = '5');
- 应该敢于尝试
- 写程序的时候不清晰,很犹豫,对整体没有把握清楚
- 2008-10-25 Saturday
- 1. 公交查询
- 查询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 Sunday
- 1. 远程连接
- 运行 mstsc
- 域 HNYD6-D3527AEFF
- 2. 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 Monday
- 1. PL/SQL块调用过程
- C:/Documents and Settings/sun>sqlplus
- SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 10月 27 14:04:09 2008
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- 请输入用户名: sun
- 输入口令:
- 连接到:
- Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
- SQL> set serveroutput on
- SQL> start D:/sql
- 001 Tom
- 002 Mary
- 005 HH
- 005 HH
- 003 Jam
- 006 Hack
- PL/SQL 过程已成功完成。
- 2. 块中的输出
- dbms_output.put_line(to_char(5));
- dbms_output.put_line(3);
- 2008-10-28 Tuesday
- 1. 访问表每一行的存储过程
- Testfor.sql
- create or replace procedure testfor
- is
- begin
- for c in (select * from student) loop
- dbms_output.put_line(c.SNO || ' ' || c.SNAME);
- end loop;
- end testfor;
- /
- SQL> set serveroutput on
- SQL> start D:/testfor
- 过程已创建。
- SQL> execute testfor
- 001 Tom
- 002 Mary
- 005 HH
- 005 HH
- 003 Jam
- 006 Hack
- PL/SQL 过程已成功完成。
- execute testfor 直接调用过程
- 通过for c in (select * from student) loop
- 这种循环方式达到游标循环的效果,比用游标要方便
- 修改数据update的时候
- create or replace procedure testfor
- is
- begin
- 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
- ------------------------------------------------------------
- SNO
- SNAME
- select 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