日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

SQL语句之操作表记录(CRUD)

發(fā)布時(shí)間:2023/12/8 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL语句之操作表记录(CRUD) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(1)INSERT
INSERT INTO table [(column [, column...])] VALUES (value [, value...]);

插入的數(shù)據(jù)應(yīng)與字段的數(shù)據(jù)類型相同。
數(shù)據(jù)的大小應(yīng)在列的規(guī)定范圍內(nèi),例如:不能將一個(gè)長(zhǎng)度為80的字符串加入到長(zhǎng)度為40的列中。
在values中列出的數(shù)據(jù)位置必須與被加入的列的排列位置相對(duì)應(yīng)。
字符和日期型數(shù)據(jù)應(yīng)包含在單引號(hào)中。
插入空值:不指定或insert into table value(null)
如果要插入所有字段可以省寫列列表,直接按表中字段順序?qū)懼盗斜?




~使用insert語(yǔ)句向表中插入三個(gè)員工的信息
insert into employee (id,name,gender,birthday,entry_date,job,salary,resume)values (null,'張飛',1,'1999-09-09','1999-10-01','打手',998.0,'老大的三弟,真的很能打');
insert into employee values (null,'關(guān)羽',1,'1998-08-08','1998-10-01','財(cái)神爺',9999999.00,'老大的二弟,公司掙錢都指著他了');
insert into employee values (null,'劉備',0,'1990-01-01','1991-01-01','ceo',100000.0,'公司的老大'),(null,'趙云',1,'2000-01-01','2001-01-01','保鏢',1000.0,'老大貼身人');
(2)UPDATE
UPDATE tbl_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition]??

UPDATE語(yǔ)法可以用新值更新原有表行中的各列。
SET子句指示要修改哪些列和要給予哪些值。
WHERE子句指定應(yīng)更新哪些行。如沒(méi)有WHERE子句,則更新所有的行



~將所有員工薪水修改為5000元。
update employee set salary = 5000;
~將姓名為’張飛’的員工薪水修改為3000元。
update employee set salary = 3000 where name='張飛';
~將姓名為’關(guān)羽’的員工薪水修改為4000元,job改為ccc。
update employee set salary=4000,job='ccc' where name='關(guān)羽';
~將劉備的薪水在原有基礎(chǔ)上增加1000元。
update employee set salary=salary+1000 where name='劉備';

??
(3)DELETE

如果不使用where子句,將刪除表中所有數(shù)據(jù)。
Delete語(yǔ)句不能刪除某一列的值(可使用update)
使用delete語(yǔ)句僅刪除記錄,不刪除表本身。如要?jiǎng)h除表,使用drop table語(yǔ)句。
同insert和update一樣,從一個(gè)表中刪除記錄將引起其它表的參照完整性問(wèn)題,在修改數(shù)據(jù)庫(kù)數(shù)據(jù)時(shí),頭腦中應(yīng)該始終不要忘記這個(gè)潛在的問(wèn)題。
外鍵約束
刪除表中數(shù)據(jù)也可使用TRUNCATE TABLE 語(yǔ)句,它和delete有所不同,參看mysql文檔。




delete from tbl_name [WHERE where_definition]? ??
~刪除表中名稱為’張飛’的記錄。
delete from employee where name='張飛';
~刪除表中所有記錄。
delete from employee;
~使用truncate刪除表中記錄。
truncate table employee;
(4)SELECT
~1.基本查詢
SELECT [DISTINCT] *|{column1, column2. column3..} FROM table;

~查詢表中所有學(xué)生的信息。
select * from exam;
~查詢表中所有學(xué)生的姓名和對(duì)應(yīng)的英語(yǔ)成績(jī)。
select name,english from exam;
~過(guò)濾表中重復(fù)數(shù)據(jù)
select distinct english from exam;
~在所有學(xué)生分?jǐn)?shù)上加10分特長(zhǎng)分顯示。
select name , math+10,english+10,chinese+10 from exam;
~統(tǒng)計(jì)每個(gè)學(xué)生的總分。
select name ,english+math+chinese from exam;
使用別名表示學(xué)生總分。
select name as 姓名 ,english+math+chinese as 總成績(jī) from exam;
select name 姓名 ,english+math+chinese 總成績(jī) from exam;
select name english from exam;


