一、增(兩種方法)
1、使用 insert 插入單行數據:
語法:insert ?into ?表名 ?(列名) ?values ?(列值)
例:insert ?into Students ?(姓名,性別,學號) ?value ('Bruce','男','14')
(將Bruce,男,14插入到表Students中)
2、使用 insert,select 語句將 原有表中的數據?添加到 已有新表?中
語法:insert ?into ?已有新表名 ?列名 ?select ?原表列名 ?from ?原表名
例:insert ?into ?AddressList ?('姓名','地址','電話') ?select ?name, address, ?phoneNum ?from Students
(將Students表中的name,address,phoneNum插入到AddressList表中的姓名,地址,電話中)
二、刪(兩種方法)
1、使用 delete 刪除數據
語法:delete ?from ?表名 ?where ?刪除條件
例:delete ?from ?Students ?where ?number='14'
(刪除Students表中學號為14的列)
2、使用 truncate table 刪除整張表的數據
語法:truncate ?table 表名
例:truncate ?table Students
(刪除Students表)
三、改
語法:update ?表名 ?set ?列名=更新值 ?where ?更新條件
例:update ?Students ?set ?age ?= ?25 ?where ?name ?= ?'Bruce'
(將Studets表中姓名為Bruce的學生年齡改為25)
四、查(* 表示查詢所有字段)
1、常規查詢
語法:select ?列名 ?from ?表名 ?where ?查詢條件 ?order ?by ?排序列名 ?asc或desc
(1) 根據表的 行/列 查詢
語法:select ?* ?from ?表名 ?where ?列/行值
例:select ?* ?from ?Students ?where ?name ?= ?'Bruce'
(查詢Students表中姓名為Bruce的行/列)
(2) 查詢部分行/列
語法:select ?所需要的列 ?from ?表名 ?where ?查詢條件
例:select ?i,j,k ?from ?Students ?where ?f=5
(查詢Students表中所有行,并顯示i,j,k 這三列)
(3)再查詢中使用as更改列名
語法:select ?原表中列名 ?as ?改后的列名 ?from ?表名 ?where ?查詢條件
例:select ?name ?as ?姓名 ?from ?Students ?gender = '男'
(查詢Students表中性別為男的所有行,顯示Name列,并將name改為“姓名”顯示)
(4)查詢空行(SQL語句中is null 或is not null來判斷是否為空)
語法:select ?要顯示的列 ?from ?表名 ?where ?為空的字段 ?is ?null
例:select ?name ?from ?Students ?where ?number ?is ?null
(查詢Students表中number為空的所有行,并顯示name列)
(5)在查詢中添加常量
語法: select ?name ?'北京' ?as ?地址 ?from ?Students
(查詢Students表中,顯示name列,并添加列值均為“北京”的地址列)
(6)查詢返回限制行數(oracle中沒有top關鍵字用rownum代替)
語法:select ?top ?限制的行數 ?顯示的列名 ?from ?表名
例:select ?top ?5 ?name ?from ?Students?
(查詢Students中name的前5列)
(7)排序查詢(desc是降序 ? ascs是升序)
語法:select ?顯示的列名 ?from ?表名 ?where ?查詢條件 ?order ?by ?desc
例:select ?name ?from ?Students ?where ?grade ?>= 60 ?order ?by ?desc
(查詢表中成績大于60的所有行,并按降序顯示name列)
2、模糊查詢
(1)使用like進行模糊查詢(like只用于字符串)
語法:select ?* ?from ?表名 ?where ?查詢的列名 ?like ?模糊的字符串
例:select ?* ?from ?Students ?where ?name ?like '張%@'
(查詢Students表中name列中姓張的記錄)
(2)使用between在某個范圍內查找
語法:select ?* ?from ?表名 ?where ?查詢的列名 ?between ?數字 ?and ?數字
例:select ?* ?from ?Students ?where ?age ?between ?18 ?and ?20
(查詢Students表中年齡在18~20 之間的記錄)
(3)使用in在列舉值內進行查詢
語法:select ?要顯示的字段名 ?from ?表名 ?where ?列名 ?in ?查詢的值
例:select ?name ?frome ?Students ?where ?address ?in ?'北京'
(查詢Students表中地址為北京的記錄,并顯示姓名)
3、多表連接查詢
語法:select ?表名1.要顯示的表1關聯的字段,表名2.要顯示的表2中的字段 ?from ?表名1,表名2 ?where ?表名1.與表名2相等的字段 ?= ?表名2.與表名1相等的字段
例:select ?a.name ?, b.age ?from ?a, b ?where ?a.name ?= ?b.name?
(查詢表a和表b中name相等的記錄,并顯示a表中name字段和b表中age字段)
4、分組查詢
(1)使用 ?group by ?進行分組查詢
例:select studentID as 學員編號, AVG(score) as 平均成績? (注釋:這里的score是列名)from score (注釋:這里的score是表名) group by studentID
(2)使用 having 子句進行分組篩選
例:select ?studentID as 學員編號, AVG from score ?group ?by ?studentID having ?count (score)>1
說明:對于“分組查詢”小編還不是特別理解,先暫時放在這里,等以后用得到的時候在整理!
再此感謝技術支持:在此感謝技術支持