1. 程式人生 > >mariadb_2 單表的增刪改查

mariadb_2 單表的增刪改查

最大值 例如 || 名稱 modify 一個 update del enter

命令關鍵字:

創建表

create

刪除表

drop

修改表的內容

update

修改表的結構

alter

刪除表中內容

delete

增加表中內容

insert

查詢表中內容

select

查詢語句選項:

消除重復行

distinct

模糊查詢

like

非連續範圍查詢

in

連續範圍查詢

between……and……

取反

not

為空

is null

不為空

is not null

排序

order by

升序

asc

降序

desc

統計總數

count

最大值

max

最小值

min

求和

sum

平均

avg

保留小數

round

分組

group by

顯示分組內容

group_concat

條件

having

分頁

limit

語句格式:
字段名稱可以寫具體的名稱,多個字段用逗號隔開,
也可以寫*,*表示所有字段
一、創建:
create  table 表名  add 字段名稱 數據類型 約束條件,字段名稱 數據類型 約束條件,...

二、修改:
1.修改字段的數據類型和約束條件
alter table 表名 modify 字段名稱 數據類型 約束條件

2.修改字段的名稱,數據類型和約束條件
alter table 表名 change 字段原名稱 字段新名稱 數據類型 約束條件

3.刪除字段
alter table 表名 drop 字段名稱

4.刪除表:
drop table 表名
 
5.增加表中內容
insert into 表名 values (值,值,...),值,值,...)

6.增加表中部分內容:
insert into 表名 (字段名,字段名,字段名...) values (值,值,值...),(值,值,...)

7.修改表的內容:
update table 表名 set 列1=值1,列2 =值2,....


三、刪除
1.物理刪除:
清空表:
delete  from  表名

刪除指定條件的記錄:
delete  from  表名  where  條件

2.邏輯刪除: 
alter table 表名 add 字段名 bit(n)     
該字段用來做邏輯判斷,bit(n)  n表示0和1有 2^n 種組合方式
delete  from  表名  where  條件 
該條件是用來指定要刪除的邏輯條件

四、查詢
1.查詢:
select 字段名稱 from 表名 where 條件

2.給字段起別名:
select 字段名稱 as 別名 from 表名 where 條件

3.給表起別名:
select 表的別名.字段名稱  from 表名 as 表的別名 where 條件

4.消除重復行:
select distinct 字段名稱 from 表名 where 條件

5.條件查詢:
比較運算符:>,<,>=,<=,=
select 字段名稱 from 表名 where 條件
select 字段名稱 from 表名 where 條件1 and 條件2
select 字段名稱 from 表名 where 條件1 && 條件2
select 字段名稱 from 表名 where 條件1 or 條件2
select 字段名稱 from 表名 where 條件1 || 條件2

6.模糊查詢:
select 字段名稱 from 表名 where 字段名 like ‘%_%‘(此處可以指定包含%,_,字符等的任意組合)

%:表示匹配0個或者多個字符
例如:‘%周%‘ 就是匹配包含 周 的所有
_:表示一個字符

7.in:表示非連續範圍內的查詢
select  字段名稱 from 表名 where 字段名 in (查詢的範圍)

8.between …… and :表示連續範圍的查詢
select  字段名稱 from 表名 where 字段名 between …… and ……

9、not:表示取反
select 字段名稱 from 表名 where 字段名 not 條件

10. 空和非空
select 字段名稱 from 表名 where 字段名 is null
select 字段名稱 from 表名 where 字段名 is not null

11.order by :排序
asc:升序排列,desc:降序排列 默認是升序

select 字段名稱 from 表名 order by 字段名 asc
select 字段名稱 from 表名 order by 字段名 desc

12.聚合函數

count:統計數量
select count(字段名稱) from 表名 where 條件

max:最大值
select max(字段名稱) from 表名 where 條件

min:最小值
select min(字段名稱) from 表名 where 條件

sum:求和
select sum(字段名稱) from 表名 where 條件

avg:平均
select avg(字段名稱) from 表名 where 條件

13.分組
select 字段名稱 from 表名 group by 字段名 (以字段名進行分組)
select 字段名稱,group_concat(字段名) from 表名 group by 字段名(分組並且列出組內內容)
select 字段名稱,group_concat(字段名) from 表名 group by 字段名 having 條件 
(分組並且列出組內符合條件的內容)

14.分頁:
select 字段名 from 表名  limit  起始位,顯示個數

  

mariadb_2 單表的增刪改查