日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql mtq_MySQL基础知识 - osc_r3mtqivi的个人空间 - OSCHINA - 中文开源技术交流社区

發(fā)布時間:2025/3/20 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql mtq_MySQL基础知识 - osc_r3mtqivi的个人空间 - OSCHINA - 中文开源技术交流社区 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

01)查看當(dāng)前存在的全部數(shù)據(jù)庫

show databases;

02)創(chuàng)建數(shù)據(jù)庫

create database 數(shù)據(jù)庫名稱;

03)查看創(chuàng)建好的數(shù)據(jù)庫信息

show create database 數(shù)據(jù)庫名稱;

04)使用數(shù)據(jù)庫

use 數(shù)據(jù)庫名稱;

05)刪除數(shù)據(jù)庫

drop database 數(shù)據(jù)庫名稱;

06)創(chuàng)建表

create table 表名(

列名1 數(shù)據(jù)類型 primary key,

列名2 數(shù)據(jù)類型,

列名3 數(shù)據(jù)類型,

.........

列名n 數(shù)據(jù)類型

);

07)查看數(shù)據(jù)庫中的全部表

show tables;

08)查看表結(jié)構(gòu)

desc 表名;

09)插入語句(3種方式)

A)insert into 表名 values(列值1,列值2,.....列值n);

B)insert into 表名(列名1,列名2,...列名n) values(列值1,列值2,.....列值n);

C)insert into 表名 values(列值1,列值2,.....列值n),(列值1,列值2,.....列值n),.....(列值1,列值2,.....列值n);

insert into 表名(列名1,列名2,...列名n) values(列值1,列值2,.....列值n),(列值1,列值2,.....列值n),.....(列值1,列值2,.....列值n);

10)更新語句

update 表名 set 列名1=該列新值,列名2=該列新值,....列名n=該列新值 where 條件;

11)刪除表中的數(shù)據(jù)

delete from 表名 where 條件;

12)刪除表

drop table 表名;

13)查詢?nèi)苛袛?shù)據(jù)

select * from 表名;

14)查詢指定列數(shù)據(jù)

select 列名1,列名2,列名3,....列名n from 表名;

15)給列起別名(distinct)

A)select 列名1 as 別名1,列名2 as 別名2,列名3 as 別名3,....列名n as 別名n from 表名;

B)select 列名1 別名1,列名2 別名2,列名3 別名3,....列名n 別名n from 表名;

16)去掉重復(fù)的列值(去重)

select distinct 列名 from 表名;

17)限制查詢(limit)

select */列名 from 表名 limit 初始位置,行數(shù);

18)排序(order by)

select */列名 from 表名 order by 列名1 asc/desc,列名2 asc/desc;

19)條件查詢

A)普通情況

select */列名 from 表名 where 條件 order by 列名1 asc/desc,列名2 asc/desc;

B)特殊情況

select */列名 from 表名 where 列名 is null;

select */列名 from 表名 where 列名 is not null;

select */列名 from 表名 where 列名 between 初值 and 終值;

select */列名 from 表名 where not 列名 between 初值 and 終值;

select */列名 from 表名 where 列名 in(列值1,列值2,列值3.....);

select */列名 from 表名 where not 列名 in(列值1,列值2,列值3.....);

select */列名 from 表名 where 列名 like 條件;

select */列名 from 表名 where not 列名 like 條件;

20)聚合函數(shù)

count(*/列名) sum(列名) avg(列名) min(列名) max(列名)

21)分組查詢(group by)

select 列名/聚合函數(shù) from 表名 where 條件 group by 列名 order by 列名1/聚合函數(shù) asc/desc,列名2/聚合函數(shù) asc/desc;

22)having語句

select 列名/聚合函數(shù) from 表名 where 條件 group by 列名 having 條件?order by 列名1/聚合函數(shù) asc/desc,列名2/聚合函數(shù) asc/desc;

23)MySQL函數(shù)

(1)常用數(shù)值處理函數(shù)

(A)round(數(shù)值,位數(shù)) 四舍五入函數(shù)

(B)truncate(數(shù)值,位數(shù)) 截取函數(shù)

(C)rand(n) 隨機(jī)數(shù)函數(shù)

(D)sqrt(n) 平方根函數(shù)

(E)mod(n,m) 取余函數(shù)

(2)常用字符處理函數(shù)

(A)length(字符串/列名) 統(tǒng)計字符串/列值的長度

(B)trim(字符串/列名) 去掉字符串2端的空格

(C)substring(參數(shù)1,參數(shù)2,參數(shù)3)字符串截取函數(shù)

(D)reverse(字符串/列名) 字符串逆序函數(shù)

(E)concat(字符串/列名,字符串/列名..字符串/列名) 字符串拼接函數(shù)

(3)常用日期處理函數(shù)

(A)curdate() 獲取當(dāng)前系統(tǒng)日期

(B)curtime() 獲取當(dāng)前系統(tǒng)時間

(C)sysdate() 獲取當(dāng)前系統(tǒng)日期時間

(D)year(date) 獲取年份

(E)month(date) 獲取月份

24)約束

(1)主鍵約束(primary key)

(A)創(chuàng)建表時,添加主鍵約束

普通/復(fù)合主鍵

(B)修改表時,添加主鍵約束

alter table 表名 add primary key(列名1,列名2,...列名n);

(C)刪除主鍵約束

alter table 表名 drop primary key;

(2)唯一約束(unique)

(A)創(chuàng)建表時,添加唯一約束

普通

(B)修改表時,添加唯一約束

alter table 表名 add unique(列名1,列名2,....列名n);

(C)刪除唯一約束

I) show keys from 表名\G; 查找"key_name"

II) alter table 表名 drop index key_name;

(3)默認(rèn)值約束(default)

(A)創(chuàng)建表時,添加默認(rèn)值約束

普通

(B)修改表時,添加默認(rèn)值約束

alter table 表名 modify 列名 數(shù)據(jù)類型 default 默認(rèn)值;

(C)刪除默認(rèn)值約束

alter table 表名 modify 列名 數(shù)據(jù)類型;

(4)非空約束(not null)

(A)創(chuàng)建表時,添加非空約束

普通

(B)修改表時,添加非空約束

alter table 表名 modify 列名 數(shù)據(jù)類型 not null;

(C)刪除非空約束

alter table 表名 modify 列名 數(shù)據(jù)類型;

25)視圖(view)

(1)創(chuàng)建視圖

create view 視圖名稱 as 查詢語句;

(2)通過視圖查詢

(3)修改視圖

alter view 視圖名稱 as 查詢語句;

(4)通過視圖實現(xiàn)對表的操作

(5)刪除視圖

drop view 視圖名稱;

總結(jié)

以上是生活随笔為你收集整理的mysql mtq_MySQL基础知识 - osc_r3mtqivi的个人空间 - OSCHINA - 中文开源技术交流社区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。