oracle資料庫基本操作

2021-06-28 13:12:42 字數 4089 閱讀 1645

一、增:有3種方法

1.使用insert插入單行資料:

insert [into] 《表名》 [列名] values

《列值》

insert

into strdents (name,age) values ('atm',12)

2.使用insert,select語句將現有表中的 資料新增到已有的新錶中

insert

into

《已有的新錶》 《列名》 select

《原表列名》 from

《原表名》

insert

into newtable (name,class)select name,class from tableinfo

3.將資料插入原表中(生成測試資料用的較多)

和第二種方法一樣,只是複製到原表中
insert

into tableinfo ('name','class')select name,class from tableinfo

二、刪:有3中方法

1.delete刪除

delete

from

《表名》 [where

《刪除條件》]  

delete

from tableinfo where name='atm'

2.truncate table 刪除整個表的資料

truncate

table

《表名》

truncate

table tableinfo

刪除表的所有行,但表的結構、列、約束、索引等不會被刪除;不能用於有外建約束引用的表

3、drop刪除

drop

table

《表名》

drop

table tableinfo

刪除表中所有行,表結構也刪除了。

三、update更新修改

update

《表名》 set

《列名=更新值》 [where

《更新條件》]

update tableinfo set age=12

where name='atm1'

set後面可以緊隨多個資料列的更新值(非數字要引號);

四、查

1.普通查詢

select

《列名》 from

《表名》 [where

《查詢條件表達試》] [order

by《排序的列名》[asc或desc]]

1).查詢所有資料

select * from tableinfo

2).查詢部分行列--條件查詢

select name,age from tableinfo where age=11;

3).在查詢中使用as更改列名

select name as 姓名 from a where age=11;

4).查詢空行

select name from tableinf where class is

null

5).查詢返回限制行數(關鍵字:top )

select top 6 name from tableinfo

顯示列name的前6行,oracle 中用rownum替代(select * from a where rownum<6 )

6).查詢排序(關鍵字:order

by , asc , desc)

例:select name from tableinfo where age>=11

order

bydesc(預設為asc公升序)

2.模糊查詢

1).使用like進行模糊查詢 

請看另一篇文章, sql like四種用法

2).使用between在某個範圍內進行查詢

select * from tableinfo where age between 11

and22

3).使用in在列舉值內進行查詢(in後是多個的資料)

select name from tableinfo where name in ('atm','atm1','atm2');

1、固定列數的行列轉換如

student subject grade

--------- ---------- --------

student1 語文 80

student1 數學 70

student1 英語 60

student2 語文 90

student2 數學 80

student2 英語 100

……轉換為

姓名 語文 數學 英語 總分

student1 80 70 60

student2 90 80 100

……語句如下:

select student,

sum(decode(subject,'語文', grade,null)) "語文",

sum(decode(subject,'數學', grade,null)) "數學",

sum(decode(subject,'英語', grade,null)) "英語",

sum(subject) "總分"

from table

group by student;

2、a表中有b表沒有的資料

表aid  name

1    a

2    b

3    c

4    d

表bid  a_id

1     1

2     3

-----------

期望結果

2  b

4  d

-----------

實際上b表中的a_id是和a表中的id對應。

sql: select * from a where id not in (select a.id from a join b on a.id=a_id); 

select * from a where id not in (select a_id from b)

select * from a where not exists (select 1  from b where id=a.id)

例如: selectdecode( x , 1 , 『x is 1 』, 2 , 『x is 2 』, 『others』) from dual 當x等於1時,則返回『x is 1』。 當x等於2時,則返回『x is 2』。 否則,返回others』。 需要,比較2個值的時候,可以配合sign()函式一起使用。 select decode( sign(5 -6), 1 'is positive', -1, 'is nagative', 'is zero') 同樣,也可以用case實現: select case sign(5 - 6) when 1 then 'is positive'when -1 then 'is nagative'else 'is zero' endfrom dual此外,還可以在order by中使用decode。 例如:表table_subject,有subject_name列。要求按照:語、數、外的順序進行排序。這時,就可以非常輕鬆的使用decode完成要求了。 select * from table_subject order by decode(subject_name, '語文', 1, '數學', 2, , '外語',3)

www.2cto.com  

將所有的結果全部寫出

select * from classes t

資料為11一班 num_1

22二班 num_2

33三班 num_3

44四班 num_4

select t.* from classes t order by decode(t.classnum,'num_1',4,'num_2',3,'num_3',1);

oracle資料庫基本操作

檢視磁碟使用情況 df h 進入oracle su oracle sqlplus nolog conn as sysdba 檢視三種資料庫檔案 select from vlo gfil e 日 志檔案s elec t fr omv datafile 資料檔案 select from v contro...

oracle資料庫基本操作

1.在 oracleservice 服務啟動動後,就可以對資料庫進行管理了,oracle 的啟動和關閉是最基本的命令,在 sql plus 中,啟動 oracle 必須是 sys 使用者,命令格式是 startup open oracle 服務關閉用命令 shutdownimmediate orac...

oracle資料庫基本操作

oracle建立表空間和使用者授權 sys使用者在cmd下以dba身份登入 在cmd中打sqlplus nolog 匿名登入 然後再conn as sysdba 以dba身份登入 建立臨時表空間 create temporary tablespace bigoa temp tempfile e or...