176142998

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

          #

           在Struts2中整合Spring的IoC支持是一件十分簡(jiǎn)單的事情。讓我們一步一步來(lái)實(shí)現(xiàn):
              1)復(fù)制struts2-spring-plugin-x-x-x.jar和相應(yīng)的spring.jar到/WEB-INF/lib目錄下。
              2)在struts.properties中設(shè)置struts.objectFactory屬性值


              struts.properties
              struts.objectFactory = spring
              或者是在XML文件中進(jìn)行常量配置

              struts.xml
              <struts>
                  <constant name="struts.objectFactory" value="spring" />
              </struts>
              3)配置Spring監(jiān)聽器

              web.xml
              <listener>
                  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
              </listener>
              通過(guò)Spring配置來(lái)注冊(cè)對(duì)象

              applicationContext.xml
              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE beans PUBLIC
                  "-//SPRING//DTD BEAN//EN"
                  "http://www.springframework.org/dtd/spring-beans.dtd">
              <beans default-autowire="autodetect">
                  <bean id="hello" class="hpfyeah.struts2.spring.HelloWorldAction"/>
              </beans>
              當(dāng)然你也可以擁有更多的Spring配置文件。在web.xml中進(jìn)行下列設(shè)置,從而使Spring的ApplicationContext通過(guò)匹配所給定模式的文件來(lái)初始化對(duì)象

              web.xml
              <!-- 用來(lái)定位Spring XML文件的上下文配置 -->
              <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                      /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
                  </param-value>
              </context-param>
              4)修改你的Struts配置文件

              struts.xml
              <!DOCTYPE struts PUBLIC
                  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
                  "http://struts.apache.org/dtds/struts-2.0.dtd">
              <struts>
                  <include file="struts-default.xml"/>
                  <package name="default" extends="struts-default">
                      <action name="pureStruts" class="hpfyeah.struts2.spring.HelloWorldAction">
                          <result>hello.jsp</result>
                      </action>
                      <action name="springStruts" class="hello">
                          <result>hello.jsp</result>
                      </action>
              </struts>
              默認(rèn)情況下,Spring從上面顯示的applicationContext.xml文件中尋找為hello所做的配置

          5)好了,現(xiàn)在你的Struts2和Spring就能正常的一起工作了。有幾個(gè)配置技術(shù)點(diǎn)需要詳細(xì)說(shuō)明下:
              裝配模式。你可以通過(guò)設(shè)置修改struts.properties中下列屬性的值來(lái)改變裝配模式。

              name 按照你的action的屬性的名字和Spring里的bean的名字匹配,如果匹配就自動(dòng)裝配。這是缺省的
              type 按照你的action的屬性的類型,在Spring注冊(cè)的bean中查找,如果相同就自動(dòng)裝配。這需要你在Spring中僅注冊(cè)了一個(gè)此類型的bean
              auto Spring會(huì)試圖自動(dòng)監(jiān)測(cè)來(lái)找到最好的方法自動(dòng)裝配你的action
              constructor Spring會(huì)自動(dòng)裝配bean的構(gòu)造函數(shù)的參數(shù)


              是否使用類緩存。你可以通過(guò)設(shè)置修改struts.properties中下列屬性的值來(lái)改變是否使用Spring自身的類緩存機(jī)制。可以設(shè)定的值為true或false,默認(rèn)為true。

              struts.properties
              struts.objectFactory.spring.useClassCache = false


          http://dev.rdxx.com/Java/Struts/2008/5/2313512756889_2.shtml

          posted @ 2008-06-12 15:55 飛飛 閱讀(305) | 評(píng)論 (0)編輯 收藏

          Spring 的快速開發(fā), 說(shuō)是快速開發(fā), 其實(shí)能幫助的地方除了語(yǔ)法高亮和自動(dòng)添加類庫(kù)外也沒多少東西了。

            1. 新建普通 Java 項(xiàng)目 MySpringTest. 這個(gè)過(guò)程無(wú)需贅述了, 建議建項(xiàng)目的時(shí)候?qū)?src 目錄和 bin(或者classes)目錄分開, 另外提示你切換透視圖的時(shí)候一定要切換過(guò)去到 Java 透視圖, 此時(shí)默認(rèn)會(huì)在 Package Explorer 中選中剛才已經(jīng)建好的 Java Project, 但是背景為灰色。

            2. 首先單擊一下左邊的 Package Explorer 中新建的 MySpringTest 項(xiàng)目來(lái)使其高亮選中, 接著點(diǎn)擊菜單項(xiàng) MyEclipse -> Add Spring Capabilities……, 接著會(huì)彈出對(duì)話框 Add Spring Capabilities 提示你設(shè)置當(dāng)前項(xiàng)目的 Spring 屬性。

            對(duì)話框的第一頁(yè)可以選擇全部的 Spring 框架, 這是最保險(xiǎn)的做法, 不過(guò)我們的例子只需要選中Spring 2.0 Core Libraries 就可以了。 點(diǎn)擊 "Next" 繼續(xù)。

            第二頁(yè)是 Add Spring bean configuration file. 保持默認(rèn)值不變就可以了。 最后點(diǎn)擊 Finish.

            3. Spring 的開發(fā)沒法自動(dòng)生成 Bean, 這里大家只好手工來(lái)寫了, 也很簡(jiǎn)單。 分別復(fù)制下面的三個(gè)代碼, 然后在 MyEclipse src 目錄上點(diǎn)擊右鍵后選擇菜單項(xiàng) Paste 就可以生成 Java 類文件了。

          public interface Action {
                      public String execute(String str);
                      }
                      public class UpperAction implements Action {
                      private String message;
                      public String getMessage() {
                      return message;
                      }
                      public void setMessage(String string) {
                      message = string;
                      }
                      public String execute(String str) {
                      return (getMessage() + str).toUpperCase();
                      }
                      }
                      import org.springframework.context.ApplicationContext;
                      import org.springframework.context.support.ClassPathXmlApplicationContext;
                      public class TestAction {
                      public static void main(String[] args) {
                      ApplicationContext ctx = new ClassPathXmlApplicationContext(
                      "applicationContext.xml");
                      Action bean = (Action) ctx.getBean("theAction");
                      System.out.println(bean.execute("Rod"));
                      }
                      }

            4. 雙擊左側(cè)在第2步生成的 applicationContext.xml, 然后選擇菜單項(xiàng) Window -> Show View -> Other……, 在彈出的對(duì)話框中選擇 MyEclipse Enterprise Workbench 節(jié)點(diǎn)下的 Spring Beans 子節(jié)點(diǎn)打開視圖 Spring Beans. 此視圖講出現(xiàn)在主界面的右下側(cè)。

            5. 展開此視圖中的 MySpringTest 父節(jié)點(diǎn), 并選中 src/applicationContext.xml 子節(jié)點(diǎn), 在此節(jié)點(diǎn)上點(diǎn)擊右鍵并選擇彈出菜單項(xiàng)中的 New Bean 來(lái)打開 Create a new Spring bean 對(duì)話框, 并按照下圖輸入對(duì)應(yīng)的內(nèi)容。

            Bean Id: [theAction]

            Bean class: [UpperAction]

            接下來(lái)請(qǐng)單擊一下 Tab 面板 Properties 并點(diǎn)擊其中的 Add…… 按鈕, 在接下來(lái)彈出的 Property Wizard 對(duì)話框中按照下圖輸入/選擇內(nèi)容:

            Name: [message]

            Spring type: [value]

            Type: [java.lang.String]

            Value:[Hello_]

            最后點(diǎn)擊兩次 Finish 按鈕關(guān)閉所有向?qū)?duì)話框。 然后點(diǎn)擊菜單 File -> Save. 此時(shí)可以看到 applicationContext.xml 的內(nèi)容如下所示:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans
          xmlns=" xmlns:xsi=" xsi:schemaLocation="http://www.springframework.org/schema/beans


          <bean id="theAction" class="UpperAction" abstract="false"
          lazy-init="default" autowire="default" dependency-check="default">
          <property name="message">
          <value type="java.lang.String">Hello_</value>
          </property>
          </bean></beans>

            然后雙擊 Package Explorer 下 MySpringTest/src/TestAction.java 打開源代碼, 然后點(diǎn)擊菜單 Run -> Run As -> 1. Java Application, 如果沒有錯(cuò)誤的話將會(huì)出現(xiàn)如下的輸入, 您的第一個(gè) Hello Spring 運(yùn)行成功了:

          log4j:WARN No appenders could be found for logger 
          (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. HELLO_ROD

            接著您就可以對(duì)著參考書繼續(xù)創(chuàng)建類, 修改 applicationContext.xml 做更多的練習(xí)了。

            開發(fā)整合 Hibernate 的關(guān)鍵操作點(diǎn)截圖:

            1. 在數(shù)據(jù)庫(kù)瀏覽器中選擇反向工程菜單;

            2. 對(duì)話框的選項(xiàng)說(shuō)明



          http://java.chinaitlab.com/Spring/727426.html

          posted @ 2008-06-12 15:15 飛飛 閱讀(217) | 評(píng)論 (0)編輯 收藏

          《1》子查詢和關(guān)聯(lián)查詢
          建立表如下:

          學(xué)生基本信息表
          CREATE Student(
          [Studentid][Int]IDENTITY(1,1)NOT NULL primary key,--主鍵
          [StudentName][char]NOT NULL

          )
          課程信息表
          CREATE Subject(
          [SubjectID][char]NOT NULL primary key,       --主鍵
          [SubjectName][char]NOT NULL
          )
          成績(jī)表
          CREATE Grade(
          [Studentid][Int]NOT NULL,  --聯(lián)合主鍵
          [SubjectID][char]NOT NULL,  --聯(lián)合主鍵
          [Grade] [INT]NOT NULL,
          primary key (studentid,subjectid)
          )

          1.將建表命令改為ORACLE語(yǔ)句在ORACLE中建表
          create table student( --學(xué)生表
          studentid number(3) primary key, --學(xué)生編號(hào)
          studentname varchar2(20) --學(xué)生的姓名
          );

          create table subject( --課程表
          subjectid char(3) primary key, --課程編號(hào)
          subjectname varchar2(20)  --課程的名字
          );


          create table grade( --分?jǐn)?shù)表
          studentid number(3) references student(studentid), --學(xué)生id
          subjectid char(3) references subject(subjectid), --課程id
          mark      number(3), --分?jǐn)?shù)
          primary key (studentid,subjectid) --聯(lián)合主鍵
          );

           

          insert into student values (101,'張三');
          insert into student values (102,'李云');
          insert into student values (103,'未');

          insert into subject values ('A01','C++');
          insert into subject values ('A02','ASP');
          insert into subject values ('A03','JAVA');


          insert into grade values (101,'A01',59);
          insert into grade values (101,'A02',72);
          insert into grade values (101,'A03',90);

          insert into grade values (102,'A01',75);
          insert into grade values (102,'A02',91);

          insert into grade values (103,'A01',71);

           


          2.作如下4題

          第一問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 課程名稱 成績(jī) (要全部學(xué)生信息)

          關(guān)聯(lián)查詢 (多張表的)
          別名

          select a.studentid as "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a , subject b , grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid;

          [select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a , subject b , grade c] 笛卡爾積

                 3 * 3 * 6 = 54;


          第二問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 課程名稱 成績(jī)(只顯示每科最高分)

          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          and (subjectname,mark)
          in (select subjectname "課程名稱",max(mark) "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          group by subjectname)

          (最高分---分?jǐn)?shù)比我高的學(xué)生的人數(shù)=0)
          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          and (select count(*) from grade
          where subjectid = b.subjectid and
          mark > c.mark) = 0


          第三問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 課程名稱 成績(jī) (成績(jī)大于60時(shí)的顯示及格,小于60時(shí)的顯示不及格)

          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",
          decode(sign(mark-60),-1,'不及格','及格') "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid

          第四問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 (查詢出課程超過(guò)1門以上學(xué)生的信息)

          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          count(subjectname)
          from student a , subject b , grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          group by a.studentid,studentname
          having count(subjectname) >= 2

           

          《2》復(fù)制一張表的結(jié)構(gòu)
           --數(shù)據(jù)一起復(fù)制
            create table mydept as select * from dept;
           --只復(fù)制結(jié)構(gòu)
            create table mydept1 as select * from dept where 1=2;
           --把數(shù)據(jù)從一個(gè)表復(fù)制到另一個(gè)結(jié)構(gòu)相同的表
             insert into mydept1 select * from dept;
           --只復(fù)制部分?jǐn)?shù)據(jù)
             insert into mydept1 select * from dept where deptno>20;
             insert into mydept1(deptno,loc) select deptno,loc
          from dept;

           --表的約束是復(fù)制不過(guò)來(lái)的,需要自己去添加


           --列的別名 
             select ename "員工 姓名" from emp;

           a.沒有主鍵的表如何消重復(fù)記錄

          create table test(
              id number(2),
              name varchar2(10));

             insert into test values (1,'aa');
             insert into test values (1,'aa');
             insert into test values (1,'aa');
             insert into test values (2,'bb');
             insert into test values (3,'cc');
           
            create table test1 as select distinct * from
            test;
            或者
            create table test1 as select id,name from test
            group by id,name;

           
            rename test to test2;
            rename test1 to test;

          b.有主鍵消重復(fù)行
           
          create table test(
              id number(2) primary key,
              name varchar2(10));

             insert into test values (1,'aa');
             insert into test values (2,'aa');
             insert into test values (3,'aa');
             insert into test values (4,'bb');
             insert into test values (5,'cc');

           create table test1 as select name from test
           group by name;

           alter table test1 add (id number(2));

           update test1 set id=(select max(id) from test
            where name=test1.name);

           create table test2 as select id,name from test1;

           rename test to testb;
           rename test2 to test;

           alter table test add primary key (id);


          <<2>>SQL*PLUS中的環(huán)境設(shè)置命令
            不需要分號(hào) 就可以執(zhí)行  ---  SQL*PLUS的環(huán)境命令
            需要分號(hào)結(jié)束 才能執(zhí)行的 -- SQL命令 (和SQLserver是一樣的)

             connect system/manager
             show user
             spool c:\aa.txt
             spool off
             clear screen
             set escape on     打開轉(zhuǎn)義功能

             set linesize 1000  設(shè)置一行顯示多少個(gè)字符
             set pagesize 20    設(shè)置一頁(yè)顯示多少行
          -------
             define aa = 10  定義一個(gè)變量 aa = 10
             define bb='MANAGER' 定義一個(gè)字符 bb = 'MANAGER'
            
             prompt Hello World   原樣輸出prompt后的字符串
            
             set feedback off     關(guān)閉系統(tǒng)反饋的信息
             set heading off      列標(biāo)題關(guān)閉
             set pagesize 0       不分頁(yè)
             set autocommit on    設(shè)置自動(dòng)提交打開
             set timing on        打開時(shí)間統(tǒng)計(jì)
             set time on          打開一個(gè)時(shí)鐘

          ------
            a) 自動(dòng)生成一個(gè)腳本
               利用SQL*PLUS環(huán)境命令  生成腳本文件
                  set heading off   --關(guān)閉列的標(biāo)題
                  set feedback off  --關(guān)閉反饋信息
           
                  spool c:\aa.txt   --緩沖寫文件c:\aa.txt

                  select 'insert into dept values ('||deptno||
           ','||''''||dname||''''||','||''''||loc||''''||');' from dept;   --執(zhí)行select語(yǔ)句

          spool off  --關(guān)閉緩沖

             用處:在備份 恢復(fù) 數(shù)據(jù)庫(kù)管理等方面有用處

           <4>ORACLE中建立數(shù)據(jù)庫(kù)對(duì)象
             表
             約束

             序列 sequence  ---  自動(dòng)編號(hào) ---- IDENTITY (SQLSERVER)
              <1>建立序列
               create sequence seq1; 從1開始 每次自動(dòng)增加1 沒有最大值
              <2>怎么使用
               select seq1.nextval from dual;
                  nextval 偽列  下一個(gè)值
               select seq1.currval from dual;
                  currval 偽列  當(dāng)前值
               
                create sequence seq2
              start with 1000        --起始值1000
              increment by 2         --步長(zhǎng)2
              maxvalue 9000          --最大值9000
              minvalue 900           --最小值900
              cycle                  --循環(huán)序列

             1000開始
              每次增加2 一直到9000
              回到900 重新開始

              <3>和表關(guān)聯(lián)作為表的字段的值
               a) create table student(
                    xh number(4) primary key, --學(xué)號(hào)
                    xm varchar2(20) not null --姓名
                  );

                  要求:學(xué)號(hào)字段 從1000開始每次增加4 最大值9999
                   --建立序列
                     create sequence xh_seq
                            start with 1000 --從1000開始
                            increment by 4  --每次增加4
                            maxvalue 9999   --最大值 9999
                            ;
                   --使用SQL語(yǔ)句關(guān)聯(lián)
                   insert into student values
                       (xh_seq.nextval,'Mike');
                   insert into student values
                       (xh_seq.nextval,'John');
                   insert into student values
                       (xh_seq.nextval,'Rose');
              
                --特點(diǎn):能產(chǎn)生唯一的值,但是不能保證值在表中的連續(xù)性
           
             b) create table teacher(
                     teacherxh varchar2(10) primary key,
                     teachername varchar2(20)
                    );
               要求:教師的編號(hào)的格式是TH00001
                                   TH00002
                                    ....
               
             --建立序列
                  create sequence teacher_seq
                        maxvalue 99999;
             --
               insert into teacher values ('TH'||
                 ltrim(to_char(teacher_seq.nextval,'00000')),
                 '張三');
                 insert into teacher values ('TH'||
                 ltrim(to_char(teacher_seq.nextval,'00000')),
                 '李');
                insert into teacher values ('TH'||
                 ltrim(to_char(teacher_seq.nextval,'00000')),
                 '王');

            視圖 (view)
             --建立視圖(用查詢語(yǔ)句 select empno,ename,sal from emp建立了一個(gè)emp_v1的視圖 )
               create or replace view emp_v1 as --select語(yǔ)句
                   select empno,ename,sal from emp;

             視圖的作用:隱藏?cái)?shù)據(jù),增加系統(tǒng)的安全性
                視圖中是否存放了數(shù)據(jù)???  不存放數(shù)據(jù),只存放查詢
                                       保證了 查詢到的數(shù)據(jù)是和表
                                       中的數(shù)據(jù)是一致的
             select * from emp_v1;
               <1>emp_v1先看是否是表
               <2>不是表 是否是視圖
               <3> select * from  (select empno,ename,sal from emp);
               <4>執(zhí)行得到結(jié)果

             能否通過(guò)視圖去改表中的數(shù)據(jù)呢???
                   有條件的可以:
                     <1>建立視圖的select語(yǔ)句必須是簡(jiǎn)單的select語(yǔ)句
                         簡(jiǎn)單:不能是多表的查詢
                              不能有分組函數(shù)
                     <2>建立視圖的時(shí)候不能帶with readonly關(guān)鍵字
               create or replace view emp_v2 as --select語(yǔ)句
                   select empno,ename,sal from emp
                   with read only;
                            
            可以改的情況:
               1)create or replace view emp_v1 as --select語(yǔ)句
                   select empno,ename,sal,comm from emp;
                  
               update emp_v1 set comm = 1000
                   where empno=7934;
           
               select * from emp;   --發(fā)現(xiàn)數(shù)據(jù)修改了

               2)create or replace view emp_v3
                  as
                    select empno,ename,hiredate,sal,deptno
                    from emp
                    where deptno = 10;

                 update emp_v3 set deptno=20 where empno=7782;
           
                 select * from emp_V3; --7782的數(shù)據(jù)沒有了

              3)
           
           

                          
                
               

           

           

            


             

           

           


           

          posted @ 2008-06-12 10:47 飛飛 閱讀(387) | 評(píng)論 (0)編輯 收藏

          回憶
              ORACLE安裝 和 刪除
              啟動(dòng)和關(guān)閉
              SQL*PLUS  windows下窗口版本
                         超級(jí)用戶 system/manager
                                 sys/change_on_install
                         普通用戶
                                 scott/tiger
                        DOS下的SQLPLUS版本
              如何使用
                命令show user
                SQL語(yǔ)句
          難點(diǎn)
              約束  如何給表加約束???

          SQL*PLUS環(huán)境命令
            結(jié)束時(shí)可以寫; 也可以不寫
            desc dept
            show user
            connect scott/tiger
            set linesize 200
            set pagesize 100
            spool c:\aa.txt
           
          SQL語(yǔ)句
            不需要分號(hào)結(jié)束的命令,sqlplus環(huán)境命令
               show user
               connect scott/tiger
               desc dept
               set linesize 200
               set pagesize 100
           
           SQL語(yǔ)句結(jié)束時(shí)候一定要 有分號(hào)或者換行加/

          《1》DDL語(yǔ)句(數(shù)據(jù)定義語(yǔ)言) Data Define Language
             create
             alter
             drop
             truncate 開頭的語(yǔ)句 truncate table <表名>
                特點(diǎn):<1>建立和修改數(shù)據(jù)對(duì)象
                     <2>建立和修改直接存入庫(kù)中,直接生效

              建立表
                 create table class(--班級(jí)表
                    classid number(2) primary key,
                    cname varchar2(20) not null);

                 /*wdsadsadsad
                   asdsadsadsadsad
                   多行注釋 
                  */ 
                 create table student( --學(xué)生表
                   xh number(4) primary key, --學(xué)號(hào)
                   name varchar2(10) not null, --姓名
                   sex  char(2)  check (sex in ('男','女')),--性別
                   birthday date,--生日
                   sal number(7,2), --獎(jiǎng)學(xué)金
                   classid number(2) references class(classid) --班級(jí)         
                 );

           外鍵引用的列一定是主鍵或有unique約束的列
           
            alter table student add (shengfenzheng number(18));

            drop table student;  刪除結(jié)構(gòu)
            delete from student; 只刪除數(shù)據(jù),速度慢,數(shù)據(jù)可以恢復(fù)
            truncate table student; 刪除記錄的 速度快 數(shù)據(jù)不能恢復(fù)

           《2》 DML語(yǔ)句(數(shù)據(jù)操作語(yǔ)言) Data Manupilate Language    
             select
             insert
             delete
             update
               特點(diǎn):<1>對(duì)數(shù)據(jù)起作用的
                   <2> 這些語(yǔ)句的修改是在內(nèi)存中發(fā)生的
                       要想改動(dòng)存入庫(kù)中必須要commit語(yǔ)句

           
            insert into student(xh,name,sex)
            values (&xh,'&n','&sd');
           
            insert into student(xh,name,sex)
            values (&學(xué)號(hào),'&姓名','&性別');

            轉(zhuǎn)義字符 \
            打開轉(zhuǎn)義 set escape on
            
            insert into dept
            values (90,'JO&HI','北京');

            insert into dept
            values (92,'JO\&HI','大家');


           《3》 TCL(事務(wù)控制語(yǔ)句) Transaction Control Language
            commit;  提交  修改保存到數(shù)據(jù)庫(kù)中
            rollback; 回滾  取消內(nèi)存中的改動(dòng)
            savepoint;保存點(diǎn) 分解事務(wù)的 把事務(wù)變小
                 DDL語(yǔ)句 會(huì)自動(dòng)提交以前未提交的事務(wù)
                 關(guān)閉SQLplus工具 也會(huì)自動(dòng)提交未提交的事務(wù)的
            事務(wù) -- 就是一個(gè)完整的對(duì)數(shù)據(jù)的DML操作
            所有事務(wù) 都是要明確的提交和回滾的
            --轉(zhuǎn)賬
              update 賬目表
              set 錢=錢-500
              where 帳號(hào)='A';
              update 賬目表
              set 錢=錢+500
              where 帳號(hào)='B';
              commit;


              事務(wù)何時(shí)存在 DML語(yǔ)句中除select以外都會(huì)有事務(wù)
            
          《《《《《《《注意》》》》》 / 重復(fù)運(yùn)行上一條SQL語(yǔ)句  

            commit;    結(jié)束上一個(gè)事務(wù) 并且開始一個(gè)新的事務(wù)

            update student set sal = null where xh =1000;

            savepoint c111;
            
            insert into student(xh,name,sex) values (1004,'MIKE','男');

            rollback to c111; --撤銷了插入的數(shù)據(jù)

            rollback;  --從c111這個(gè)點(diǎn)回滾到事務(wù)的開始點(diǎn)

          《SQLPLUS規(guī)則》 
             a)DML語(yǔ)句后跟上DDL語(yǔ)句 DML語(yǔ)句的事務(wù)會(huì)被自動(dòng)提交
             b)exit/quit命令 退出 SQLPLUS環(huán)境時(shí)也會(huì)自動(dòng)提交事務(wù)
                點(diǎn)小叉子關(guān)閉sqlplus窗口 事務(wù)都自動(dòng)回滾了
             c)非法操作是不能提交事務(wù)的 ,只能導(dǎo)致事務(wù)回滾
           
          《4》 DCL語(yǔ)句(數(shù)據(jù)控制語(yǔ)句) Data Control Language                    grant 授予權(quán)限
               revoke 撤銷權(quán)限
             權(quán)限 select ,insert,delete,update
                  all (select ,insert,delete,update 總和)
             角色 connect (登陸數(shù)據(jù)庫(kù)),resource(建立表和對(duì)象)
            
             如何建一個(gè)自己的用戶?
               必須是超級(jí)用戶才能建用戶
               --連接到超級(jí)用戶
               connect system/manager
               --建立用戶名zhangsan 密碼m123
               create user zhangsan identified by m123;
               --授予必要的權(quán)限connect 你能夠連接
                              resource 你能建表不受空間的限制,建立對(duì)象
               grant connect,resource to zhangsan;
               --這個(gè)普通用戶就建好了 和scott用戶的權(quán)限是一樣的     
               grant DBA to zhangsan; --張三的權(quán)限和System一樣
            
               --改張三的密碼
              <<1>> 自己改自己的密碼
                  connect zhangsan/m123
                  密碼改為了mm1 
                  alter user zhangsan identified by mm1;
              <<2>> 超級(jí)用戶來(lái)改
                  connect system/manager
                  alter user zhangsan identified by mm1; 
           
           
              

             在scott/tiger這個(gè)用戶下
               grant select on dept to zhangsan;
              在zhangsan下 可以使用select * from scott.dept;
                   看到結(jié)果
             
           在scott/tiger這個(gè)用戶下
                 revoke select on dept from zhangsan;撤銷授權(quán)
              在zhangsan下 可以使用select * from scott.dept;
                   看不到結(jié)果
             


          約束
             主鍵約束 --  每個(gè)表要有主鍵,唯一的標(biāo)識(shí)一行數(shù)據(jù)
             非空約束
             唯一性約束
             外鍵約束
             檢查約束
               腳本(SCRIPT)
                  create table cla( --班級(jí)表
                    id number(2) primary key, --班級(jí)編號(hào)
                    cname varchar2(20) not null --班級(jí)名字
                 );

                  create table stu( --學(xué)生表
                    xh number(4) primary key, --學(xué)號(hào)是主鍵
                    xm varchar2(20) not null, --姓名非空
                    age number(2) check (age between 10 and 90),--年齡在10到90之間(10<= age  <=90 )
                    birthday date,
                    shenfenzheng number(18) unique, --身份證唯一 
                    classid number(2) references cla(id) -- 班級(jí)編號(hào)外鍵
                     --(引用的一定是主鍵或唯一性約束的字段)
                   );
                 
                <1>建立表的同時(shí)使用約束
            create table student( --學(xué)生表
                     xh number(4) primary key, --學(xué)號(hào)主鍵
                     xm varchar2(10) not null, --姓名不能為空
                     sex char(2)  check (sex in ('男','女')), --性別
                     birthday date unique, --日期
                     sal number(7,2) check (sal between 500 and 1000),--獎(jiǎng)學(xué)金 sal >=500 and sal <=1000
               classid number(2) references cla(id)
                  );  --必須要先有cla表才對(duì)
                      --一定先建立班級(jí)cla表
           
             主鍵約束 primary key
                     not null
                     check
                     unique 唯一約束          

           create table student( --學(xué)生表
                     xh number(4) constraint pk_stu primary key, --學(xué)號(hào)主鍵
                     xm varchar2(10) constraint nn_stu not null, --姓名不能為空
                     sex char(2) constraint ck_stu_sex check (sex in ('男','女')), --性別
                     birthday date constraint uq_bir unique, --日期
                     sal number(7,2) constraint ck_sal check (sal between 500 and 1000)--獎(jiǎng)學(xué)金 sal >=500 and sal <=1000
                  );
                <2>建立約束的同時(shí)給約束指定名字,便于刪除
                  create table cla( --班級(jí)表
                    id number(2) constraint pk_cla primary key, --班級(jí)編號(hào)
                    cname varchar2(20) constraint nn_cla not null --班級(jí)名字
                 );
               
                create table stu( --學(xué)生表
                    xh number(4) constraint pk_stu primary key, --學(xué)號(hào)是主鍵
                    xm varchar2(20) constraint nn_stu not null, --姓名非空
                    age number(2) constraint ck_stu check (age between 10 and 90),--年齡在10到90之間(10<= age  <=90 )
                    birthday date,
                    shenfenzheng number(18) constraint uq_stu unique, --身份證唯一 
                    classid number(2) constraint fk_stu references cla(id) -- 班級(jí)編號(hào)外鍵
                     --(引用的一定是另外表的主鍵或唯一性約束的字段)
                   );
            
           
                <3>建完表后加約束
           
           學(xué)生表student
                  create table student( --學(xué)生表
                     xh number(4), --學(xué)號(hào)
                     xm varchar2(10), --姓名
                     sex char(2), --性別
                     birthday date, --日期
                     sal number(7,2) --獎(jiǎng)學(xué)金
                  );
           加約束
             加主鍵
              alter table student add constraint pk_stu
              primary key (xh);
             加非空
              alter table student modify (xm not null);
             檢查約束
              alter table student add check(sex in ('男','女'));
              alter table student add constraint ck_sal check(sal between 500 and 1000));


           給student加班級(jí)字段
             alter table student add (classid number(2));


             班級(jí)表cla
              create table cla( --班級(jí)表
                    id number(2), --班級(jí)編號(hào)
                    cname varchar2(20) --班級(jí)名字
                 );

          添加 主鍵
           alter table cla add constraint pk_cla
                 primary key (id);
          加 not null
           alter table cla modify
                 (cname not null);

                         學(xué)生表student
                create table student( --學(xué)生表
                    xh number(4) ,
                    xm varchar2(20) , --姓名非空
                    age number(2),--年齡在10到90之間(10<= age  <=90 )
                    birthday date,
                    shenfenzheng number(18), --身份證唯一 
                    classid number(2) -- 班級(jí)編號(hào)外鍵
                     --(引用的一定是另外表的主鍵或唯一性約束的字段)
                   );

          加外鍵約束
          alter table student add constraint fk_stu
              foreign key (classid) references cla(id);

          加主鍵
          alter table student add constraint pk_stu
           primary key (xh);

          加not null
          alter table student modify(xm not null);

          加檢查約束
          alter table student add constraint cc_age
           check (age >= 10 and age <=90);

          加唯一約束
            alter table student add constraint
                uq_sfz unique(shenfenzheng);

          加外鍵約束
           alter table student add constraint
              fk_stu foreign key (classid)
               references cla(id);

          如何刪除約束
            
            alter table student drop constraint
                  fk_stu;
          可以用一個(gè)統(tǒng)一的格式來(lái)刪除
            alter table 表名 drop constraint 約束名

            <4>如何查看約束?? 約束一定加在表上

              一個(gè)表上到底有哪些約束???
            select constraint_name,constraint_type
                from user_constraints
                  where table_name = 'STUDENT'
          --查看表上有什么約束
            select * from user_constraints;
          --查看約束作用在什么字段上
            select * from user_cons_columns
             where CONSTRAINT_NAME='PK_STU';

          user_constraints數(shù)據(jù)字典表


            <5>約束是如何起作用的??

                 create table cla( --班級(jí)表
                    id number(2) constraint pk_cla primary key, --班級(jí)編號(hào)
                    cname varchar2(20) constraint nn_cla not null --班級(jí)名字
                 );
               
                create table stu( --學(xué)生表
                    xh number(4) constraint pk_stu primary key, --學(xué)號(hào)是主鍵
                    xm varchar2(20) constraint nn_stu not null, --姓名非空
                    age number(2) constraint ck_stu check (age between 10 and 90),--年齡在10到90之間(10<= age  <=90 )
                    birthday date,
                    shenfenzheng number(18) constraint uq_stu unique, --身份證唯一 
                    classid number(2) constraint fk_stu references cla(id) -- 班級(jí)編號(hào)外鍵
                     --(引用的一定是另外表的主鍵或唯一性約束unique的字段)
                   );
            
             主鍵 = 非空 + 唯一
             非空
             唯一 = 有值的話  值要不同
                    null的話  都是可以的
             外鍵 = 有值 一定要在被引用的表的數(shù)據(jù)中
                    null的話  是可以的

          ANSI SQL92 數(shù)據(jù)庫(kù)的標(biāo)準(zhǔn)
           

           


            
            Oracle的約束

          * 如果某個(gè)約束只作用于單獨(dú)的字段,即可以在字段級(jí)定義約束,也可以在表級(jí)定義約束,但如果某個(gè)約束作用于多個(gè)字段,
          必須在表級(jí)定義約束
          * 在定義約束時(shí)可以通過(guò)CONSTRAINT關(guān)鍵字為約束命名,如果沒有指定,ORACLE將自動(dòng)為約束建立默認(rèn)的名稱

          定義primary key約束(單個(gè)字段)
          create table employees (empno number(5) primary key,...)

          指定約束名
          create table employees (empno number(5) constraint emp_pk primary key,...)

          定義primary key約束(多個(gè)字段,在表級(jí)定義約束)
          create table employees
          (empno number(5),
          deptno number(3) not null,
          constraint emp_pk primary key(empno,deptno)
          using index tablespace indx
          storage (initial 64K
          next 64K
          )
          )

          ORACLE自動(dòng)會(huì)為具有PRIMARY KEY約束的字段(主碼字段)建立一個(gè)唯一索引和一個(gè)NOT NULL約束,定義PRIMARY KEY約束時(shí)可以為它的索引
          指定存儲(chǔ)位置和存儲(chǔ)參數(shù)

          alter table employees add primary key (empno)
          alter table employees add constraint emp_pk primary key (empno)
          alter table employees add constraint emp_pk primary key (empno,deptno)

          not null約束(只能在字段級(jí)定義NOT NULL約束,在同一個(gè)表中可以定義多個(gè)NOT NULL約束)
          alter table employees modify deptno not null/null

          unique約束
          create table employees
          ( empno number(5),
          ename varchar2(15),
          phone varchar2(15),
          email varchar2(30) unique,
          deptno number(3) not null,
          constraint emp_ename_phone_uk unique (ename,phone)
          )

          alter table employees
          add constraint emp_uk unique(ename,phone)
          using index tablespace indx

          定義了UNIQUE約束的字段中不能包含重復(fù)值,可以為一個(gè)或多個(gè)字段定義UNIQUE約束,因此,UNIQUE即可以在字段級(jí)也可以在表級(jí)定義,
          在UNIQUED約束的字段上可以包含空值.

          foreign key約束

          * 定義為FOREIGN KEY約束的字段中只能包含相應(yīng)的其它表中的引用碼字段的值或者NULL值
          * 可以為一個(gè)或者多個(gè)字段的組合定義FOREIGN KEY約束
          * 定義了FOREIGN KEY約束的外部碼字段和相應(yīng)的引用碼字段可以存在于同一個(gè)表中,這種情況稱為"自引用"
          * 對(duì)同一個(gè)字段可以同時(shí)定義FOREIGN KEY約束和NOT NULL約束

          定義了FOREIGN KEY約束的字段稱為"外部碼字段",被FORGIEN KEY約束引用的字段稱為"引用碼字段",引用碼必須是主碼或唯一碼,包含外部碼的表稱為子表,
          包含引用碼的表稱為父表.

          A:
          create table employees
          (.....,
          deptno number(3) NOT NULL,
          constraint emp_deptno_fk foreign key (deptno)
          references dept (deptno)
          )

          如果子表中的外部碼與主表中的引用碼具有相同的名稱,可以寫成:
          B:
          create table employees
          (.....,
          deptno number(3) NOT NULL
          constraint emp_deptno_fk references dept
          )

          注意:
          上面的例子(B)中not null后面沒有加逗號(hào),因?yàn)檫@一句的contraint是跟在那一列deptno后面的,屬于列定義,所以都無(wú)需指明列。而A例中的是表定義,需要指明那一列,所以要加逗號(hào),不能在列后面定義,還可以寫成:

          create table employees
          (empno char(4),
          deptno char(2) not null constraint emp_deptno_fk references dept,
          ename varchar2(10)
          )
          表定義contraint的只能寫在最后,再看兩個(gè)例子:

          create table employees
          (empno number(5),
          ename varchar2(10),
          deptno char(2) not null constraint emp_deptno_fk references dept,
          constraint emp_pk primary key(empno,ename)
          )

          create table employees
          ( empno number(5),
          ename varchar2(15),
          phone varchar2(15),
          email varchar2(30) unique,
          deptno number(3) not null,
          constraint emp_pk primary key(empno,ename),
          constraint emp_phone_uk unique (phone)
          )

          添加foreign key約束(多字段/表級(jí))
          alter table employees
          add constraint emp_jobs_fk foreign key (job,deptno)
          references jobs (jobid,deptno)
          on delete cascade

          更改foreign key約束定義的引用行為(delete cascade/delete set null/delete no action),默認(rèn)是delete on action

          引用行為(當(dāng)主表中一條記錄被刪除時(shí),確定如何處理字表中的外部碼字段):
          delete cascade : 刪除子表中所有的相關(guān)記錄
          delete set null : 將所有相關(guān)記錄的外部碼字段值設(shè)置為NULL
          delete no action: 不做任何操作

          先刪除原來(lái)的外鍵約束,再添加約束
          ALTER TABLE employees DROP CONSTRAINT emp_deptno_fk;
          ALTER TABLE employees ADD CONSTRAINT emp_deptno_fk FOREIGN KEY(deptno) REFERENCES dept(deptno) ON DELETE CASCADE;

          check約束
          * 在CHECK約束的表達(dá)式中必須引用到表中的一個(gè)或多個(gè)字段,并且表達(dá)式的計(jì)算結(jié)果必須是一個(gè)布爾值
          * 可以在表級(jí)或字段級(jí)定義
          * 對(duì)同一個(gè)字段可以定義多個(gè)CHECK約束,同時(shí)也可以定義NOT NULL約束
           
          create table employees
          (sal number(7,2)
          constraint emp_sal_ck1 check (sal > 0)
          )

          alter table employees
          add constraint emp_sal_ck2 check (sal < 20000)

          刪除約束

          alter table dept drop unique (dname,loc) --指定約束的定義內(nèi)容
          alter table dept drop constraint dept_dname_loc_uk --指定約束名

          刪除約束時(shí),默認(rèn)將同時(shí)刪除約束所對(duì)應(yīng)的索引,如果要保留索引,用KEEP INDEX關(guān)鍵字
          alter table employees drop primary key keep index

          如果要?jiǎng)h除的約束正在被其它約束引用,通過(guò)ALTER TABLE..DROP語(yǔ)句中指定CASCADE關(guān)鍵字能夠同時(shí)刪除引用它的約束

          利用下面的語(yǔ)句在刪除DEPT表中的PRIMARY KEY約束時(shí),同時(shí)將刪除其它表中引用這個(gè)約束的FOREIGN KEY約束:
          alter table dept drop primary key cascade

          禁用/激活約束(禁用/激活約束會(huì)引起刪除和重建索引的操作)
          alter table employees disable/enable unique email
          alter table employees disable/enable constraint emp_ename_pk
          alter tabel employees modify constraint emp_pk disable/enable
          alter tabel employees modify constraint emp_ename_phone_uk disable/enable

          如果有FOREIGN KEY約束正在引用UNIQUE或PRIMARY KEY約束,則無(wú)法禁用這些UNIQUE或PRIMARY KEY約束,
          這時(shí)可以先禁用FOREIGN KEY約束,然后再禁用UNIQUE或PRIMARY KEY約束;或者可以在ALTER TABLE...DISABLE
          語(yǔ)句中指定CASCADE關(guān)鍵字,這樣將在禁用UNIQUE或PRIMARY KEY約束的同時(shí)禁用那些引用它們的FOREIGN KEY約束,如:
          alter table employees disable primary key cascade

          約束數(shù)據(jù)字典
          all_constraints/dba_constraints/user_constraints 約束的基本信息,包括約束的名稱,類型,狀態(tài)
          (約束類型:C(CHECK約束),P(主碼約束),R(外部碼約束),U(唯一碼約束))
          all_cons_columns/dba/user 約束對(duì)應(yīng)的字段信息
          Oracle的索引
              索引和對(duì)應(yīng)的表應(yīng)該位于不同的表空間中,oracle能夠并行讀取位于不同硬盤上的數(shù)據(jù),可以避免產(chǎn)生I/O沖突
          B樹索引:在B樹的葉節(jié)點(diǎn)中存儲(chǔ)索引字段的值與ROWID。
          唯一索引和不唯一索引都只是針對(duì)B樹索引而言.
          Oracle最多允許包含32個(gè)字段的復(fù)合索引

          索引創(chuàng)建策略
          1.導(dǎo)入數(shù)據(jù)后再創(chuàng)建索引
          2.不需要為很小的表創(chuàng)建索引
          3.對(duì)于取值范圍很小的字段(比如性別字段)應(yīng)當(dāng)建立位圖索引
          4.限制表中的索引的數(shù)目
          5.為索引設(shè)置合適的PCTFREE值
          6.存儲(chǔ)索引的表空間最好單獨(dú)設(shè)定

          創(chuàng)建不唯一索引
          create index emp_ename on employees(ename)
          tablespace users
          storage(......)
          pctfree 0;

          創(chuàng)建唯一索引
          create unique index emp_email on employees(email)
          tablespace users;

          創(chuàng)建位圖索引
          create bitmap index emp_sex on employees(sex)
          tablespace users;

          創(chuàng)建反序索引
          create unique index order_reinx on orders(order_num,order_date)
          tablespace users
          reverse;

          創(chuàng)建函數(shù)索引(函數(shù)索引即可以是普通的B樹索引,也可以是位圖索引)
          create index emp_substr_empno
          on employees(substr(empno,1,2))
          tablespace users;

          修改索引存儲(chǔ)參數(shù)(與表類似,INITIAL和MINEXTENTS參數(shù)在索引建立以后不能再改變)
          alter index emp_ename storage(pctincrease 50);

          由于定義約束時(shí)由oracle自動(dòng)建立的索引通常是不知道名稱的,對(duì)這類索引的修改經(jīng)常是利用alter table ..using index語(yǔ)句進(jìn)行的,而不是alter index語(yǔ)句

          利用下面的語(yǔ)句將employees表中primary key約束對(duì)應(yīng)的索引的PCTFREE參數(shù)修改為5
          alter table employees enable primary key using index pctfree 5;

          清理索引碎片
          1.合并索引(只是簡(jiǎn)單的將B樹葉結(jié)點(diǎn)中的存儲(chǔ)碎片合并在一起,并不會(huì)改變索引的物理組織結(jié)構(gòu))
          alter index emp_pk coalesce;

          2.重建索引(不僅能夠消除存儲(chǔ)碎片,還可以改變索引的全部存儲(chǔ)參數(shù)設(shè)置,并且可以將索引移動(dòng)到其它的表空間中,重建索引
          實(shí)際上就是再指定的表空間中重新建立一個(gè)新的索引,然后刪除原來(lái)的索引)
          alter index emp_pk rebuild;

          刪除索引
          drop index emp_ename;

          如果索引中包含損壞的數(shù)據(jù)塊,或者包含過(guò)多的存儲(chǔ)碎片,需要首先刪除這個(gè)索引,然后再重建它.
          如果索引是在創(chuàng)建約束時(shí)由oracle自動(dòng)產(chǎn)生的,可以通過(guò)禁用約束或刪除約束的方法來(lái)刪除對(duì)應(yīng)的索引.
          在刪除一個(gè)表時(shí),oracle會(huì)自動(dòng)刪除所有與該表相關(guān)的索引.

          索引數(shù)據(jù)字典
          all_indexes/dba_indexes/user_indexes 索引的基本信息
          all_ind_columns/dba_ind_columns/user_ind_columns 索引對(duì)應(yīng)的字段信息
              
           

          posted @ 2008-06-12 10:30 飛飛 閱讀(416) | 評(píng)論 (0)編輯 收藏

          課程
            java語(yǔ)言  java基本編程 -> j2EE編程
            數(shù)據(jù)庫(kù)的能力 sqlserver -> ORACLE
            數(shù)據(jù)結(jié)構(gòu)    訓(xùn)練編程思維方式 實(shí)際問題到計(jì)算機(jī)問題的轉(zhuǎn)化
            linux      熟悉一種新的操作系統(tǒng)

            軟件工廠    學(xué)到的知識(shí) 去做一個(gè)項(xiàng)目

            課程安排
            linux操作系統(tǒng) 4天
            數(shù)據(jù)結(jié)構(gòu)    5天
            DOTNET介紹      4天

            ORACLE
              ORACLE數(shù)據(jù)庫(kù)  10天
              XML語(yǔ)言       5天
            J2EE 
              JSP/servlet   12天
              EJB           3天
              struts        4天


            軟件工廠        2周


          找工作的技能
              j2EE   +  XML   + ORACLE/SQLSERVER
          基礎(chǔ) java    html javascript   sql


          ORACLE數(shù)據(jù)庫(kù)
             甲骨文   英文中 意思是神寓 神說(shuō)的話
                復(fù)雜性
                公司野心
          1973年
              i  internet
              g  grid (網(wǎng)格)
             
              ERP  企業(yè)資源計(jì)劃 Enterprise Resource Plan
              MIS  管理信息系統(tǒng) Management Information System

              德國(guó) SAP
              ORACLE application 11i
             

          別的數(shù)據(jù)庫(kù)
             DB2  IBM公司  IBM產(chǎn)品綁定緊密 (windows unix/linux)
             sybase  sybase公司  電信和銀行 (windows unix/linux)
             sqlserver          windows平臺(tái) 
            
          小型
             access     office套件
             mysql      PHP(Personal HomePage)(linux) +
                         mysql(不支持存儲(chǔ)過(guò)程,觸發(fā)器等)

                

          學(xué)習(xí)方法
             動(dòng)手能力
             做會(huì)的  不是書本學(xué)會(huì)的
            
             找問題的答案:
               問同學(xué) 問老師
               從網(wǎng)上 找答案
                 搜索引擎  www.google.com
                          百度
                   錯(cuò)誤號(hào)  錯(cuò)誤文字放入關(guān)鍵字中
                   提取關(guān)鍵字 多實(shí)驗(yàn)
                    ORACLE安裝
                    ORACLE  +“安裝”

          ORACLE安裝和刪除
             ORACLE9i  9.0.1  空間1.76G
             ORACLE9i  9.0.2  空間2.86G
              事務(wù)處理 --  增 刪除 修改頻繁
              數(shù)據(jù)倉(cāng)庫(kù) -- 查詢 頻繁

              GBK    --多200個(gè)字左右(偏僻字)
              GB2312 
           
           SETUP.exe 雙擊


           啟動(dòng)ORACLE數(shù)據(jù)庫(kù)  OracleServiceACCP  服務(wù)  多啟動(dòng)幾次
                            OracleOraHome90TNSListener 服務(wù)
              如果啟動(dòng)不了Listener(監(jiān)聽器)
               <1>改注冊(cè)表 
                     ImagePath  D:\oracle\ora90\BIN\TNSLSNR
               <2>用命令行啟動(dòng)
                      cmd  ->   tnslsnr 最小化
                      tNSlsnter  監(jiān)聽器  如果無(wú)法啟動(dòng)
                      運(yùn)行cmd 切換到DOS界面 敲tnslsnr 窗口不要關(guān)閉
               <3>D:\oracle\ora90\network\ADMIN
                          listener.ora 文本文件
                       HOST=localhost或機(jī)器名
                        存盤
           
           
                    用工具 ORACLE SQL*PLUS驗(yàn)證數(shù)據(jù)庫(kù)是否啟動(dòng)
                      用戶名system   密碼manager
                      出現(xiàn) 連接到 和 SQL>符號(hào) 說(shuō)明數(shù)據(jù)庫(kù)已經(jīng)可用了

                  默認(rèn)用戶:
                    超級(jí)用戶: 用戶名system/密碼manager
                             用戶名sys/密碼change_on_install

                    普通的用戶: 用戶名scott/密碼tiger


           注意的問題:安裝的源程序放在什么目錄下??
                  <1>目錄中不要出現(xiàn)中文名字的目錄
                  <2>數(shù)據(jù)庫(kù)的名字不要是中文,8個(gè)字符以內(nèi)
                  <3>字符集用默認(rèn)的ZHS16GBK

          刪除:
             在硬盤上的一個(gè)隱含安裝的目錄Programm Files下的ORACLE  

          操作ORACLE數(shù)據(jù)庫(kù)的工具

             ORACLE SQL*PLUS 字符界面 ORACLE自帶
               sqlplusw.exe windows窗口下的sqlplus
               sqlplus.exe   DOS下的sqlplus          
                  
                 用戶名scott 密碼tiger 普通用戶
                       system    manager   超級(jí)用戶
                       sys       change_on_install  超級(jí)用戶 比system權(quán)限還要大

               SQLPLUS下的常用命令
                 show user 察看用戶
                 connect 用戶名/密碼  連接到指定用戶
                      connect scott/tiger   
                  select * from tab where tabtype='TABLE';  察看當(dāng)前用戶下的表      
                 select count(*) from dept;  查詢表dept中記錄的行數(shù)
                 desc dept             察看表dept的結(jié)構(gòu)
                 quit/exit             退出
                 clear screen          清除屏幕
                 set linesize 200      設(shè)置一行顯示200個(gè)字符
                 set pagesize 20       設(shè)置每頁(yè)顯示20行

                 spool 文件名 (spool c:\abc.txt) 作日志文件命令        
                 spool off
                 
                 修改D:\oracle\ora90\sqlplus\admin\glogin.sql文件
           可以設(shè)置SQLPLUS的環(huán)境
                     set linesize 200
                     set pagesize 20  

             
                    

             ORACLE 企業(yè)管理器 (OEM)  圖形界面

             PL/SQL Developer 第三方軟件 ORACLE不帶的

            SQLPLUS  /nolog    nolog是不登陸

            瀏覽器使用oracle (isqlplus)
               D:\oracle\ora90\Apache\Apache\conf\httpd.conf
                     修改80端口   -> 8001
              http://localhost:8001/isqlplus

           

          ORACLE中字段的數(shù)據(jù)類型
             字符型    char        范圍  最大2000個(gè)字節(jié) 定長(zhǎng)
                         char(10)   '張三' 后添空格6個(gè)把10個(gè)字節(jié)補(bǔ)滿  '張三      '
                           性別 char(2)   '男'
                          
                        varchar2    范圍  最大4000個(gè)字節(jié) 變長(zhǎng)
                           varchar2(10)  '張三'      在數(shù)據(jù)庫(kù)中'張三'
                          
                       大對(duì)象 字符型大對(duì)象 >4000字節(jié) 最大4G
                         CLOB (Character Large OBject)    
              
             數(shù)字     number     范圍  10的-38次方 到10的38次方          
                        可以表示小數(shù) 也可以表示整數(shù)  
                       number(4)   最大表示4位整數(shù)  -9999 到 9999
                       number(5,2)   表示5位有效數(shù)字 2位小數(shù)的 一個(gè)小數(shù)  -999.99 到 999.99
                            
                      
             日期     date      包含年月日和時(shí)分秒  7個(gè)字節(jié)
             圖片     blob      二進(jìn)制大對(duì)象    圖像/聲音  4G

          如何建表
             學(xué)生表student
                  create table student( --學(xué)生表
                     xh number(4), --學(xué)號(hào)
                     xm varchar2(10), --姓名
                     sex char(2), --性別
                     birthday date, --日期
                     sal number(7,2) --獎(jiǎng)學(xué)金
                  );
             班級(jí)class
                 create table class( --班級(jí)表
                    classid number(2), --班級(jí)編號(hào)
                    cname varchar2(20) --班級(jí)名字
                 );
            添加字段(學(xué)生所在班級(jí)classid)
                alter table student add (classid number(2));
            修改字段的長(zhǎng)度
                alter table student modify (xm varchar2(12)) ;
            修改字段的類型(不能有記錄的)
                alter table student modify (xh varchar2(5));
            刪除一個(gè)字段
                alter table student drop column sal;
            刪除表
                drop table student;
            表的名字修改
                rename student to stu;
            字段如何改名字
                --先刪除
                a)alter table student drop column sal;        
                --再添加
                b)alter table student add (salary number(7,2));

          如何插入數(shù)據(jù)
            插入數(shù)據(jù) insert語(yǔ)句
                所有字段都插入
                 insert into student values ('A001','張三','男','01-5月-05',10);
                      ORACLE中默認(rèn)的日期格式'DD-MON-YY'   dd 日子(天)  mon 月份 yy 2位的年
                       '09-6月-99' 1999年6月9號(hào)
                      改日期的默認(rèn)格式
                           alter session set nls_date_format = 'yyyy-mm-dd';
              
                 insert into student values ('A002','MIKE','男','1905-05-06',10);

                     恢復(fù)ORACLE默認(rèn)格式
                         alter session set nls_date_format = 'dd-mon-yy';
                     察看日期的格式
                         set linesize 1000
                         select * from nls_session_parameters
                           where parameter='NLS_DATE_FORMAT';
                     永久設(shè)置日期格式
                         改注冊(cè)表oracle/HOME0 加字符串NLS_DATE_FORMAT 值yyyy-mm-dd
               部分字段插入
                 insert into student(xh,xm,sex) values ('A003','JOHN','女');
               插入空值
                 insert into student(xh,xm,sex,birthday) values ('A004','MARTIN','男',null);

            修改  update
                改一個(gè)字段 
                 update student set sex='女' where xh='A001';
                改多個(gè)字段
                 update student set sex='男',
                                    birthday='1980-04-01'
                        where xh='A001';
                改為空值 (修改為空時(shí)=null)
                 update student set birthday=null where xh='A001';
                把生日為空的人的班級(jí)編號(hào)改為20(條件中的空是is null / is not null)
                    update student set classid=20 where birthday is null;
                   錯(cuò)誤的沒有達(dá)到要求
                   update student set classid=20
                     where birthday=null;
                   不表示空值 表示xm是null的字符串        
                   update student set classid=20 where xm='null';
           
             
            刪除 delete
                delete from student;  刪除所有記錄,表結(jié)構(gòu)還在,寫日志,可以恢復(fù)的,速度慢
                drop table student;  刪除表的結(jié)構(gòu)和數(shù)據(jù)
                delete from student where xh='A001';  刪除一條記錄

                truncate table student; 刪除表中的所有記錄,表結(jié)構(gòu)還在,不寫日志,無(wú)法找回刪除的記錄,速度快

            查詢 select   
                select * from student;

                select xh,xm,sex from student;    

                select * from student where xh like 'A%1'; %任意多個(gè)字符
                select * from student where xh like 'A__1'; _1個(gè)字符
                select * from student where xh like '%A%';             select * from student where xh like 'A%';
          select * from student where xh like '%A';                              

                select * from student where xh = 'A%';
                                 
            
                select * from student
                order by birthday ;  升序 (order by birthday asc;)
           
                select * from student
                order by birthday desc; --降序
            
                select * from student
                order by birthday desc,xh asc; --按birthday 降序 按xh升序(asc/默認(rèn))
                          
                select * from student
                where sex='女' or birthday='1999-02-01';

                select * from student
                where sex='女' and birthday='1999-02-01';

                select * from student
                 where salary > 20 and xh <> 'B002'; (!=)

          ORALCE的函數(shù)
             單行函數(shù)   返回值只有一個(gè)
               
             分組函數(shù)   返回值是多條記錄
                group by
                sum
                avg           
               
          單行函數(shù)
            字符函數(shù)
              concat 連接  ||
             <1>顯示dname和loc中間用-分隔
               select deptno,dname||'----'||loc from dept;
              
               dual啞元表   沒有表需要查詢的時(shí)候 可以用它
                   select 'Hello World' from dual;
                   select 1+1 from dual;
                   查詢系統(tǒng)時(shí)間
                    select sysdate from dual;
             <2>  initcap 首字母大寫
                 select ename,initcap(ename) from emp;
             <3>  lower   轉(zhuǎn)換為小寫字符
                  select ename,lower(ename) from emp;
             <4> upper 轉(zhuǎn)換為大寫
                  update dept set loc=lower(loc);
                  update dept set loc=upper(loc);
             <5> LPAD 左填充
                  select deptno,lpad(dname,10,' '),loc from dept;
             <6> RPAD 右填充
             <7> LTRIM 去除左邊的空格
                 RTRIM 去除右邊的空格
                 ALLTRIM  去除兩邊的空格
             <8>replace    替換
                translate  轉(zhuǎn)換
                 select ename,replace(ename,'S','s') from emp;
                     用's'去替換ename中的'S'
                  select ename,translate(ename,'S','a') from emp;
             <9> ASCII 求ASC碼
                 chr   asc碼變字符
                   select ascii('A') from dual;
                   select chr(97) from dual;
                   select 'Hello'||chr(9)||'World' from dual;
                                     '\t' ascii碼是 9
                                     '\n' ascii碼是 10

                   select 'Hello'||'\t'||'World' from dual;    

             <10> substr 字符截取函數(shù)
                     select ename,substr(ename,1,3) from emp;
                         從第1個(gè)位置開始 顯示3個(gè)字符
                     select ename,substr(ename,4) from emp;
                        從第4個(gè)位置開始顯示后面所有的字符
             <11> instr 測(cè)試字符串出現(xiàn)的位置
                    select ename,instr(ename,'S') from emp;
                       'S'第1次出現(xiàn)的位置
            select ename,instr(ename,'T',1,2) from emp;                
                       從第1個(gè)位置開始 測(cè)試'T'第2次出現(xiàn)的位置 
            <12> length 字符串的長(zhǎng)度
                  select ename,length(ename) from emp;
                
          日期和 時(shí)間函數(shù)
             <1> sysdate 系統(tǒng)時(shí)間
                  select sysdate from dual;
                  select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;
                  select to_char(sysdate,'DDD') from dual  
                select to_char(sysdate,'D') from dual
                select to_char(sysdate,'DAY') from dual

                select to_char(sysdate,'yyyy-mm-dd') from dual;

            select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh24:mi:ss') from dual;
           
           select '''' from dual;

          select to_char(sysdate,'SSSSS') from dual;
            --從今天零點(diǎn)以后的秒數(shù)

             <2> ADD_MONTHS 添加月份 得到一個(gè)新的日期
                  select add_months(sysdate,1) from dual;

                 select add_months(sysdate,-1) from dual;

                 select trunc(sysdate)-to_date('20050101','yyyymmdd') from dual;
                 select add_months(sysdate,12) from dual;
                     一年以后的今天
          select add_months(sysdate,-12) from dual;
                     一年以前的今天

               trunc(sysdate) 截取年月日
           
           select sysdate+2 from dual;
                  數(shù)字代表的是天數(shù)

               兩個(gè)日期之間的差值代表天數(shù)

             <3> last_day  某月的最后一天
                 select last_day(sysdate) from dual;
                
                 select add_months(last_day(sysdate)+3,-1) from dual;
                      本月第3天的日期
            <4>  months_between 兩個(gè)日期之間的月數(shù)
                   select months_between(sysdate,'2005-02-01') from dual;
                           方向 sysdate - '2005-02-01'
                select months_between('2005-02-01',sysdate) from dual;
              
           轉(zhuǎn)換函數(shù)
             to_char   把日期或數(shù)字類型變?yōu)樽址?br />        select to_char(sysdate,'hh24:mi:ss') from dual;
                 select to_char(sysdate,'yyyymmdd hh24:mi:ss') from dual;

                 select sal,to_char(sal,'L9,999') from emp;
                       L本地貨幣
            to_number   把字符串變成數(shù)字
                select to_number('19990801') from dual;
           
            to_date     把字符串變成日期
                select to_date('19800101','yyyymmdd') from dual;

                select to_char(to_date('19800101','yyyymmdd'),
                  'yyyy"年"mm"月"dd"日"') from dual;     
           數(shù)學(xué)函數(shù)
              ceil(x)  不小于x的最小整數(shù)
                  ceil(12.4)   13
                  ceil(-12.4)   -12
              floor(x)  不大于x的最大整數(shù)
                  floor(12.5)  12
                  floor(-12.4)  -13


             round(x)  四舍五入
               round(12.5)   13
               round(12.456,2) 12.46

             trunc(x)  舍去尾數(shù)
               trunc(12.5)  12
               trunc(12.456,2)  12.45
              舍去日期的小時(shí)部分
               select to_char(trunc(sysdate),'yyyymmdd hh24:mi:ss') from dual;
             mod(x,n)  x除以n以后的余數(shù)
               mod(5,2) 1
               mod(4,2) 0

             power(x,y)  x的y次方
              select power(3,3) from dual;
              
           
          混合函數(shù)
                求最大值
             select greatest(100,90,80,101,01,19) from dual;
            
                求最小值
             select least(100,0,-9,10) from dual;

                空值轉(zhuǎn)換函數(shù) nvl(comm,0) 字段為空值 那么就返回0 否則返回本身
             select comm,nvl(comm,0) from emp;
                comm 類型和 值的類型是 一致的
            
          復(fù)雜的函數(shù)
             decode   選擇結(jié)構(gòu)  (if ... elseif .... elesif ... else結(jié)構(gòu))
             
          要求:
             sal=800  顯示低工資  
             sal=3000  正常工資
             sal=5000  高工資
              只能做等值比較

            select sal,decode(sal,800,'低工資',3000,'正常工資',5000,'高工資','沒判斷')
            from emp;
           表示如下的if  else 結(jié)構(gòu)
               if sal=800 then
                  '低工資'
               else if sal =3000 then
                  '正常工資'
               else if  sal = 5000 then
                   '高工資'
               else
                  '沒判斷'
               end if
                   

             sal > 800           sal -800 > 0  

           判斷正負(fù)
            sign(x)   x是正  1
                      x是負(fù)  -1
                      x是0   0
              select sign(-5) from  dual;


           如何做大于小于的比較????
            sal<1000  顯示低工資   sal-1000<0   sign(sal-1000) = -1
             1000<=sal<=3000  正常工資
             3000<sal<=5000  高工資

             select sal,decode(
                       sign(sal-1000),-1,'低工資',
                      decode(sign(sal-3000),-1,'正常工資',
                                      0,'正常工資',1,
                      decode(sign(sal-5000),-1,'高工資','高工資')
                       )) as 工資狀態(tài) from emp;
               
            一般的情況  decode(x,y1,z1,y2,z2,z3)
                if x= y1 then
                    z1         
                else if x = y2 then
                    z2
                else
                   z3
                end if 

          分組函數(shù)   返回值是多條記錄 或計(jì)算后的結(jié)果
                group by
                sum
                avg

          <1>  計(jì)算記錄的條數(shù) count

            select count(*) from emp;
            select count(1) from emp;


            select count(comm) from emp; 字段上count 會(huì)忽略空值
                comm不為空值的記錄的條數(shù)

            統(tǒng)計(jì)emp表中不同工作的個(gè)數(shù) ????
             select count(distinct job) from emp;

             select distinct job from emp;
             select distinct job,empno from emp;
             select job,empno from emp;
              得到的效果是一樣的,distinct 是消去重復(fù)行
                                 不是消去重復(fù)的列
           <2>group  by 分組統(tǒng)計(jì)
               --在沒有分組函數(shù)的時(shí)候
               --相當(dāng)于distinct 的功能
               select job from emp group by job;

               select distinct job from emp;

             --有分組函數(shù)的時(shí)候
             --分組統(tǒng)計(jì)的功能
             統(tǒng)計(jì)每種工作的工資總額是多少??
               select job,sum(sal) from emp
                    group by job;       --行之間的數(shù)據(jù)相加

               select sum(sal) from emp;  --公司的工資總額


           統(tǒng)計(jì)每種工作的平均工資是多少?? 
               select job,avg(sal) from emp
                    group by job;   

              select avg(saL) from emp; --整個(gè)公司的平均工資


           顯示平均工資>2000的工作???
             <1>統(tǒng)計(jì)每種工作的平均工資是多少
             <2>塞選出平均工資>2000的工作     

              從分組的結(jié)果中篩選 having
             select job,avg(sal) from emp
                    group by job
                    having avg(sal) > 2000;
             group by 經(jīng)常和having搭配來(lái)篩選

          計(jì)算工資在2000以上的各種工作的平均工資????
            select job,avg(sal) from emp
                    where sal > 2000
                    group by job
                    having avg(sal) > 3000;

              一般group by  和 having搭配
                  表示對(duì)分組后的結(jié)果的篩選
               where子句 --- 用于對(duì)表中數(shù)據(jù)的篩選  
           
            <3> max min
             select max(sal) from emp;
               公司的最高工資
             select min(sal) from emp ;
               公司的最低工資

          找每個(gè)部門的最高和最低的工資??
            select deptno,max(sal),min(sal) from emp
               group by deptno;
          找每個(gè)工作的最高和最低的工資??
            select job,max(sal),min(sal) from emp
               group by job;
          找每個(gè)部門中每種工作的最高和最低的工資??
            select deptno,job,max(sal),min(sal)
             from emp
             group by deptno,job;

           select max(sal),min(sal)
             from emp
             group by deptno,job;

              單個(gè)字段如果沒有被分組函數(shù)所包含,
                 而其他字段又是分組函數(shù)的話     
                一定要把這個(gè)字段放到group by中去

           <4>關(guān)聯(lián)查詢
                多張表,而表與表之間是有聯(lián)系的

                 是通過(guò)字段中的數(shù)據(jù)的內(nèi)在聯(lián)系來(lái)發(fā)生
                 而不是靠相同的字段名來(lái)聯(lián)系的或者是否有主外鍵的聯(lián)系是沒有關(guān)系的
                select dname,ename from emp,dept;
                   笛卡爾積  (無(wú)意義的)
                --當(dāng)2個(gè)表作關(guān)聯(lián)查詢的時(shí)候一定要寫關(guān)聯(lián)的條件
                --N個(gè)表 關(guān)聯(lián)條件一定有N-1個(gè)

                select dname,ename from mydept,myemp
                 where mydept.no = myemp.deptno;


                多表查詢的時(shí)候一定要有關(guān)聯(lián)的條件
                  --使用的表的全名
                  select dname,ename from emp,dept
                   where emp.deptno = dept.deptno ;
                 
                  --使用表的別名
                   select dname,ename,a.deptno from emp a,dept b
                   where a.deptno = b.deptno and a.deptno = 10;

                 --等值連接(內(nèi)連接-兩個(gè)表的數(shù)據(jù)作匹配a.deptno = b.deptno )
                   select dname,ename,a.deptno from
                   emp a inner join dept b
                   on a.deptno = b.deptno;
                   where a.deptno = 10;

                 --on寫連接條件的
                 --where中寫別的條件

                 --使用where/on
                   select dname,ename,a.deptno from emp a,dept b
                   where a.deptno = b.deptno and a.deptno=10;
                  
                    --on中寫連接條件
                    --where中寫其他的條件
                    select dname,ename,a.deptno from
                   emp a inner join dept b
                   on a.deptno = b.deptno
                   where a.deptno = 10 ;

                 --外連接
                   左外連接 右外連接  全外連接
                   (+)寫法只有在ORACLE中有效
                  select dname,ename,b.deptno
                  from emp a,dept b
                  where a.deptno(+) = b.deptno;
                 --標(biāo)準(zhǔn)寫法
                   select dname,ename,b.deptno
                  from emp a right outer join dept b
                  on a.deptno = b.deptno;        


                  select dname,ename,b.deptno
                  from emp a,dept b
                  where a.deptno = b.deptno(+);
              --標(biāo)準(zhǔn)寫法
                   select dname,ename,b.deptno
                  from emp a left outer join dept b
                  on a.deptno = b.deptno;        

              --標(biāo)準(zhǔn)寫法(全外聯(lián)) 
                     select dname,ename,b.deptno
                  from emp a full outer join dept b
                  on a.deptno = b.deptno;    
            
              --自連接
                  select a.ename as 員工姓名,b.ename as 經(jīng)理名字 from emp a,emp b
                  where a.mgr = b.empno(+);
                        a.empno = b.mgr  ???????
           
           <5>子查詢
              在select語(yǔ)句中嵌套了另一個(gè)select語(yǔ)句
               1)where 子句中嵌套子查詢
               2)用子查詢的結(jié)果 作為字段來(lái)出現(xiàn)

          --1)where 子句中嵌套子查詢,執(zhí)行順序是
                先執(zhí)行子查詢 再執(zhí)行主查詢
            找出工資高于公司平均工資的所有員工??
             select * from emp where sal+nvl(comm,0) > (select avg(sal+nvl(comm,0)) from emp);

             高于部門30中員工最高工資的其他員工???
              
            select * from emp where  sal+nvl(comm,0) > all (select sal+nvl(comm,0) from emp
                         where deptno = 30);

             低于部門30中員工工資的其他員工???
            select * from emp where  sal+nvl(comm,0) < all (select sal+nvl(comm,0) from emp
                         where deptno = 30);

            select * from emp where  sal+nvl(comm,0) < any (select sal+nvl(comm,0) from emp
                         where deptno = 30);


          --2)用子查詢的結(jié)果 作為字段來(lái)出現(xiàn) 
                先執(zhí)行主查詢 再執(zhí)行子查詢

            <1>找員工姓名和直接上級(jí)的名字
             select ename as 員工姓名,(select ename from emp where empno = a.mgr) as 經(jīng)理姓名
             from emp a;      
           <2>顯示部門名稱和人數(shù)
            select dname,(select count(*) from emp where deptno=a.deptno) as rs from dept a;
           <3>顯示每個(gè)部門的最高工資的員工
              select * from emp a where (deptno, sal) in  (  select deptno,max(sal) from emp group by deptno);
           
           select a.* from emp a,(  select deptno,max(sal) as msal from emp group by deptno) c where a.deptno = c.deptno and
          a.sal = c.msal;

          --最大值和最小值的比較 轉(zhuǎn)化為人數(shù)的比較
          select * from emp a where (select count(*) from
           emp where deptno = a.deptno and
           sal > a.sal) = 0 and a.deptno is not null;

           <4>顯示每個(gè)部門的工資前2名的員工
          select * from emp a where (select count(*) from
           emp where deptno = a.deptno and
           sal > a.sal) <=1 and a.deptno is not null;

          <6> 層次查詢
          --level 偽列 層次查詢的時(shí)候可以使用的  層的編號(hào)

            select lpad('+',level,' ')||ename from emp
               connect by prior empno = mgr --父子關(guān)系 父結(jié)點(diǎn)中的empno = 子節(jié)點(diǎn)中的mgr
               start with mgr is null;--從 mgr is null的節(jié)點(diǎn) 開始遍歷

          select lpad('+',level,' ')||ename from emp
               connect by prior empno = mgr
               start with ename = 'BLAKE';

          <7> TOP 前幾行 (sqlserver)
              rownum  (oracle偽列)

           --emp表的前2行
           --rownum必須使用<=的關(guān)系比較運(yùn)算符

           select * from emp where rownum <= 2;

           select top 2 * from emp; --sqlserver的寫法

           select * from emp where rownum = 1;

           --公司工資最高的2個(gè)人
           /*select * from emp
           where rownum <= 2
          order by sal desc;*/ 錯(cuò)誤的

             select * from (select * from emp order by sal desc)
             where rownum <= 2;

           --分頁(yè)查詢
            --每頁(yè)顯示4行 一共有14行記錄

            第1頁(yè)    第1-4行
            第2頁(yè)    第5-8行
            第3頁(yè)    第9-12行
            第4頁(yè)    第13-14行

           --希望每次頁(yè)面顯示的時(shí)候 都只查詢回需要的記錄
           
             select * from (select rownum as num,emp.* from emp)
             where num >= 9 and num <= 12;

             select * from (select rownum as num,emp.* from emp)
             where num >= 13 and num <= 14;


          <例子>
          建立表如下:

          學(xué)生基本信息表
          CREATE Student(
          [Studentid][Int]IDENTITY(1,1)NOT NULL primary key,--主鍵
          [StudentName][char]NOT NULL

          )
          課程信息表
          CREATE Subject(
          [SubjectID][char]NOT NULL primary key,       --主鍵
          [SubjectName][char]NOT NULL
          )
          成績(jī)表
          CREATE Grade(
          [Studentid][Int]NOT NULL,  --聯(lián)合主鍵
          [SubjectID][char]NOT NULL,  --聯(lián)合主鍵
          [Grade] [INT]NOT NULL,
          primary key (studentid,subjectid)
          )

          1.將建表命令改為ORACLE語(yǔ)句在ORACLE中建表
          create table student( --學(xué)生表
          studentid number(3) primary key, --學(xué)生編號(hào)
          studentname varchar2(20) --學(xué)生的姓名
          );

          create table subject( --課程表
          subjectid char(3) primary key, --課程編號(hào)
          subjectname varchar2(20)  --課程的名字
          );


          create table grade( --分?jǐn)?shù)表
          studentid number(3) references student(studentid), --學(xué)生id
          subjectid char(3) references subject(subjectid), --課程id
          mark      number(3), --分?jǐn)?shù)
          primary key (studentid,subjectid) --聯(lián)合主鍵
          );

           

          insert into student values (101,'張三');
          insert into student values (102,'李云');
          insert into student values (103,'未');

          insert into subject values ('A01','C++');
          insert into subject values ('A02','ASP');
          insert into subject values ('A03','JAVA');


          insert into grade values (101,'A01',59);
          insert into grade values (101,'A02',72);
          insert into grade values (101,'A03',90);

          insert into grade values (102,'A01',75);
          insert into grade values (102,'A02',91);

          insert into grade values (103,'A01',71);

           


          2.作如下4題

          第一問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 課程名稱 成績(jī) (要全部學(xué)生信息)

          關(guān)聯(lián)查詢 (多張表的)
          別名

          select a.studentid as "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a , subject b , grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid;

          [select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a , subject b , grade c] 笛卡爾積

                 3 * 3 * 6 = 54;


          第二問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 課程名稱 成績(jī)(只顯示每科最高分)

          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          and (subjectname,mark)
          in (select subjectname "課程名稱",max(mark) "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          group by subjectname)

          (最高分---分?jǐn)?shù)比我高的學(xué)生的人數(shù)=0)
          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",mark "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          and (select count(*) from grade
          where subjectid = b.subjectid and
          mark > c.mark) = 0


          第三問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 課程名稱 成績(jī) (成績(jī)大于60時(shí)的顯示及格,小于60時(shí)的顯示不及格)

          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          subjectname "課程名稱",
          decode(sign(mark-60),-1,'不及格','及格') "成  績(jī)"
          from student a,subject b,grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid

          第四問:查詢出以下信息

          學(xué)號(hào) 學(xué)生姓名 (查詢出課程超過(guò)1門以上學(xué)生的信息)

          select a.studentid "學(xué)  號(hào)",studentname "學(xué)生姓名",
          count(subjectname)
          from student a , subject b , grade c
          where a.studentid = c.studentid
          and b.subjectid = c.subjectid
          group by a.studentid,studentname
          having count(subjectname) >= 2

           

           

           

           

           

           

           

           

           

           

           

           


          -- select * from emp where rownum > 2;   錯(cuò)誤的

           

           

           

           

           

           

           


              

          posted @ 2008-06-12 10:29 飛飛 閱讀(1301) | 評(píng)論 (3)編輯 收藏

          set pagesize 0
          set long 90000
          set feedback off
          set echo off 
          spool get_allddl.sql 
          connect  USERNAME/PASSWORD@SID;
          SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
          FROM USER_TABLES u;
          SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name)
          FROM USER_INDEXES u;
          spool off;


          注:本方法僅限oracle9i以后版本
          posted @ 2008-06-12 10:28 飛飛 閱讀(1484) | 評(píng)論 (0)編輯 收藏

          僅列出標(biāo)題
          共12頁(yè): First 上一頁(yè) 4 5 6 7 8 9 10 11 12 
          主站蜘蛛池模板: 灯塔市| 岳普湖县| 山西省| 泰顺县| 绥德县| 南丹县| 西乡县| 密云县| 左贡县| 扶风县| 武城县| 青阳县| 神池县| 时尚| 乌鲁木齐县| 永川市| 法库县| 都兰县| 高青县| 伊宁市| 醴陵市| 太谷县| 乐至县| 肥乡县| 贵港市| 横山县| 平泉县| 盐山县| 东山县| 广安市| 香格里拉县| 新平| 开鲁县| 乌苏市| 大城县| 泾川县| 雅安市| 无棣县| 滦南县| 离岛区| 通城县|