上海市住房與城鄉(xiāng)建設(shè)管理委員會網(wǎng)站網(wǎng)絡(luò)營銷咨詢公司
目錄
1.CRUD
2.增加數(shù)據(jù)
2.1創(chuàng)建數(shù)據(jù)
2.2插入數(shù)據(jù)
2.2.1單行插入
2.2.2多行插入
3.查找數(shù)據(jù)
3.1全列查詢
3.2指定列查詢
3.3查詢字段為表達(dá)式
3.3.1表達(dá)式不包含字段
3.3.2表達(dá)式包含一個(gè)字段
3.3.3表達(dá)式包含多個(gè)字段?
3.4起別名
3.5distinct(去重)
3.6order by(排序)
3.6.1某字段默認(rèn)排序
3.6.2某字段降序排序
3.6.3使用表達(dá)式及別名排序
3.6.4對多個(gè)字段進(jìn)行排序
3.7where(條件查詢)
3.7.1比較運(yùn)算符
3.7.2邏輯操作符
3.8limit(分頁)
4.修改數(shù)據(jù)
5.刪除數(shù)據(jù)
1.CRUD
CRUD就是增刪改查的幾個(gè)sql語句的首字母即create(創(chuàng)建)、查詢(retrieve)、更新(update)、刪除(delete)。
2.增加數(shù)據(jù)
2.1創(chuàng)建數(shù)據(jù)
創(chuàng)建一個(gè)數(shù)據(jù)庫,語法為:create database 數(shù)據(jù)庫名; 如創(chuàng)建一個(gè)名為test的數(shù)據(jù)庫:
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
當(dāng)最后一行出現(xiàn)了Query Ok字段時(shí),代表著這個(gè)數(shù)據(jù)庫的創(chuàng)建成功。?
創(chuàng)建一個(gè)表,語法為:create table 表名; 如創(chuàng)建一個(gè)名為student的表:
mysql> use test;
Database changed
mysql> create table student(-> id int,-> name varchar(20),-> price decimal-> );
Query OK, 0 rows affected (0.02 sec)
我們在創(chuàng)建表的時(shí),得先使用一個(gè)數(shù)據(jù)庫。創(chuàng)建表成功后,最后一行語句會提示Query Ok..語句。
2.2插入數(shù)據(jù)
2.2.1單行插入
當(dāng)我們創(chuàng)建表后,表內(nèi)是沒有信息的因此我們得插入一些數(shù)據(jù)來存儲。我們使用 insert into 表名 values(字段1,字段2,....); 來插入一些數(shù)據(jù),注意插入的數(shù)據(jù)應(yīng)當(dāng)與表的結(jié)構(gòu)相同。我們來往student表中插入一行信息:
mysql> insert into student values(1,'張三',20);
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1
我們可以看到,出現(xiàn)了ERROR(錯誤)。代表著張三這個(gè)字符串沒有引入utf-8格式,因此我們在創(chuàng)建數(shù)據(jù)庫時(shí),得先聲明utf8格式才能使數(shù)據(jù)的增添不發(fā)生錯誤,如下所示:
mysql> create database test charset utf8;
Query OK, 1 row affected (0.00 sec)
這樣我們在創(chuàng)建數(shù)據(jù)庫的時(shí)候同時(shí)申明了字符集為utf-8類型,這樣我們在創(chuàng)建表并新添數(shù)據(jù)的時(shí)候就不會出現(xiàn)錯誤。聲明字符集語法:charset 字符集類型。
mysql> use test;
Database changed
mysql> create table student(-> id int,-> name varchar(20),-> price decimal-> );
Query OK, 0 rows affected (0.02 sec)mysql> insert into student values(1,'張三',20);
Query OK, 1 row affected (0.00 sec)
我們可以看到最后插入一行數(shù)據(jù),提示Query OK了,這是單行插入操作,下面我們來看多行插入。
2.2.2多行插入
多行插入語法為:insert? into 表名(類型1,類型2,類型3,...) values (字段1,字段2,字段3,...),(字段1,字段2,字段3,...);
mysql> insert into student(id,name,price) values-> (2,'李四',24),-> (3,'王五',30);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
以上代碼為student又增添了兩行數(shù)據(jù),因此現(xiàn)在的student表中有三行數(shù)據(jù)。我們可以使用select * from student; 全列查詢查看student表結(jié)構(gòu)。
mysql> select * from student;
+------+------+-------+
| id | name | price |
+------+------+-------+
| 1 | 張三 | 20 |
| 2 | 李四 | 24 |
| 3 | 王五 | 30 |
+------+------+-------+
3 rows in set (0.00 sec)
當(dāng)然,以上的多行插入我把id、name、price這三個(gè)字段都增添了。你也可以任意增添這三個(gè)字段中的任意數(shù)量,如只增添id、name這兩個(gè)字段等等。?
3.查找數(shù)據(jù)
3.1全列查詢
全列查詢,即查詢整個(gè)表里面的數(shù)據(jù)。語法格式為:select * from 表名; 其中*是一個(gè)通配符代表著所有的數(shù)據(jù)。如查詢上文中student表中的所有數(shù)據(jù):
mysql> use test;
Database changed
mysql> select * from student;
+------+------+-------+
| id | name | price |
+------+------+-------+
| 1 | 張三 | 20 |
| 2 | 李四 | 24 |
| 3 | 王五 | 30 |
+------+------+-------+
3 rows in set (0.00 sec)
我們可以看到所有的數(shù)據(jù)都顯示出來了 ,這就是全列查詢。但注意,在我們實(shí)際開發(fā)過程中*號不得隨意使用。當(dāng)我們使用*號查詢一個(gè)龐大的數(shù)據(jù)表或者其他類型的數(shù)據(jù)時(shí),所有的內(nèi)存都被占用,此時(shí)數(shù)據(jù)庫就不會正常的更新或增加數(shù)據(jù)這樣是非常危險(xiǎn)的!因此,我們在自己電腦上嘗試即可,因?yàn)槲覀兊臄?shù)據(jù)庫是較小的大概率不會造成數(shù)據(jù)丟失這種風(fēng)險(xiǎn)。
3.2指定列查詢
在上面我們知道了*號是非常的危險(xiǎn),那么指定列查詢就比較安全了,我們可以直接查詢某一列的數(shù)據(jù),這樣內(nèi)存的占用量就很低,指定列查詢語法為:select 字段1,字段2 from 表名; 如我要查找student表中的id和name這兩列數(shù)據(jù):
mysql> select id,name from student;
+------+------+
| id | name |
+------+------+
| 1 | 張三 |
| 2 | 李四 |
| 3 | 王五 |
+------+------+
3 rows in set (0.00 sec)
我們可以看到, 指定列查詢非常的“人性化”,我們想要查詢那一列就查詢那一列數(shù)據(jù),這樣對內(nèi)存的占用量也會很低,推薦這種查詢方式。
3.3查詢字段為表達(dá)式
首先,我們創(chuàng)建一個(gè)成績表,并且插入兩行數(shù)據(jù):
mysql> create table grades(-> chinese int,-> math int,-> english int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into grades(chinese,math,english) values-> (78,98,59),-> (76,99,35);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
以上就很好的創(chuàng)建了一個(gè)成績表,并且這個(gè)表有兩行數(shù)據(jù)。那么創(chuàng)建表和插入表信息在上文中已經(jīng)講解到了,不熟練的伙伴可以再去看看。
3.3.1表達(dá)式不包含字段
表達(dá)式不包含字段的情況意為這個(gè)表達(dá)式中的任何信息不為表中的任何字段,如下標(biāo)中查詢一個(gè)名為10的字段:
mysql> select chinese,math,english,10 from grades;
+---------+------+---------+----+
| chinese | math | english | 10 |
+---------+------+---------+----+
| 78 | 98 | 59 | 10 |
| 76 | 99 | 35 | 10 |
+---------+------+---------+----+
2 rows in set (0.00 sec)
我們可以看到,這個(gè)表達(dá)式列數(shù)都為10。這樣的查詢就是表達(dá)式不包含字段的一個(gè)查詢,注意此處的字段是表中的每一列的列名。
3.3.2表達(dá)式包含一個(gè)字段
表達(dá)式中包含一個(gè)字段的情況,通常我們想使表中的某一列數(shù)據(jù)發(fā)生改變時(shí)會使用該操作。如我想使grades表中每個(gè)人的英語成績增加10分:
mysql> select chinese,math,english+10 from grades;
+---------+------+------------+
| chinese | math | english+10 |
+---------+------+------------+
| 78 | 98 | 69 |
| 76 | 99 | 45 |
+---------+------+------------+
2 rows in set (0.00 sec)
3.3.3表達(dá)式包含多個(gè)字段?
表達(dá)式包含多個(gè)字段,一般我們在求一個(gè)表中的所有數(shù)據(jù)的總和時(shí)會使用該查詢方式。如我要求grades表中所有數(shù)據(jù)的總和:
mysql> select chinese,math,english,chinese+math+english from grades;
+---------+------+---------+----------------------+
| chinese | math | english | chinese+math+english |
+---------+------+---------+----------------------+
| 78 | 98 | 59 | 235 |
| 76 | 99 | 35 | 210 |
+---------+------+---------+----------------------+
2 rows in set (0.00 sec)
我們可以看到,這樣的查詢的非常方便的唯一的缺點(diǎn)就是,列名太長了。因此我們又有一個(gè)sql語句可以將這個(gè)冗長的列名給起一個(gè)別名,來簡化一下。具體講解請看下方:
3.4起別名
起別名我們所用的語法為 表達(dá)式或列名 as 別名;如我把上方冗長的chinese+math+english起別名為sum:
mysql> select chinese,math,english,chinese+math+english as sum from grades;
+---------+------+---------+------+
| chinese | math | english | sum |
+---------+------+---------+------+
| 78 | 98 | 59 | 235 |
| 76 | 99 | 35 | 210 |
+---------+------+---------+------+
2 rows in set (0.00 sec)
當(dāng)然也可以給單獨(dú)的一列起別名:
mysql> select chinese as '中文' from grades;
+------+
| 中文 |
+------+
| 78 |
| 76 |
+------+
2 rows in set (0.00 sec)
這樣,我們在查詢數(shù)據(jù)的時(shí)候看一些字段就不會太別扭,這就是起別名操作。 注意,as可以省略但為了代碼的可讀性建議加上!
3.5distinct(去重)
創(chuàng)建一個(gè)students表:
mysql> create table students(-> id int,-> name varchar(20),-> chinese int,-> math int,-> english int);
Query OK, 0 rows affected (0.02 sec)
插入四行數(shù)據(jù):
mysql> insert into students(id,name,chinese,math,english) values-> (1,'孫悟空',69,89,47),-> (2,'沙悟凈',77,99,60),-> (3,'豬八戒',80,88,50),-> (4,'白龍馬',69,77,88);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
我們發(fā)現(xiàn)孫悟空和白龍馬的語文成績相同了,當(dāng)我要顯示語文成績并且每個(gè)成績各不相同時(shí)可以使用distinct來去重:
mysql> select distinct chinese from students;
+---------+
| chinese |
+---------+
| 69 |
| 77 |
| 80 |
+---------+
3 rows in set (0.01 sec)
我們可以看到,69只顯示了一次,這就是distinct在表中的作用:去掉重復(fù)的數(shù)據(jù)。需要注意的是,distinct只是在我們顯示的時(shí)候去掉了重復(fù)的數(shù)據(jù),在實(shí)際的數(shù)據(jù)庫中被去重的數(shù)據(jù)是未發(fā)生改變的!?
3.6order by(排序)
3.6.1某字段默認(rèn)排序
我們拿students這個(gè)表來進(jìn)行操作,如我要按照英語成績來進(jìn)行默認(rèn)排序:
mysql> select id,name,chinese,math,english from students order by english;
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| 1 | 孫悟空 | 69 | 89 | 47 |
| 3 | 豬八戒 | 80 | 88 | 50 |
| 2 | 沙悟凈 | 77 | 97 | 60 |
| 4 | 白龍馬 | 69 | 77 | 88 |
+------+--------+---------+------+---------+
4 rows in set (0.00 sec)
通過觀察,我們發(fā)現(xiàn)默認(rèn)排序是按照升序進(jìn)行排序的。那么默認(rèn)排序的語法為:order by 字段名;語當(dāng)然你也可以加上asc這個(gè)字段來強(qiáng)調(diào)你進(jìn)行的排序是升序的,增強(qiáng)代碼的可讀性。
3.6.2某字段降序排序
例如按照english成績降序進(jìn)行排序:
mysql> select id,name,chinese,math,english from students order by english desc;
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| 4 | 白龍馬 | 69 | 77 | 88 |
| 2 | 沙悟凈 | 77 | 97 | 60 |
| 3 | 豬八戒 | 80 | 88 | 50 |
| 1 | 孫悟空 | 69 | 89 | 47 |
+------+--------+---------+------+---------+
4 rows in set (0.00 sec)
觀察發(fā)現(xiàn),如果要進(jìn)行降序排序的話,我們只需要在字段名后面加上desc關(guān)鍵字即可,因此降序排序語法為:order by 字段名 desc;
3.6.3使用表達(dá)式及別名排序
使用表達(dá)式進(jìn)行查詢,查詢語數(shù)外三門成績的總和按照降序進(jìn)行排序:
mysql> select id,name,chinese + math + english as sum from students order by chinese + math + english desc;
+------+--------+------+
| id | name | sum |
+------+--------+------+
| 2 | 沙悟凈 | 234 |
| 4 | 白龍馬 | 234 |
| 3 | 豬八戒 | 218 |
| 1 | 孫悟空 | 205 |
+------+--------+------+
4 rows in set (0.00 sec)
?使用別名進(jìn)行查詢,查詢語數(shù)外三門成績的總和按照降序進(jìn)行排序:
mysql> select id,name,chinese + math + english as sum from students order by sum desc;
+------+--------+------+
| id | name | sum |
+------+--------+------+
| 2 | 沙悟凈 | 234 |
| 4 | 白龍馬 | 234 |
| 3 | 豬八戒 | 218 |
| 1 | 孫悟空 | 205 |
+------+--------+------+
4 rows in set (0.00 sec)
我們發(fā)現(xiàn)上述兩個(gè)查詢的結(jié)果是一樣的,因此大家如果以后有類型的操作可以使用其中任意一種方式進(jìn)行查詢。
3.6.4對多個(gè)字段進(jìn)行排序
例如,按照語文的降序,數(shù)學(xué)的升序進(jìn)行查詢:
mysql> select id,name,chinese,math,english from students order by chinese desc,math;
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| 3 | 豬八戒 | 80 | 88 | 50 |
| 2 | 沙悟凈 | 77 | 97 | 60 |
| 4 | 白龍馬 | 69 | 77 | 88 |
| 1 | 孫悟空 | 69 | 89 | 47 |
+------+--------+---------+------+---------+
4 rows in set (0.00 sec)
這樣的操作,適合混合排序,具體功能具體操作 。大家按照自己的需求選擇自己的sql語句。
3.7where(條件查詢)
條件查詢使用的語句是where,它的語法格式為:select 字段1,字段2,... where 查詢條件;
3.7.1比較運(yùn)算符
運(yùn)算符 | 說明 |
---|---|
>,>=,<,<= | 大于,大于等于,小于就,小于等于 |
= | 等于,NULL 不安全,例如NULL = NULL 的結(jié)果是NULL |
<=> | 等于,NULL 安全,例如 NULL <=> NULL 的結(jié)果是 TRUE(1) |
!=,<> | 不等于 |
BETWEEN .. ADN .. | 在..和..之間,前閉后閉 |
IN() | 在 |
IS NULL | 是空 |
IS NOT NULL | 不是空 |
LIKE | 模糊查詢,當(dāng)LIKE后面為%號時(shí)代表0個(gè)或多個(gè)字段,當(dāng)LIKE后面為_時(shí)根據(jù)_的個(gè)數(shù)來表示字段。 |
查找英語成績不及格的學(xué)生:
mysql> select id,name,chinese,math,english from students where english<60;
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| 1 | 孫悟空 | 69 | 89 | 47 |
| 3 | 豬八戒 | 80 | 88 | 50 |
+------+--------+---------+------+---------+
2 rows in set (0.00 sec)
查找英語成績在60和90之間的學(xué)生:?
mysql> select id,name from students where english between 60 and 90;
+------+--------+
| id | name |
+------+--------+
| 2 | 沙悟凈 |
| 4 | 白龍馬 |
+------+--------+
2 rows in set (0.01 sec)
查找姓孫的同學(xué):
mysql> select id,name from students where name like '孫%';
+------+--------+
| id | name |
+------+--------+
| 1 | 孫悟空 |
+------+--------+
1 row in set (0.00 sec)
查找姓孫且姓名長度為2的學(xué)生:
mysql> select id,name from students where name like'孫_';
Empty set (0.00 sec)
以上代碼,最后顯示Empty代表沒有符合這個(gè)字段的學(xué)生。那么上述表格中有許多操作都是差不太多的,因此博主在這里就舉三個(gè)例子,大家可以對照表格自行進(jìn)行測試。?
3.7.2邏輯操作符
運(yùn)算符 | 說明 |
---|---|
AND | 多個(gè)條件都必須為TRUE,結(jié)果才為TRUE |
OR | 任意一個(gè)條件為TRUE,結(jié)果為TRUE |
NOT | 條件為TRUE,結(jié)果為FALSE |
?查詢語文成績>=80且英語成績>=80的學(xué)生:
mysql> select id,name from students where chinese>= 80 and math>=80;
+------+--------+
| id | name |
+------+--------+
| 3 | 豬八戒 |
+------+--------+
1 row in set (0.00 sec)
查詢語文成績>80或則英語成績<60的學(xué)生:?
mysql> select id,name from students where chinese>80 or english<60;
+------+--------+
| id | name |
+------+--------+
| 1 | 孫悟空 |
| 3 | 豬八戒 |
+------+--------+
2 rows in set (0.00 sec)
查詢英語成績不及格的學(xué)生:
mysql> select id,name from students where not english>=60;
+------+--------+
| id | name |
+------+--------+
| 1 | 孫悟空 |
| 3 | 豬八戒 |
+------+--------+
2 rows in set (0.00 sec)
我們可以看到,邏輯操作符進(jìn)行查詢與我們的C、Java中的&&、||?這種操作符表達(dá)的效果是一致的,只不過MySQL中使用的是英文。
3.8limit(分頁)
分頁查詢是按照規(guī)定查詢相應(yīng)的數(shù)據(jù),這個(gè)規(guī)定是根據(jù)你的需求來決定的,也就是你想查詢哪一頁或者哪幾行的數(shù)據(jù)。它的語法格式是在指定列查詢基礎(chǔ)上加上limit語句,有三種格式:
-- 起始下標(biāo)為 0
-- 從 0 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 從 s 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 從 s 開始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
查詢students表的前三條記錄中的id和name信息,我之間在最后面加上limit 3即可:
mysql> select id,name from students limit 3;
+------+--------+
| id | name |
+------+--------+
| 1 | 孫悟空 |
| 2 | 沙悟凈 |
| 3 | 豬八戒 |
+------+--------+
3 rows in set (0.00 sec)
也可以指定從第幾行開始查詢,如我要從students表的第3行查詢,查詢兩行記錄:
mysql> select id,name from students limit 2,2;
+------+--------+
| id | name |
+------+--------+
| 3 | 豬八戒 |
| 4 | 白龍馬 |
+------+--------+
2 rows in set (0.00 sec)
我們看到達(dá)到了我所想要的效果,因此指定行數(shù)的分頁查詢語法為:limit n,s;其中s為起始位置,n為行數(shù)。?但這種方式不夠嚴(yán)謹(jǐn),正確的應(yīng)該是 limit n?offset s;其中s也為起始位置,n為行數(shù)。注意,行數(shù)是按照0為第一條數(shù)據(jù)往后自增的跟數(shù)組的下標(biāo)是一致的。
4.修改數(shù)據(jù)
修改數(shù)據(jù),使用的sql語句為update。它的語法為:update?表名 set 修改值 where 條件;如將students表中的豬八戒英語成績置為0:
mysql> update students set english = 0 where name = '豬八戒';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
我們再來通過全列查詢查看students表的所有信息:
我們可以看到,豬八戒的英語成績已經(jīng)置為0了。當(dāng)然,我們也可以通過表達(dá)式進(jìn)行修改數(shù)據(jù)。如將豬八戒的英語成績通過表達(dá)式增加100:
mysql> update students set english = english+100 where name = '豬八戒';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
我們可以看到通過表達(dá)式進(jìn)行修改也是可以的,但唯一要注意的是。如果我們要增加或者減少一個(gè)數(shù)據(jù)的話我們應(yīng)該寫成數(shù)據(jù) = 數(shù)據(jù)+值,不能寫成數(shù)據(jù)+ = 值同上述代碼中的chinese = chinese + 100;
5.刪除數(shù)據(jù)
刪除數(shù)據(jù)使用的sql語句為delete,它的語法格式為:delete from 表名 where 刪除目標(biāo);如將students表中的白龍馬信息給刪除掉:
mysql> delete from students where name = '白龍馬';
Query OK, 1 row affected (0.00 sec)
顯示students表:
我們可以看到,students表已經(jīng)剩下三條記錄了。當(dāng)然,我們也可以刪除整張表的信息也是通過delete來刪除:
Query OK, 3 rows affected (0.01 sec)mysql> select * from students;
Empty set (0.00 sec)
刪除students整張表信息后,我們在使用全列查詢顯示為空。注意,delete刪除表只是刪除表中所有的數(shù)據(jù)并不刪除表,而drop這個(gè)操作就比較危險(xiǎn)了它把表以及表的數(shù)據(jù)全部都給刪除了。因此我們在刪庫操作的時(shí)候一定要注意!
文章內(nèi)容比較豐富,大家下來了一定要多加練習(xí)。只有實(shí)現(xiàn)了這些操作,我們才能更好的掌握這些知識點(diǎn),另外在where條件查詢中的比較操作符沒有舉到的例子大家也可以自行測試一番。本期博文到這里就結(jié)束了,感謝你的耐心閱讀,如有收獲還請點(diǎn)個(gè)小小的關(guān)注。