數據庫之MySQL(MySQL學習筆記)——數據表的增、刪、改、查

數據表的增、刪、改、查

  1. 數據表的增、刪、改
  • 往商品種類表中增加數據
insert into commoditytype (ct_id,ct_name) values (1,'玩具'),(2,'文具'),(3,'書籍');
Query OK,3 rows affected (0.00 sec)		//查詢成功,3行受到影響
Records:3	Duplicate:0		Warings:0		//記錄:3		副本:0		警告:0
  • 如果增加表中已有的內容
 insert into commoditytype (ct_id,ct_name) values (1,'玩具');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'		//錯誤:主鍵的重複條目'1'

增加數據

  • 刪除數據
delete from commoditytype where ct_id=3;
Query OK, 1 row affected (0.00 sec)

注意:刪除條件,否則將全部刪除

delete from commoditytype;
Query OK, 2 rows affected (0.01 sec)

刪除數據

  • 修改數據
update commoditytype set ct_name='假期' where ct_id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

注意:修改條件,否則將全部修改

update commoditytype set ct_name='假期';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

修改數據

  1. 數據表的單表查詢
  • select
    select:理解爲輸出 as :是,理解爲重命名 單引號(’’):表示值 反引號(``):表示字段名稱
    select
  • select語句操作
    注意:當有值爲null參加運算時,輸出結果也爲null。
    計算利潤
select c_id as 商品編號,c_name as 商品名稱,c_inprice as 商品進價,c_outprice as 商品售價,c_outprice-c_inprice as 利潤
 from commodity;

select簡單語句操作

  • distinct關鍵字
    作用:去掉重複數據
select distinct c_type from commodity;

distinct關鍵字

  • 使用限制條件查詢:
    (1)帶關係運算符和邏輯運算符的條件數據查詢;
    用關係運算符和邏輯運算符輸出進價在10-100之間的商品
select c_name,c_inprice
from commodity
where c_inprice>=10 and c_inprice<=100;

關係運算符和邏輯運算符
(2)帶BETWEEN AND關鍵字的條件查詢語句;
注意:between num1 and num2,包括num1和num2。
not between num1 and num2,不包括num1和num2。

用between and輸出書籍類型進價在10-100的商品,

select c_name,c_inprice
from commodity
where (c_inprice between 10 and 100) and c_type=3;

between and
用 not between and輸出書籍類型進價不在10-100之間的商品

select c_name,c_inprice
from commodity
where (c_inprice not between 10 and 100) and c_type=3;

not between and
(3)帶IS NULL關鍵字的條件查詢語句
is null:爲空值
not is null:不爲空值

輸出商品售價爲null的商品

select c_name,c_outprice
from commodity
where c_outprice is null;

is null
輸出商品售價不爲爲null的商品

select c_name,c_outprice
from commodity
where c_outprice is not null;

is not null
(4)帶IN關鍵字的條件查詢語句
注意:in:in裏面的數字之間是或關係。
not in:not in裏面的數字是且關係。

輸出商品進價爲10,20,30,40,50的商品

select c_name,c_inprice
from commodity
where c_inprice in (10,20,30,40,50);

in
輸出商品進價不爲10,20,30,40,50的商品

select c_name,c_inprice
from commodity
where c_inprice not in (10,20,30,40,50);

not in
(5)帶LIKE關鍵字的條件查詢語句
%表示一個或多個任意字符,_表示一個任意字符。
輸出商品名稱中帶有‘玩具’的商品
like

  • order by 關鍵字
select c_name,c_outprice from commodity order by c_outprice [asc|desc]

asc:默認排序,升序,如果有null值,出現在首部。
desc:降序,如果有null值,出現在尾部。

order by desc
null值
去除帶有null值

select c_name,c_outprice
from commodity
where c_outprice is not null
order by c_outprice desc;
  • limit關鍵字
    應用:得到排序後的第幾條數據。
    limit關鍵字應用
  • 聚合函數
    COUNT()函數:統計記錄數;
    AVG()函數:求平均值;
    SUM()函數:求和;
    MAX()函數:求最大數;
    MIN()函數:求最小數;
    要對分組查詢結果進行條件限制查詢不能使用where關鍵字,需要使用having關鍵字
    一般來說聚合函數和having關鍵字一起使用
select c_type,sum(c_inprice),avg(c_inprice),max(c_outprice)
from commodity
group by c_type having avg(c_inprice)>100;

聚合函數應用
count和其他聚合函數的區別:
當沒有結果的時候其他函數返回null,count返回0;

查看最大連接數
show variables like ‘%max_connections%’;
MySQL5.7的最大連接數:151
最大連接數
通常,mysql的最大連接數默認是100, 最大可以達到16384。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章