共計 3430 個字符,預計需要花費 9 分鐘才能閱讀完成。
這篇文章主要講解了“SQL 語句增刪改查的用法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“SQL 語句增刪改查的用法”吧!
關鍵字: sql 語句 (增刪改查)
一、增:有 4 種方法
1. 使用 insert 插入單行數據:
語法:insert [into] 表名 [列名] values 列值
例:insert into Strdents (姓名, 性別, 出生日期) values (開心朋朋 , 男 , 1980/6/15)
注意:into 可以省略;列名列值用逗號分開;列值用單引號因上;如果省略表名,將依次插入所有列
2. 使用 insert select 語句將現有表中的數據添加到已有的新表中
語法:insert into 已有的新表 列名
select 原表列名 from 原表名
例:insert into tongxunlu (姓名 , 地址 , 電子郵件)
select name,address,email
from Strdents
注意:into 不可省略;查詢得到的數據個數、順序、數據類型等,必須與插入的項保持一致
3. 使用 select into 語句將現有表中的數據添加到新建表中
語法:select 新建表列名 into 新建表名 from 源表名
例:select name,address,email into tongxunlu from strdents
注意:新表是在執行查詢語句的時候創建的,不能夠預先存在
在新表中插入標識列(關鍵字‘identity’):
語法:select identity (數據類型,標識種子,標識增長量) AS 列名
into 新表 from 原表名
例:select identity(int,1,1) as 標識列,dengluid,password into tongxunlu from Struents
注意:關鍵字‘identity’
4. 使用 union 關鍵字合并數據進行插入多行
語法:insert 表名 列名 select 列值 tnion select 列值
例:insert Students (姓名, 性別, 出生日期)
select 開心朋朋 , 男 , 1980/6/15 union(union 表示下一行)
select 藍色小明 , 男 , 19**/**/**
注意:插入的列值必須和插入的列名個數、順序、數據類型一致
二、刪:有2中方法
1. 使用 delete 刪除數據某些數據
語法:delete from 表名 [where 刪除條件]
例:delete from a where name= 開心朋朋(刪除表 a 中列值為開心朋朋的行)
注意:刪除整行不是刪除單個字段,所以在 delete 后面不能出現字段名
2. 使用 truncate table 刪除整個表的數據
語法:truncate table 表名
例:truncate table tongxunlu
注意:刪除表的所有行,但表的結構、列、約束、索引等不會被刪除;不能用語有外建約束引用的表
三、改
使用 update 更新修改數據
語法:update 表名 set 列名 = 更新值 [where 更新條件]
例:update tongxunlu set 年齡 =18 where 姓名 = 藍色小名
注意:set 后面可以緊隨多個數據列的更新值;where 子句是可選的,用來限制條件,如果不選則整個表的所有行都被更新
四、查
1. 普通查詢
語法:select 列名 from 表名 [where 查詢條件表達試] [order by 排序的列名 [asc 或 desc]]
1). 查詢所有數據行和列
例:select * from a
說明:查詢 a 表中所有行和列
2). 查詢部分行列 – 條件查詢
例:select i,j,k from a where f=5
說明:查詢表 a 中 f = 5 的所有行,并顯示 i,j,k3列
3). 在查詢中使用AS更改列名
例:select name as 姓名 from a whrer xingbie= 男
說明:查詢 a 表中性別為男的所有行,顯示 name 列,并將 name 列改名為(姓名)顯示
4). 查詢空行
例:select name from a where email is null
說明:查詢表 a 中 email 為空的所有行,并顯示 name 列;SQL 語句中用 is null 或者 is not null 來判斷是否為空行
5). 在查詢中使用常量
例:select name 唐山 as 地址 from a
說明:查詢表 a,顯示 name 列,并添加地址列,其列值都為 唐山
6). 查詢返回限制行數 (關鍵字:top percent)
例1:select top 6 name from a
說明:查詢表 a,顯示列 name 的前6行,top 為關鍵字
例2:select top 60 percent name from a
說明:查詢表 a,顯示列 name 的 60%,percent 為關鍵字
7). 查詢排序(關鍵字:order by , asc , desc)
例:select name
from a
where chengji =60
order by desc
說明:查詢表中 chengji 大于等于 60 的所有行,并按降序顯示 name 列;默認為ASC升序
2. 模糊查詢
1). 使用 like 進行模糊查詢
注意:like 運算副只用語字符串,所以僅與 char 和 varchar 數據類型聯合使用
例:select * from a where name like 趙 %
說明:查詢顯示表 a 中,name 字段第一個字為趙的記錄
2). 使用 between 在某個范圍內進行查詢
例:select * from a where nianling between 18 and 20
說明:查詢顯示表 a 中 nianling 在 18 到 20 之間的記錄
3). 使用 in 在列舉值內進行查詢
例:select name from a where address in (北京 , 上海 , 唐山)
說明:查詢表 a 中 address 值為北京或者上海或者唐山的記錄,顯示 name 字段
3. 分組查詢
1). 使用 group by 進行分組查詢
例:select studentID as 學員編號, AVG(score) as 平均成績 (注釋: 這里的 score 是列名)
from score (注釋: 這里的 score 是表名)
group by studentID
說明:在表 score 中查詢,按 strdentID 字段分組,顯示 strdentID 字段和 score 字段的平均值;select 語句中只允許被分組的列和為每個分組返回的一個值的表達試,例如用一個列名作為參數的聚合函數
2). 使用 having 子句進行分組篩選
例:select studentID as 學員編號, AVG(score) as 平均成績 (注釋: 這里的 score 是列名)
from score (注釋: 這里的 score 是表名)
group by studentID
having count(score) 1
說明:接上面例子,顯示分組后 count(score) 1 的行,由于 where 只能在沒有分組時使用,分組后只能使用 having 來限制條件,
4. 多表聯接查詢
1). 內聯接
①在 where 子句中指定聯接條件
例:select a.name,b.chengji
from a,b
where a.name=b.name
說明:查詢表 a 和表 b 中 name 字段相等的記錄,并顯示表 a 中的 name 字段和表 b 中的 chengji 字段
②在 from 子句中使用 join…on
例:select a.name,b.chengji
from a inner join b
on (a.name=b.name)
說明:同上
2). 外聯接
①左外聯接查詢
例:select s.name,c.courseID,c.score
from strdents as s
left outer join score as c
on s.scode=c.strdentID
說明:在 strdents 表和 score 表中查詢滿足 on 條件的行,條件為 score 表的 strdentID 與 strdents 表中的 sconde 相同
②右外聯接查詢
例:select s.name,c.courseID,c.score
from strdents as s
right outer join score as c
on s.scode=c.strdentID
說明:在 strdents 表和 score 表中查詢滿足 on 條件的行,條件為 strdents 表中的 sconde 與 score 表的 strdentID 相同
感謝各位的閱讀,以上就是“SQL 語句增刪改查的用法”的內容了,經過本文的學習后,相信大家對 SQL 語句增刪改查的用法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!