Oracle學習筆記摘錄1-----create alter insert update select等

 1.create alter insert update select等

如何建表
   學生表student
        create table student( --學生表
           xh number(4), --學號
           xm varchar2(10), --姓名
           sex char(2), --性別
           birthday date, --日期
           sal number(7,2) --獎學金
        );
   班級class
       create table class( --班級表
          classid number(2), --班級編號
          cname varchar2(20) --班級名字
       );
  添加字段(學生所在班級classid)
      alter table student add (classid number(2));
  修改字段的長度
      alter table student modify (xm varchar2(12)) ;
  修改字段的類型(不能有記錄的)
      alter table student modify (xh varchar2(5));
  刪除一個字段
      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));

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

           恢復ORACLE默認格式
               alter session set nls_date_format = 'dd-mon-yy';
           察看日期的格式
               set linesize 1000
               select * from nls_session_parameters
                 where parameter='NLS_DATE_FORMAT';
           永久設置日期格式
               改註冊表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
      改一個字段 
       update student set sex='女' where xh='A001';
      改多個字段
       update student set sex='男',
                          birthday='1980-04-01'
              where xh='A001';
      改爲空值 (修改爲空時=null)
       update student set birthday=null where xh='A001';
      把生日爲空的人的班級編號改爲20(條件中的空是is null / is not null)
          update student set classid=20 where birthday is null;
         錯誤的沒有達到要求
         update student set classid=20
           where birthday=null;
         不表示空值 表示xm是null的字符串        
         update student set classid=20 where xm='null';
 
   
  刪除 delete
      delete from student;  刪除所有記錄,表結構還在,寫日誌,可以恢復的,速度慢
      drop table student;  刪除表的結構和數據
      delete from student where xh='A001';  刪除一條記錄

      truncate table student; 刪除表中的所有記錄,表結構還在,不寫日誌,無法找回刪除的記錄,速度快

  查詢 select   
      select * from student;

      select xh,xm,sex from student;    

      select * from student where xh like 'A%1'; %任意多個字符
      select * from student where xh like 'A__1'; _1個字符
      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/默認)
                
      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'; (!=)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章