java:Oracle(table的增刪改查,data的增刪改查)


1、oracle命名規范:和Java的命名規范很像 1.嚴格區分大小寫
    2.所有的sql語句都要以';'結尾
    3.所有的sql 都要使用空格區分:sqlplus空格/空格as空格sysdba回車
    4.無論是表名還是列名都必須以字母開頭 java中的class必須要以大寫字母開頭,遵循駝峰命名方式
    5.不能使用ORACLE的保留字
    6.不能與用戶定義的其它對象重名
    7.數字,$#_,字母(只能包含A-Z,a-z,0-9,_,$,#)字母加上_
   
emp表中有一列叫emp_no(員工的編號)
2、連接oracle有兩種方式:
    清理命令行:cls
    oracle的清理命令行:host cls
    1.PL/SQL連接
   2.原生命令行連接(CMD) sqlplus回車 請輸入用戶名:scoot 請輸入密碼(輸入的密碼在命令行上是不可見):回車 user scott locked!(用戶被鎖了,無法登陸!)

    加鎖命令: 首先要登陸管理員用戶 sqlplus / as sysdba:使用超級(系統)管理員登錄 alter user oracle的用戶名 account locked     sysdbs:system database administator(超級管理員) 解鎖命令: 首先要登陸管理員用戶 sqlplus / as sysdba alter user oracle的用戶名 account unlock 查看當前用戶登錄sql: show user; 修改用戶密碼:(修改的是密碼!) 首先要登陸管理員用戶 在oracle中,以后只要看到account這個單詞,就一定是用來修改用戶的某些信息 但是只需要修改密碼的時候就不需要account關鍵字 alter user oracle的用戶名 identified by 123; 解鎖並修改用戶密碼: oracle有自己的錯誤代碼ORA-00922:選項缺失或無效 alter user scott account unlock identified by 新密碼; 查看當前用戶狀態: select username,account_status from dba_status where username='scott'; 在登錄oracle完畢后,如何切換用戶: conn 用戶名;(適用於黑框)