~2.使用where子句進(jìn)行過(guò)濾查詢
~查詢姓名為張飛的學(xué)生成績(jī)
select * from exam where name='張飛';
~查詢英語(yǔ)成績(jī)大于90分的同學(xué)
select * from exam where english > 90;
~查詢總分大于230分的所有同學(xué)
select name 姓名,math+english+chinese 總分 from exam where math+english+chinese>230;
~查詢英語(yǔ)分?jǐn)?shù)在 80-100之間的同學(xué)。
select * from exam where english between 80 and 100;
~查詢數(shù)學(xué)分?jǐn)?shù)為75,76,77的同學(xué)。
select * from exam where math in(75,76,77);
~查詢所有姓張的學(xué)生成績(jī)。
select * from exam where name like '張%';
select * from exam where name like '張__';
~查詢數(shù)學(xué)分>70,語(yǔ)文分>80的同學(xué)。
select * from exam where math>70 and chinese>80;


~3.使用order by關(guān)鍵字對(duì)查詢結(jié)果進(jìn)行排序操作
SELECT column1, column2. column3.. FROM table where... order by column asc|desc;
asc 升序 -- 默認(rèn)就是升序
desc 降序


~對(duì)語(yǔ)文成績(jī)排序后輸出。
select name,chinese from exam order by chinese desc;
~對(duì)總分排序按從高到低的順序輸出
select name 姓名,chinese+math+english 總成績(jī) from exam order by 總成績(jī) desc;
~對(duì)姓張的學(xué)生成績(jī)排序輸出
select name 姓名,chinese+math+english 總成績(jī) from exam where name like '張%' order by 總成績(jī) desc;
~4.聚合函數(shù)
(1)Count -- 用來(lái)統(tǒng)計(jì)符合條件的行的個(gè)數(shù)
~統(tǒng)計(jì)一個(gè)班級(jí)共有多少學(xué)生?
select count(*) from exam;
~統(tǒng)計(jì)數(shù)學(xué)成績(jī)大于90的學(xué)生有多少個(gè)?
select count(*) from exam where math>70;
~統(tǒng)計(jì)總分大于230的人數(shù)有多少?
select count(*)from exam where math+english+chinese > 230;
(2)SUM -- 用來(lái)將符合條件的記錄的指定列進(jìn)行求和操作
~統(tǒng)計(jì)一個(gè)班級(jí)數(shù)學(xué)總成績(jī)?
select sum(math) from exam;
~統(tǒng)計(jì)一個(gè)班級(jí)語(yǔ)文、英語(yǔ)、數(shù)學(xué)各科的總成績(jī)
select sum(math),sum(english),sum(chinese) from exam;
~統(tǒng)計(jì)一個(gè)班級(jí)語(yǔ)文、英語(yǔ)、數(shù)學(xué)的成績(jī)總和
select sum(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) from exam;
在執(zhí)行計(jì)算時(shí),只要有null參與計(jì)算,整個(gè)計(jì)算的結(jié)構(gòu)都是null
此時(shí)可以用ifnull函數(shù)進(jìn)行處理
~統(tǒng)計(jì)一個(gè)班級(jí)語(yǔ)文成績(jī)平均分
select sum(chinese)/count(*) 語(yǔ)文平均分 from exam;
(3)AVG -- 用來(lái)計(jì)算符合條件的記錄的指定列的值的平均值
~求一個(gè)班級(jí)數(shù)學(xué)平均分?
select avg(math) from exam;
~求一個(gè)班級(jí)總分平均分?
select avg(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) from exam;


(4)MAX/MIN -- 用來(lái)獲取符合條件的所有記錄指定列的最大值和最小值
~求班級(jí)最高分和最低分
select max(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) from exam;
select min(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) from exam;
~5.分組查詢
~對(duì)訂單表中商品歸類后,顯示每一類商品的總價(jià)
select product,sum(price) from orders group by product;
~詢購(gòu)買了幾類商品,并且每類總價(jià)大于100的商品
select product 商品名,sum(price)商品總價(jià) from orders group by product having sum(price)>100;

where子句和having子句的區(qū)別:
where子句在分組之前進(jìn)行過(guò)濾having子句在分組之后進(jìn)行過(guò)濾
having子句中可以使用聚合函數(shù),where子句中不能使用
很多情況下使用where子句的地方可以使用having子句進(jìn)行替代


~查詢單價(jià)小于100而總價(jià)大于150的商品的名稱
select product from orders where price<100 group by product having sum(price)>150;




~~sql語(yǔ)句書寫順序:?
select from where groupby having orderby
~~sql語(yǔ)句執(zhí)行順序:
from where select group by having order by

總結(jié)

以上是生活随笔為你收集整理的SQL语句之操作表记录(CRUD)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。