Mysql數(shù)據(jù)庫中數(shù)據(jù)的操作CRUD詳解
本文內(nèi)容: 詳細描述對Mysql數(shù)據(jù)庫中數(shù)據(jù)的操作(CRUD),包括插入、修改、刪除數(shù)據(jù),還有查詢數(shù)據(jù),包括where、in、like、ifnull、與或非、order by、聚集函數(shù)等。
一、插入數(shù)據(jù)(insert)
1.插入數(shù)據(jù)的語法
- insert into 表名 (字段1,字段2,字段3) values (值1,值2,值3);
- insert into 表名 values (值1,值2,值3);
2.注意事項
- 插入的數(shù)據(jù)與字段類型必須是相同的
- 數(shù)據(jù)的大小范圍在字段范圍內(nèi)
- 值與字段一一對應(yīng)
- 字符串或者日期類型數(shù)據(jù)需要使用單引號
示例:
insert into user values (1,'meimei','1956‐1‐1','1957‐1‐1','HR',5000,'meimeimei','xx'); insert into user values (2,'小鳳','1996‐1‐1','2013‐1‐1','BOSS',15000,'mei','xx'); insert into user values (3,'聰聰','1993‐11‐11','2015‐09‐10','WORKER',500.0,'chou','yy'); insert into user values (4,'如花','1994‐1‐1','2013‐1‐1','BOSS',25000,'mei','xx'); insert into user values (5,'小蒼','1991‐1‐1','2014‐1‐1','BOSS',15000,'mei','xx'); insert into user values (6,'小澤','1986‐1‐1','2013‐1‐1','BOSS',15000,'mei','xx');
問題:若MySQL插入中文數(shù)據(jù)亂碼
解決:
1.先把MySQL服務(wù)停止。 2.找到MySQL安裝文件的my.ini的配置文件 [client] port=3306 [mysql] default‐character‐set=gbk 3.重啟MySQL服務(wù)
二、修改數(shù)據(jù)(update)
1.語法
update 表名 set 字段1=值,字段2=值 where 條件; where username = 'meimei';
2.有無where
如果沒有where條件語句,默認更新所有的數(shù)據(jù)。
如果有where條件,默認更新符合條件的記錄。
示例:
將所有員工薪水修改為5000元。 update user set salary = 5000; 將姓名為'聰聰'的員工薪水修改為3000元。 update user set salary = 3000 where username = '聰聰'; 將姓名為'小鳳'的員工薪水修改為4000元,job改為ccc。 update user set salary = 4000,job = 'ccc' where username = '小鳳'; 將如花的薪水在原有基礎(chǔ)上增加1000元。 update user set salary = salary+1000 where username = '如花';
三、刪除數(shù)據(jù)(delete)
1.語法
delete from 表名 where 條件;
2.有無where
如果沒有where條件,默認刪除所有的數(shù)據(jù)。
3. truncate 表名;刪除表中所有的數(shù)據(jù)。
delete from 表名; 也可以刪除所有數(shù)據(jù)。
- 區(qū)別: truncate先把你整個表刪除掉,默默創(chuàng)建一個空的表(和原來的表結(jié)構(gòu)是一樣的)。
- delete from 表名 一行一行的刪除。(使用它)
- 事物的概念:事物提交和事物回滾。
示例:
刪除表中名稱為'聰聰'的記錄。 delete from user where username = '聰聰'; 刪除表中所有記錄。 delete from user; drop table user;刪除數(shù)據(jù)
四、查詢數(shù)據(jù)(select)(重點)
1.基本的select語句
語法:
select * from 表名; ‐‐ 查詢所有列的記錄
select 字段1,字段2,字段3 from 表名; ‐‐ 查詢字段123的記錄
DISTINCT ‐‐ 去除重復的數(shù)據(jù)
示例:select distinct english from stu;
練習:
create database day15;
use day15;
create table stu(
id int,
name varchar(30),
math int,
english int,
chinese int
);insert into stu values (1,'美美',78,93,56); insert into stu values (2,'聰聰',18,13,16); insert into stu values (3,'小鳳',98,96,89); insert into stu values (4,'如花',90,100,46); insert into stu values (5,'歐陽鋒',74,93,56); insert into stu values (6,'吳彥祖',37,11,89); insert into stu values (7,'聰大',88,77,66); insert into stu values (8,'聰二',55,44,33);
2.查詢語句中使用運算和別名(數(shù)據(jù)庫中數(shù)據(jù)不變)
在所有學生分數(shù)上加10分特長分。 select name,(math+10) m,(english+10) e,(chinese+10) c from stu; 統(tǒng)計每個學生的總分。 select name,(math+english+chinese) 總 分 from stu; 使用別名表示學生分數(shù) select name,(math+english+chinese) 總 分 from stu;
3.使用where條件過濾
查詢姓名為聰聰?shù)膶W生成績 select name,math,chinese from stu where name = '聰聰'; 查詢英語成績大于90分的同學 select name,english from stu where english > 20; 查詢總分大于200分的所有同學 select name,math+english+chinese from stu where (math+english+chinese) > 200;
4.where子句中出現(xiàn)的運算(3個)
4.1 > < <= >= = <> 大于、小于、大于(小于)等于、不等于
4.2 in 表示范圍。
select * from stu where math = 18; 查詢出一條數(shù)據(jù) select * from stu where math in (78,18,99);
4.3 like 模糊查詢 ‐‐ 符合模糊的條件
select * from stu where name like '張_'; 姓張的名稱(只有兩個)的記錄 select * from stu where name like '張%'; 姓張的名稱(張飛 張翼德 張是是是冠希)的記錄。 select * from stu where name like '%張'; 末尾是張(聰聰張 XSDF張) select * from stu where name like '%張%'; 只要名稱中包含張
4.4 isnull ifnull nullif 判斷某一個字段記錄是否為空
ifnull:如果xxx為null,可替換成默認值;
4.5 and與 or或者 not非
查詢英語分數(shù)在 80-90之間的同學。 select * from stu where english >= 10 and english < 19; 查詢數(shù)學分數(shù)為89,90,91的同學。 select * from stu where math in (89,90,91); 查詢所有姓小的學生成績。 select * from stu where name like '小%'; 查詢數(shù)學分>80,語文分>80的同學。 select * from stu where math > 80 or chinese > 80;
總結(jié):select 列名(運算) from 表名(別名) where 條件(運算的符號);
5.order by 對查詢的結(jié)果進行排序
5.1排序的語法
select * from 表名 where 條件 order by 列名 升序/降序;
5.2升序和降序
- order by 列名 asc;(升序,默認值)
- order by 列名 desc;(降序)
5.3 order by 子句必須出現(xiàn)在select語句的末尾。 示例:
對數(shù)學成績排序后輸出。 select name,math from stu order by math desc; 對總分排序按從高到低的順序輸出 select name,(math+english+chinese) as total from stu order by total desc; 對姓聰?shù)膶W生成績按照英語進行降序排序,英語相同學員按照數(shù)學降序 select name,english,math from stu order by english desc,math desc; 對姓聰?shù)膶W生成績排序輸出 select name,(math+english+chinese) as total from stu where name like '聰%' order by total desc;
6. 聚集函數(shù) where,group by, having,order by
6.1聚集函數(shù):總計某一列數(shù)據(jù)總和。一列的個數(shù)。一列的平均數(shù)。一列中最大值和最小值。
6.2聚集函數(shù)來操作列的。
count ---計數(shù) sum ---求和 ifnull --判斷是否為空:語法:ifnul(xxx,0) 如果xxx為null,替換成0 avg ‐‐ 平均值 max ‐‐ 最大值 min ‐‐ 最小值
練習:
統(tǒng)計一個班級共有多少學生? select count(name) from stu; 統(tǒng)計數(shù)學成績大于90的學生有多少個? select count(math) from stu where math >= 90; 統(tǒng)計總分大于220的人數(shù)有多少? select count(*) from stu where math + english+chinese > 200; 統(tǒng)計一個班級數(shù)學總成績? select sum(math) from stu; 統(tǒng)計一個班級語文、英語、數(shù)學各科的總成績 select sum(math),sum(english),sum(chinese) from stu; 統(tǒng)計一個班級語文、英語、數(shù)學的成績總和 select sum(ifnull(math,0)+english+chinese) from stu; select sum(math) + sum(english) + sum(chinese)from stu; 編寫一條更新語句:update stu set math = null where id = 2;
到此這篇關(guān)于Mysql數(shù)據(jù)庫中數(shù)據(jù)的操作(CRUD)的文章就介紹到這了,更多相關(guān)mysql 數(shù)據(jù)crud內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL AUTO_INCREMENT 主鍵自增長的實現(xiàn)
本文主要介紹了MySQL AUTO_INCREMENT 主鍵自增長的實現(xiàn),每增加一條記錄,主鍵會自動以相同的步長進行增長,具有一定的參考價值,感興趣的可以了解一下2023-11-11
MySQL報錯Lost connection to MySQL server&n
在確保網(wǎng)絡(luò)沒有問題的情況下,服務(wù)器正常運行一段時間后,數(shù)據(jù)庫拋出了異常"Lost connection to MySQL server during query",本文將給大家介紹MySQL報錯Lost connection to MySQL server during query的解決方案,需要的朋友可以參考下2024-01-01
MySql如何使用not in實現(xiàn)優(yōu)化
這篇文章主要介紹了MySql如何使用not in實現(xiàn)優(yōu)化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
MySQL添加外鍵時報錯:1215 Cannot add the foreign key constraint的解決方法
大家都知道MySQL中經(jīng)常會需要創(chuàng)建父子表之間的約束,這個約束是需要建立在主外鍵基礎(chǔ)之上的,最近在MySQL添加外鍵時發(fā)現(xiàn)了一個報錯:1215 Cannot add the foreign key constraint,所以這篇文章就給大家介紹了如何解決在創(chuàng)建主外鍵約束過程中碰到的這個問題。2016-11-11
用批處理實現(xiàn)自動備份和清理mysql數(shù)據(jù)庫的代碼
有網(wǎng)友問我在win2003下如何自動備份MySQL數(shù)據(jù)庫,既然是自動備份,那肯定得寫腳本,當然我們也可以利用軟件實現(xiàn)2013-08-08

