【SQL】基础增删改查
目錄
🥱前言🥱
🍔關鍵字
🍔1. 增
🦐1.1 代碼格式
🦐1.2 全字段插入
🦐1.3 指定字段插入
🍔2. 查
🦐2.1 代碼格式
🦐2.2 查詢表中所有數據
🦐2.3 查詢表中某個或某些字段
🦐2.4 帶表達式的查詢
🦐2.5 別名
🦐2.6 distinct(去重)
🦐2.7 SQL 中的函數調用
🦐2.8 order by
🦐2.9 分頁查詢
🦐2.10 where
🦐2.11 模糊匹配
🍔3. 改
🍔4. 刪
?🦐4.1 delete
?🦐4.2 truncate
🥱前言🥱
增刪改查是 SQL 中最基本也是最重要的操作,基本上所有的 SQL 代碼都離不開增刪改查
🍔關鍵字
增:insert
刪:delete
改:update
查:select
SQL 代碼中是不區分大小寫的,大寫或小寫都一樣
🍔增
提前建表以供插入
CREATE TABLE student (id INT,sn INT comment '學號',name VARCHAR(20) comment '姓名',qq_mail VARCHAR(20) comment 'QQ郵箱' );🦐代碼格式
insert into 表名(字段名) values (插入的值);🦐全字段插入
全字段插入分為全字段單行插入和全字段批量插入,單行插入一次插入一行,批量插入一次插入多行
?單行插入
INSERT INTO student VALUES (99, 9999, '豬八戒', NULL);?
批量插入
INSERT INTO student VALUES(101, 10001, "孫悟空", '11111'),(101, 10002, '沙悟凈', 'hello');🦐指定字段插入
指定字段同樣有指定字段單行插入和指定字段批量插入
?單行插入
INSERT INTO student (sn, id, name, qq_mail) VALUES (102, 20001, '曹孟德', null);?
批量插入?
INSERT INTO student (sn, id, name, qq_mail) VALUES (104, 200011, '123', null),(103, 2001, 'sss', 'nnn');?
?需要注意的是:指定字段插入時,缺少的字段會填入默認值,如果該字段沒有默認值則會報錯
?
🍔查
提前建表并且插入測試數據以供查詢,
CREATE TABLE exam_result (id INT,name VARCHAR(20),chinese DECIMAL(3,1),math DECIMAL(3,1),english DECIMAL(3,1) );-- 插入測試數據 INSERT INTO exam_result (id,name, chinese, math, english) VALUES(1,'唐三藏', 67, 98, 56),(2,'孫悟空', 87.5, 78, 77),(3,'豬悟能', 88, 98, 90),(4,'曹孟德', 82, 84, 67),(5,'劉玄德', 55.5, 85, 45),(6,'孫權', 70, 73, 78.5),(7,'宋公明', 75, 65, 30);🦐代碼格式
select 字段名 from 表名;🦐查詢表中所有數據
select * from exam_result;🦐查詢表中某個或某些字段
select id, name, english from exam_result;🦐帶表達式的查詢
常數
select 之后直接跟常數在某個表中查詢,查詢到的結果就是一列這個常數,表中原本有多少行數據,就會查詢到多少行這個常數
select 后也可跟常數的運算表達式,查詢到的結果就是常數的運算結果
-- 帶常數的select select 103 from exam_result;-- 帶常數運算的select SELECT 1 + 1 FROM exam_result;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
有字段參與計算的表達式
有字段參與計算的 select 查詢后的結果就是表中字段數值的運算結果
SELECT id, name, id * 10 FROM exam_result;SELECT id, name, chinese + math + english from exam_result;?? ?
?帶 bool 表達式的 select
帶 bool 表達式的 select 查詢到的結果是 bool 表達式為 true 時的結果
SQL 中 1 為 true ,0 為 false
select id, id > 5 from exam_result;select id, id != 2 from exam_result;???
?與、或、非? ? ? ? and、or、not
select id, id > 1 and id < 5 from exam_result;select id, id > 1 or id < 5 from exam_result;select id, not id = 1 from exam_result;????
?關于 null
SQL 中只要有 null 參與的表達式,此表達式的結果仍為 null
select 1 + null from exam_result;?
🦐別名
別名就是在一個表達式后面用 as 加上一個賦給該表達式的名字,as 可省略,并且別名會替換查詢結果的名字
-- 以下兩條是一樣的 SELECT id, name, chinese + math + english AS total from exam_result;SELECT id, name, chinese + math + english total from exam_result;🦐distinct(去重)
distinct 在代碼中位于字段之前,表示對該字段進行去重,即將所有重復的字段值合并
select distinct math from exam_result;🦐SQL 中的函數調用
SQL 中的函數有很多,此處演示幾個常用的函數
count()?
count 函數是用來統計該字段中數值的個數,即該字段在表中有多少行數據此函數返回的值就是幾
select count(math) from exam_result;avg()?
該函數返回一個字段中所有數值的平均值,該字段中的元素必須是數值元素
select avg(math) from exam_result;min()?
該函數返回一個字段中所有數值中的最小值,該字段中的元素必須是數值元素
select min(math) from exam_result;?其他函數可以去MySQL 函數看看
🦐order by
order by 表示查詢到的結果以某個字段為依據排序
-- 以 id 升序排序,asc 可省略 select * from exam_result order by id asc;-- 以 id 降序排序 select * from exam_result order by id desc;🦐分頁查詢
用到的關鍵字:offset、limit
? ? ? ? offset:本頁從哪一條開始
? ? ? ? limit:本頁限制顯示多少條
這兩個關鍵字搭配 order by 來使用
-- 第一頁從 第 0 個 id 開始,顯示兩條 select * from exam_result order by id limit 2 offset 0;-- 第二頁從 第 2 個 id 開始,顯示兩條 select * from exam_result order by id limit 2 offset 2;?
🦐where
where 后跟條件表達式,用于篩選查詢后的結果
如果 where 后跟的表達式為永真的,那么將會查詢到表中所有結果
-- 查詢 chinese < 60 的所有結果 select * from exam_result where chinese < 60;🦐模糊匹配
?關鍵字:like? ? ? ? ? ? ? ? 只能用于字符串類型
%:表示任意字符
-- 查詢 name = 孫某 select * from exam_result where name like '孫%';-- 查詢 name = 某孫 select * from exam_result where name like '%孫';-- 查詢name = 某孫某 select * from exam_result where name like '%孫%';🍔改
代碼格式
update 表明 set 字段名 = '修改的值' where 條件?演示
?更新前
?
update exam_result set name = 123 where id = 1;更新后
🍔刪
代碼格式
delete from 表名 where 條件;?🦐delete
-- 刪除 id = 1 在表中的那一行 delete from exam_result where id = 1;🦐truncate
代碼格式
-- 清空表中的數據 truncate 表名;總結
以上是生活随笔為你收集整理的【SQL】基础增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米oj 反向位整数(简单位运算)
- 下一篇: 在HTML中插入JavaScript代码