3.oracle的table(表): 數據庫是需要存儲數據,數據庫中的表把存儲的數據進行按規定存放。 雇員(emp):存放員工的信息 部門dept:存放部門的信息 table:表名,列,定義列的數據類型,行
  
  列的類型: 最常用的三個類型: 1.varchar2 2.number 3.date/timestap char和varchar2都是字符串類型: char是固定長度的字符串。char(10),這個里面就必須要有10個char類型字符 比如:男,女;省份(河南省) varchar2是不固定長度的字符串。 推薦使用varchar2 CLOB類型(String): 字符集據:很長很長很長很長的字符串,最大可以存儲4個G的數據。 decimal(其實就是number的一個長類型:比number類型存儲的數據量要大):decimal(100,3):100為多少位整數,3位小數 表數據的唯一標識: 主鍵:是為了約束每一行數據的唯一性。 遵循特點: 不為空,且唯一。 建表的時候添加主鍵: create table 表名 (列名 列的類型 primary key not null); 表建立完成以后,指定列為主鍵 前提是:沒有null的數據&&沒有重復的數據。 alter table 表名 add constraint(約束,限制) PK_表名_指定的列名 primary key(指定的主鍵那一列); 表的建立(表名,列名,列的類型): create table 需要創建的表名 (列名和列的類型,列名和列的類型); 創建一個學生表: create table stus (stu_no number(5), stu_name varchar2(20),........); 建立表的同時添加主鍵: create table 表名(列名 列的類型 primary key not null, 列名 列的類型, 列名 列的類型...); 表的刪除: drop table 需要刪除的表名; 只清空表中的數據,並不刪除表: truncate table 表名; 表的修改: 修改表名: alter table 修改前的表的名字 rename to 新的表名 修改列名: alter table 表名 reanme column 修改的列名 to 新的列名 添加列: alter table 表名 add(需要添加的列名 列的類型); 刪除列名: alter table 表名 drop column 需要刪除的列名; 修改列的類型: alter table 表名 modify (需要修改的列名 修改的列的類型);
   數據的修改:

       1.增加數據
        insert into 表名 (列名) values (列名對應的值);(使用pl/sql操作oracle中時,插入varchar2類型數據必須加單引號!
      2.刪除數據
        delete from teacher where name='hahaha';
        delete from teacher where id=8;
      3.修改數據
        update teacher set age=29 where id=7;
      4. 通過where關鍵字進行過濾查詢數據
        select * from teacher where id =1;

  視圖:
     1.創建
       
create view 視圖名 as (select * from 哪個表)(可以將查詢得到的表創建成一個視圖)

      2.創建替換更名設置權限只讀
        create  or replace  view 視圖名 as(select st.id  from stu2 st) with read only  
     3.刪除
       
drop view 視圖名
     4.增加數據
       
insert into 視圖名 (列名) values (列名對應的值);  
      5.修改數據

        update 視圖名 s set s.AGE=99,s.SALARY=100,s.NAME='張三' where s.ID=7
      6.查詢
        select * from 視圖名
        
4.PL/SQL: PL/SQL是一種sql語言 PL/SQL是一個oracle的連接工具 users:包含了所有oracle的用戶(在sysdba用戶登錄模式下,來完成用戶名,登錄密碼,權限配置) tables:包含了當前用戶的所有表 簡單的查詢語句: select * from 表名; 查詢出表中的所有數據 oracle中的注釋: --
     
data:

1.通過sql語句插入一條數據
  --
insert into 表名 (列名) values (列名對應的值);   


  -- 在使用insert into語句時,如果是字符串類型的數據,就必須要加上單引號''---->中文單引號‘’---->英文單引號''   -- 在使用insert into語句時,如果設定了非空字段,就一定要插入值,否則會報錯。   -- 並且insert into teacher(列名) values(列所對應的值);--->要一一對應   insert into teacher (id,name,age,description) values(5,'tianqi',36,'我是田七');   insert into teacher (id, name, age) values(6,'趙八', 34);   --insert into teacher(id,name,description) values(7, '王九', '我是王九');   insert into teacher (name, id, age, description) values('hahaha', 7, 30, '我是一個哈哈哈');

2.通過sql進行刪除數據   -- 在刪除的過程中,盡量要使用唯一鍵進行刪除,或者使用主鍵進行刪除   delete from teacher where name='hahaha';   delete from teacher where id=8; 3.通過sql語句修改一條數據   -- modify/update   -- modify在oracle中適用於列的類型修改,也就是對表的修改進行操作   -- update關鍵字在oracle中適用於對數據的修改   -- 使用update關鍵字修改數據時,一定要用唯一鍵或者主鍵進行修改   -- 在修改數據時,如果修改多個數據,中間用逗號隔開,字符串帶上單引號   update teacher set id=7, name='我是哈哈哈', age=35, description='我是測試數據' where id=10;   update teacher set age=29 where id=7;   --where要講 in, =, <, >, between and, !=(<>), >=, <=, or, is null, is not null, not in, like "_L%", like "%_a%" escape 'a' 4.通過where關鍵字進行過濾查詢    =關鍵符號   select * from teacher where id =1;    大於和小於   select * from teacher where id > 1;   select * from teacher where id < 9;   大於等於和小於等於   select * from teacher where id >= 5;   select * from teacher where id <= 1;   實現區間(范圍)查詢   select * from teacher where id >= 2 and id <= 10;   between and 是包含兩邊的極限數據(閉區間)   select * from teacher where id between 2 and 10;   !=(不等於)   select * from teacher where id != 5;   select * from teacher where id <> 5;   or關鍵字(或者)   select * from teacher where id =1 or id = 2;   and關鍵字(並且)   select * from teacher where id = 6 and name = '趙八';   in關鍵字(主要的作用:批量操作)   select * from teacher where id in(1, 5, 10);  
 is null 過濾出來所有為null的數據   select * from teacher where description is null;   is not null 過濾出來所有不為null的數據   select * from teacher where description is not null;   not in(過濾出不在范圍內的所有數據)   select id from teacher where id > 5只查詢出了指定的id   select * from teacher where id not in(select id from teacher where id > 5);   select * from teacher where id not in(1,5,7);   select * from teacher where name not in('zhangsan', 'lisi', 'wangwu');   模糊查詢   --like一般情況下要和%結合起來用,%其實一個占位符,如果把%寫在前面,匹配以趙結尾的所有名字,反之匹配以趙開頭的所有名字     如果所需要查詢的字段前后都加上%,只要包含該查詢字段就全部查找出來     select * from teacher where name like '%a%';   

    以_a%進行模糊查詢的時候,會匹配以第二位為'a'的字符串     --_就是代表了任意一個字符     select * from teacher where name like '_a%';     -- 查詢以_開頭的所有數據     -- 需要把a給去掉     -- 匹配規則:使用escape的時候會,如果_寫在需要查詢字段的前面,oracle會自動把_轉移為任意一個字符     -- 只有把下划線寫在需要查詢字段的后面,才能使用escape關鍵字去掉多余字段,只保留下划線。     如果'%_x%',使用escape關鍵字時,會將要去掉后面的一位轉義     select * from teacher where name like '%x_d%' escape 'x';     

    *查詢     -- 在sql中,可以使用簡單的運算符(+,-,*,/)     -- 查詢某張表的所有數據     select * from teacher;     -- 查詢某張表的指定數據     select name, age from teacher;     -- 查詢某張表的指定數據(計算)     select name, age from teacher where id=1;     -- 十年后張三多大?     select name, age+10 from teacher where id=1;     

    給age字段起別名     -- 給字段起別名:別名是為了更好對字段進行描述,也就是說起一個別名並不能隨意起,要適用於這個字段的意思     select name, age+10 as age from teacher where id=1;     -- 通常情況下,這里的as關鍵字可以省略     select name, age+10 age from teacher where id=1;     -- 也可以給表起一個別名(小名)     select t.name from teacher t;          
    更改了列名,里面加了個空格
    
      如果別名中間出現了空格,就會報錯(找不到from關鍵字)
    
    只需要給別名加一個雙引號(必須要是雙引號)
    select name, age+10 "zhangsan age" from teacher where id=1;
    
    更改了列名,數據內容
    -- 姓名:zhangsan 年齡:33<---\\     -- 姓名:也需要加上引號(一定要使用單引號)     select '插入數據中的字段'|| name '自己起的列名' from teacher;     查詢出所有的老師信息,並且以年齡從小到大排列-->order by     -- order by 默認是升序--->asc升序,如果以升序進行排列通常情況下asc省略     select * from teacher order by age asc;     查詢出所有的老師信息,並且以年齡從大到小排序---->desc降序     select * from teacher order by age desc;     -- 如果兩個老師年紀相同,就以姓名的首字母排序     select * from teacher order by age desc, name;     去重(關鍵字:distinct)     select distinct age from teacher;     select distinct t.age, t.name from teacher t;     select distinct name from teacher;多表查詢     dual,計算1+1等於幾?     select 1+1 from dual;-- dual其實也是一張表,這一表是虛擬的,沒有列和數據,當查詢的時候會進行賦予列和數據,用於方便用戶測試使用     -- 查詢當前oracle登錄的用戶     select user from dual;     --sysdate,sysdate-,months_netween(,),add_months(,),next_day(,'星期幾'),last_day,to_day 5.時間函數     獲取當前的時間:sysdate     select sysdate from dual;     sysdate-時間,查詢的是指定日期距離當前日期的天數     select t.hire_date, t.name, sysdate, sysdate-hire_date from teacher t;     add_months:對指定日期月份進行加減計算     select add_months(sysdate,3) from dual;     select add_months(sysdate,-3) from dual;     months_between:指定日期距離當前日期的月份     select t.hire_date, sysdate, months_between(sysdate, hire_date) from teacher t;     next_day:查詢出指定日期和指定星期幾的日期:只能往后不能往前     select next_day(sysdate, '星期一') from dual;     last_day:查詢出當月的最后一天     select last_day(sysdate) from dual;     select t.hire_date, last_day(hire_date) from teacher t;     


    to_date:時間轉換函數     -- 默認轉換為年月日,不帶時分秒     --但是oracle查詢當前日期會自帶時分秒     -- 使用to_date函數進行轉換的時候,查詢出的數據仍然遵循oracle的日期規范     -- to_date:只能把年月日時分秒轉換為年月日,或者把年月日轉換為時分秒,不能轉換oracle的日期格式規范     -- oracle中沒有任何一個函數可以修改oracle日期格式規范     select to_date('1987/08/21 19:18:02', 'yyyy-mm-dd hh24:MI:ss') from dual;     --lower(), upper(), initcat(), length(), replace(替換的列名,'z','Z')(把小z替換成大Z),substr(需要截取的列名,開始截取的位置,最后截取的位置), concat(,),ceil,float,round,trunc    

    to_char:把日期轉化為字符串
      提取年
      select to_char(sysdate,'yyyy')from dual
      提取月
      select to_char(sysdate,'mm')from dual
      提取日

       select to_char(sysdate,'dd')from dual

       提取小時(hh是12制的,hh24是24進制)

       select to_char(sysdate,'hh')from dual 

          提取具體時間
       select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')from dual

     to_number:把字符串轉為數字類型 (必須為數字類型的字符串)

       select to_number('123456')from dual 

     count(*)或count(任意數字):計算數據的數量,其實就是把數據全轉化為括號內的數字,再進行統計,查詢      出來是多出一個count(?)列。

     實際開發中一定要使用count(1)不要使用count(*)!

      

6.oracle中的函數

    -- lower
    -- 把所有教師名字(英文)全部轉換為小寫
    select lower(name) from teacher;
    -- upper
    -- 把所有教師名字(英文)全部轉換為大寫
    select upper(name) from teacher;
    --initcap:首字母開頭大寫
    select initcap('oracle') from dual;
    select initcap(name) from teacher;

    --length:獲取字符串的長度
    select length(name) from teacher where id =1;

    --replace:有三個參數
    -- 第一個參數:列名(字段);第二個參數:需要替換字段中的某一個字符;第三參數:替換的字符
    select * from teacher where id =1;
    select replace(name,'z','Q') from teacher where id =1;

    -- substr:和Java的subString差不多
    -- substr有兩個參數:第一個參數:列名,第二個參數:從哪一位開始截取(包含最后一位截取的數):name,要截取前兩位:subtr('name',2);--->得到的是ame
    select substr(name, 3) from teacher where id =1;
    -- 如果第二個參數為負數的話,就倒着從最后一位開始往前截取
    select substr('zhangsan', -3) from dual;
    --substr:
    select * from teacher;
    --subtr:有三個參數:第一個參數:列名,第二個參數:從哪一位開始截取(閉區間),第三個參數:截取到多少位(會從第二個參數的截取位置往后開始進行截取)
    -- oracle中,截取的位置會從0或者1起始(0和1的位置是一樣的)
    select substr('zhangsan',0,5) from teacher where id = 1;
    select substr('zhangsan',1,5) from teacher where id = 1;

    -- ceil:向上取整
    select ceil('12345.1') from dual;
    -- floor:向下取整
    select floor('12345.1') from dual;
    -- round:四舍五入
    select round('12345.1') from dual;
    select round('12345.7') from dual;
    select round('12845.6', -3) from dual;

    -- trunc:截斷:會取整數
    -- 第二個參數:保留的小數位
    -- 如果沒有小數,並且第二個參數為正數:就原樣顯示,不會加上.00
    select trunc('123444.87978787',2) from dual;
    -- 如果沒有小數,並且第二個參數為負數:會把整數位從后往前寫為0
    select trunc('123456', -3) from dual;

    -- concat:拼接字符串--->代替||
    select concat('zhang','san') from dual;
    select concat('姓名是:',name) from teacher;





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM