您好,登錄后才能下訂單哦!
這篇文章主要介紹“MYSQL表的全面分析”,在日常操作中,相信很多人在MYSQL表的全面分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MYSQL表的全面分析”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
CREATE TABLE tablename (column_name_1 column_type_1 constraints, column_name_2 column_type_2 constraints , ……)
column_name
是列的名字column_type
是列的數據類型contraints
是這個列的約束條件
mysql> create table orders (ordername varchar(10),createtime date,ordermoney decimal(10,2),ordernumber int(2)); Query OK, 0 rows affected (0.23 sec)
結構化定義:
mysql> desc orders; +-------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------+------+-----+---------+-------+ | ordername | varchar(10) | YES | | NULL | | | createtime | date | YES | | NULL | | | ordermoney | decimal(10,2) | YES | | NULL | | | ordernumber | int(2) | YES | | NULL | | +-------------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
表詳細定義:
查看詳細的表定義:
mysql> show create table orders \G; *************************** 1. row *************************** Table: orders Create Table: CREATE TABLE `orders` ( `ordername` varchar(10) DEFAULT NULL, `createtime` date DEFAULT NULL, `ordermoney` decimal(10,2) DEFAULT NULL, `ordernumber` int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified
由此可以看到表的 ENGINE
(存儲引擎)是InnoDB
CHARSET
(字符集)是Latin1
“\G
”選項的含義是使得記錄能夠按照字段豎著排列,對于內容比較長的記錄更易于顯示。
命令:
DROP TABLE tablename
刪除orders:
mysql> drop table orders -> ; Query OK, 0 rows affected (0.14 sec)
ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
例:修改表 orders
的 name
字段定義,將 varchar(10)
改為 varchar(20)
:
mysql> alter table orders modify ordername varchar(20); Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc orders; +-------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------+------+-----+---------+-------+ | ordername | varchar(20) | YES | | NULL | | | createtime | date | YES | | NULL | | | ordermoney | decimal(10,2) | YES | | NULL | | | ordernumber | int(2) | YES | | NULL | | +-------------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition [FIRST|AFTER col_name]
例:表 orders
上將ordernumber
修改為ordernumbers
mysql> alter table orders change column ordernumber ordernumbers int(4); Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc orders; +--------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------------+------+-----+---------+-------+ | ordername | varchar(20) | YES | | NULL | | | createtime | date | YES | | NULL | | | ordermoney | decimal(10,2) | YES | | NULL | | | ordernumbers | int(4) | YES | | NULL | | +--------------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
特別說明:change 和 modify 都可以修改表的定義,不同的是 change 后面需要寫兩次列名,不方便。但是 change 的優點是可以修改列名稱,modify 則不能。
ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name]
例:表 orders
上新增加字段 username
,類型為 varchar(3)
:
mysql> alter table orders add column username varchar(30); Query OK, 0 rows affected (0.39 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc orders; +-------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------+------+-----+---------+-------+ | ordername | varchar(20) | YES | | NULL | | | createtime | date | YES | | NULL | | | ordermoney | decimal(10,2) | YES | | NULL | | | ordernumber | int(2) | YES | | NULL | | | username | varchar(30) | YES | | NULL | | +-------------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
ALTER TABLE tablename DROP [COLUMN] col_name
例:表 orders
上刪除字段 username
:
mysql> alter table orders drop column username; Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc orders; +-------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------+------+-----+---------+-------+ | ordername | varchar(20) | YES | | NULL | | | createtime | date | YES | | NULL | | | ordermoney | decimal(10,2) | YES | | NULL | | | ordernumber | int(2) | YES | | NULL | | +-------------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
ALTER TABLE tablename RENAME [TO] new_tablename
例:表 orders
名字改為goodsorders
mysql> alter table orders rename goodsorders; Query OK, 0 rows affected (0.16 sec) mysql> desc orders; ERROR 1146 (42S02): Table 'ordermanage.orders' doesn't exist mysql> desc goodsorders; +--------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------------+------+-----+---------+-------+ | ordername | varchar(20) | YES | | NULL | | | createtime | date | YES | | NULL | | | ordermoney | decimal(10,2) | YES | | NULL | | | ordernumbers | int(4) | YES | | NULL | | +--------------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
插入(insert
)、查詢(select
)、更新(update
)、刪除(delete
)
INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn);
例:表 goodsorders
中插入一條記錄,ordername
為zhang
,createtime
為2021-05-12
,ordermoney
為100.00
,ordernumbers
為:1
mysql> insert into goodsorders (ordername,createtime,ordermoney,ordernumbers) values('zhang','2021-05-12',100.00,1); Query OK, 1 row affected (0.03 sec)
也可以省略(field1,field2,……fieldn)這一部分
mysql> insert into goodsorders values('zhang1','2021-05-12',1001.00,11); Query OK, 1 row affected (0.05 sec)
SELECT * FROM tablename [WHERE CONDITION]
例:查看goodsorders
中所有插入數據
mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-05-12 | 100.00 | 1 | | zhang1 | 2021-05-12 | 1001.00 | 11 | +-----------+------------+------------+--------------+ 2 rows in set (0.00 sec)
其中“*”表示要將所有的記錄都選出來
distinct
例:查詢非goodsorders
中非重復創建時間(createtime)的數據
mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-03-11 | 50.00 | 1 | | li | 2020-05-12 | 70.00 | 15 | | li | 2020-03-12 | 70.00 | 15 | | li | 2020-03-11 | 70.00 | 15 | | li | 2021-03-11 | 70.00 | 15 | +-----------+------------+------------+--------------+ 5 rows in set (0.00 sec) mysql> select distinct createtime from goodsorders; +------------+ | createtime | +------------+ | 2021-03-11 | | 2020-05-12 | | 2020-03-12 | | 2020-03-11 | +------------+ 4 rows in set (0.00 sec)
由此可以看到,將重復的一條時間數據2021-03-11
去掉了
where
后面的條件是一個字段的‘='比較,還可以使用>、<、>=、<=、!=等比較運算符;
多個條件之間還可以使用 or、and 等邏輯運算符進行多條件聯合查詢,
例:查詢非goodsorders
中 ordername='li'
并且createtime
為2020-03-11
mysql> select * from goodsorders where ordername='li'and createtime ='2020-03-11'; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | li | 2020-03-11 | 70.00 | 15 | +-----------+------------+------------+--------------+ 1 row in set (0.00 sec)
SELECT * FROM tablename [WHERE CONDITION] [ORDER BY field1 [DESC|ASC] , field2 [DESC|ASC],……fieldn [DESC|ASC]]
例:把 goodsorders
表中的記錄按照創建時間高低進行排序顯示
mysql> select * from goodsorders order by createtime; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | li | 2020-03-11 | 70.00 | 15 | | li | 2020-03-12 | 70.00 | 15 | | li | 2020-05-12 | 70.00 | 15 | | zhang | 2021-03-11 | 50.00 | 1 | | li | 2021-03-11 | 70.00 | 15 | +-----------+------------+------------+--------------+ 5 rows in set (0.01 sec)
SELECT ……[LIMIT offset_start,row_count]
offset_start
表示記錄的起始偏移量row_count
表示顯示的行數
例如1:顯示 goodsorders
表中按照 createtiem
排序后的前 3 條記錄:
mysql> select * from goodsorders order by createtime limit 3; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | li | 2020-03-11 | 70.00 | 15 | | li | 2020-03-12 | 70.00 | 15 | | li | 2020-05-12 | 70.00 | 15 | +-----------+------------+------------+--------------+ 3 rows in set (0.00 sec)
例如2:如果要顯示 goodsorders
表中按照 createtiem
排序后 從第二條記錄開始,顯示3條數據:
mysql> select * from goodsorders order by createtime limit 2,3; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | li | 2020-05-12 | 70.00 | 15 | | zhang | 2021-03-11 | 50.00 | 1 | | li | 2021-03-11 | 70.00 | 15 | +-----------+------------+------------+--------------+ 3 rows in set (0.00 sec)
SELECT [field1,field2,……fieldn] fun_name FROM tablename [WHERE where_contition] [GROUP BY field1,field2,……fieldn [WITH ROLLUP]] [HAVING where_contition]
參數說明:
1、fun_name
表示要做的聚合操作,也就是聚合函數,常用的有 sum
(求和)、count
(*)(記錄數)、max
(最大值)、min
(最小值)
2、GROUP BY
關鍵字表示要進行分類聚合的字段,比如要按照部門分類統計員工數量,部門就應該寫在 group
by 后面。
3、WITH ROLLUP
是可選語法,表明是否對分類聚合后的結果進行再匯總。
4、HAVING
關鍵字表示對分類后的結果再進行條件的過濾。
注意:having 和 where 的區別在于 having 是對聚合后的結果進行條件的過濾,而 where 是在聚合前就對記錄進行過濾,如果邏輯允許,我們盡可能用 where 先過濾記錄,這樣因為結果集減小,將對聚合的效率大大提高,最后再根據邏輯看是否用 having 進行再過濾。
例1:查詢統計goodsorders
表中,記錄總數
mysql> select count(1) from goodsorders; +----------+ | count(1) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec)
例2:在此基礎上,按照創建日期(createtime
)進行分組統計
mysql> select createtime,count(1) from goodsorders group by createtime; +------------+----------+ | createtime | count(1) | +------------+----------+ | 2020-03-11 | 1 | | 2020-03-12 | 1 | | 2020-05-12 | 1 | | 2021-03-11 | 2 | +------------+----------+ 4 rows in set (0.00 sec)
例3:在此基礎上,既要按照創建日期(cretetime
)進行分組統計,又要計算總數
mysql> select createtime,count(1) from goodsorders group by createtime with rollup; +------------+----------+ | createtime | count(1) | +------------+----------+ | 2020-03-11 | 1 | | 2020-03-12 | 1 | | 2020-05-12 | 1 | | 2021-03-11 | 2 | | NULL | 5 | +------------+----------+ 5 rows in set (0.02 sec)
最有一行,null
所展示的數字,就是總數
例4:按照創建日期(createtime
)進行分組統計,并且數量大于1
mysql> select createtime,count(1) from goodsorders group by createtime having count(1)>1; +------------+----------+ | createtime | count(1) | +------------+----------+ | 2021-03-11 | 2 | +------------+----------+ 1 row in set (0.00 sec)
例5:查詢goodsorders
表中,訂單金額(ordermoney
)的總額、最低額、最高額
mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-03-11 | 50.00 | 1 | | li | 2020-05-12 | 70.00 | 15 | | li | 2020-03-12 | 70.00 | 15 | | li | 2020-03-11 | 70.00 | 15 | | li | 2021-03-11 | 70.00 | 15 | +-----------+------------+------------+--------------+ 5 rows in set (0.00 sec) mysql> select sum(ordermoney),max(ordermoney),min(ordermoney) from goodsorders; +-----------------+-----------------+-----------------+ | sum(ordermoney) | max(ordermoney) | min(ordermoney) | +-----------------+-----------------+-----------------+ | 330.00 | 70.00 | 50.00 | +-----------------+-----------------+-----------------+ 1 row in set (0.02 sec)
1、左連接:包含所有的左邊表中的記錄甚至是右邊表中沒有和它匹配的記錄;關鍵指令:left join
2、右連接:包含所有的右邊表中的記錄甚至是左邊表中沒有和它匹配的記錄;關聯指令:right join
例1:現在我們又創建一張用戶表(member
),使用goodorders
進行左連接,查詢關聯的用戶表信息
mysql> select * from member; +------+------------+ | id | membername | +------+------------+ | 15 | zhang | | 1 | li | | 13 | liss | +------+------------+ 3 rows in set (0.00 sec) mysql> select * from goodsorders; +-----------+------------+------------+--------------+----------+ | ordername | createtime | ordermoney | ordernumbers | memberid | +-----------+------------+------------+--------------+----------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | | li | 2020-05-12 | 70.00 | 15 | 1 | | li | 2020-03-12 | 70.00 | 15 | 1 | | li | 2020-03-11 | 70.00 | 15 | 3 | | li | 2021-03-11 | 70.00 | 15 | 1 | +-----------+------------+------------+--------------+----------+ 5 rows in set (0.00 sec) mysql> select * from goodsorders left join member on goodsorders.memberid = member.id; +-----------+------------+------------+--------------+----------+------+------------+ | ordername | createtime | ordermoney | ordernumbers | memberid | id | membername | +-----------+------------+------------+--------------+----------+------+------------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | 15 | zhang | | li | 2020-05-12 | 70.00 | 15 | 1 | 1 | li | | li | 2020-03-12 | 70.00 | 15 | 1 | 1 | li | | li | 2021-03-11 | 70.00 | 15 | 1 | 1 | li | | li | 2020-03-11 | 70.00 | 15 | 3 | NULL | NULL | +-----------+------------+------------+--------------+----------+------+------------+ 5 rows in set (0.00 sec)
例2:member
和goodsorders
中數據不變,我們再來看一下右連接的查詢,以及結果:
mysql> select * from goodsorders right join member on goodsorders.memberid = member.id; +-----------+------------+------------+--------------+----------+------+------------+ | ordername | createtime | ordermoney | ordernumbers | memberid | id | membername | +-----------+------------+------------+--------------+----------+------+------------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | 15 | zhang | | li | 2020-05-12 | 70.00 | 15 | 1 | 1 | li | | li | 2020-03-12 | 70.00 | 15 | 1 | 1 | li | | li | 2021-03-11 | 70.00 | 15 | 1 | 1 | li | | NULL | NULL | NULL | NULL | NULL | 13 | liss | +-----------+------------+------------+--------------+----------+------+------------+ 5 rows in set (0.00 sec)
這里發生了翻轉,變為左側goodsorders
表中的一條數據為空了
主要包括 in
、not in
、=
、!=
、exists
、not exists
等
例:從goodsorders
表中查詢所有用戶在memeber表中的記錄
mysql> select * from member; +------+------------+ | id | membername | +------+------------+ | 15 | zhang | | 1 | li | | 13 | liss | +------+------------+ 3 rows in set (0.00 sec) mysql> select * from goodsorders; +-----------+------------+------------+--------------+----------+ | ordername | createtime | ordermoney | ordernumbers | memberid | +-----------+------------+------------+--------------+----------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | | li | 2020-05-12 | 70.00 | 15 | 1 | | li | 2020-03-12 | 70.00 | 15 | 1 | | li | 2020-03-11 | 70.00 | 15 | 3 | | li | 2021-03-11 | 70.00 | 15 | 1 | +-----------+------------+------------+--------------+----------+ 5 rows in set (0.00 sec) mysql> select * from goodsorders where memberid in(select id from member); +-----------+------------+------------+--------------+----------+ | ordername | createtime | ordermoney | ordernumbers | memberid | +-----------+------------+------------+--------------+----------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | | li | 2020-05-12 | 70.00 | 15 | 1 | | li | 2020-03-12 | 70.00 | 15 | 1 | | li | 2021-03-11 | 70.00 | 15 | 1 | +-----------+------------+------------+--------------+----------+ 4 rows in set (0.05 sec)
SELECT * FROM t1 UNION|UNION ALL SELECT * FROM t2 …… UNION|UNION ALL SELECT * FROM tn;
UNION
和 UNION ALL
的主要區別:
UNION ALL
是把結果集直接合并在一起,
UNION
是將UNION ALL
后的結果進行一次 DISTINCT
,去除重復記錄后的結果。
例1:將member
表和goodsorders
表中的用戶編號id
(memberid)的集合顯示出來
mysql> select memberid from goodsorders union all select id from member; +----------+ | memberid | +----------+ | 15 | | 1 | | 1 | | 3 | | 1 | | 15 | | 1 | | 13 | +----------+ 8 rows in set (0.00 sec)
例2:如果希望將上面的結果去掉重復記錄后顯示
mysql> select memberid from goodsorders union select id from member; +----------+ | memberid | +----------+ | 15 | | 1 | | 3 | | 13 | +----------+ 4 rows in set (0.00 sec)
UPDATE tablename SET field1=value1,field2.=value2,……fieldn=valuen [WHERE CONDITION]
例:將表 goodsorders
中ordername
為zhang
的訂單金額(ordermoney
)改為50
mysql> update goodsorders set ordermoney=50.00 where ordername='zhang'; Query OK, 1 row affected (0.09 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-05-12 | 50.00 | 1 | | zhang1 | 2021-05-12 | 1001.00 | 11 | +-----------+------------+------------+--------------+ 2 rows in set (0.00 sec)
更新時,如遇到錯誤代碼1175:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences
解決方法:
1、先進行狀體查詢:
show variables like 'SQL_SAFE_UPDATES';
2、執行下面的sql,關閉safe-updates模式:
SET SQL_SAFE_UPDATES = 0;
或者
SET SQL_SAFE_UPDATES = false;
DELETE FROM tablename [WHERE CONDITION]
例:將表 goodsorders
中ordername
為zhang1
的記錄全部刪除
mysql> delete from goodsorders where ordername = 'zhang1'; Query OK, 1 row affected (0.06 sec) mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-05-12 | 50.00 | 1 | +-----------+------------+------------+--------------+ 1 row in set (0.02 sec)
例:將表中的所有數據清空
mysql> select * from varc; +------+------+ | v | c | +------+------+ | abc | abc | +------+------+ 1 row in set (0.03 sec) mysql> truncate table varc; Query OK, 0 rows affected (0.25 sec) mysql> select * from varc; Empty set (0.00 sec)
DCL
語句主要是為了管理數據庫系統中的操作對象權限
例:創建一個數據庫用戶 user1
,初始密碼為123
,具有對 ordermanage
數據庫中所有表的 SELECT/INSERT
權限:
mysql> grant select,insert on ordermanage.* to 'user1'@'localhost' identified by '123'; Query OK, 0 rows affected, 1 warning (0.06 sec) mysql> exit Bye C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uuser1 -p123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 82 Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ordermanage | +--------------------+ 2 rows in set (0.00 sec)
在此基礎上,將此用戶(user1
)的insert
權限進行收回
mysql> revoke insert on ordermanage.* from 'user1'@'localhost'; Query OK, 0 rows affected (0.02 sec) mysql> exit Bye C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uuser1 -p123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 84 Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use ordermanage; Database changed mysql> insert into member values('11','ss'); ERROR 1142 (42000): INSERT command denied to user 'user1'@'localhost' for table 'member' mysql>
由此可以看出插入權限不足,插入失敗
到此,關于“MYSQL表的全面分析”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。