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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

数据库

Mysql数据库常用命令,mysql速学实用教程。

發(fā)布時(shí)間:2025/3/15 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql数据库常用命令,mysql速学实用教程。 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

說(shuō)明:對(duì)mysql數(shù)據(jù)庫(kù)常用命令的整理
適用:mysql5.5+

一、Mysql的基本操作命令
查看所有數(shù)據(jù)庫(kù)

show databases;

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

use mysql;

顯示數(shù)據(jù)庫(kù)中的表

show tables;

創(chuàng)建數(shù)據(jù)庫(kù) 編碼為utf8

create database 數(shù)據(jù)庫(kù)英文名稱 charset utf8;

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

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

創(chuàng)建表 需先use數(shù)據(jù)庫(kù)庫(kù)名

use 表所在的數(shù)據(jù)庫(kù)名;

創(chuàng)建a表,包含字段sid

create table a(sid tinyint);

向表a插數(shù)據(jù)

insert into a set sid=1; insert into a set sid=2;

查詢表a的數(shù)據(jù)

select * from a;

刪除表a

drop table a;

創(chuàng)建新a表

create table a(sid tinyint,money decimal(5,2));

插入數(shù)據(jù)

insert into a set sid=1,money=288; insert into a set sid=1,money=18; insert into a set sid=1,money=99.235;

字段類(lèi)型說(shuō)明:
1.char(數(shù)字)字符串類(lèi)型,只能存多少個(gè)字符
2.char和varchar區(qū)別

查詢速度,char優(yōu)于varcharchar(5) 1個(gè)字符串,那么也是占5個(gè) 定長(zhǎng)存儲(chǔ)大小,varchar優(yōu)于charvarchar(5) 1個(gè)字符,就占一個(gè) 變長(zhǎng)

3.前導(dǎo)零 zerofill 偶爾會(huì)把類(lèi)型變正數(shù)范圍

tinyint(10) 存1位數(shù)字,會(huì)補(bǔ)充9個(gè)零

4.非負(fù) unsigned,把字段類(lèi)型變正數(shù)范圍

create table a(sid tinyint unsigned);

5 enum 枚舉 單選

create table a(sid tinyint unsigned,nickname char(20),sex enum('男','女'));

6.set 多選

create table stu(sid tinyint unsigned,nickname char(20)sex enum('男','女'),play set('籃球','排球','抖音','PHP') );

單條數(shù)據(jù)存入實(shí)例:

insert into a set sid=1,nickname ='chtml.cn',sex=1,hobby='籃球,排球,PHP';

二、Mysql的進(jìn)階操作命令
查看表結(jié)構(gòu):

desc stu;

default 字段默認(rèn)值,一般not null 和 default 和結(jié)合起來(lái)使用的

create table b(uid smallint unsigned not null default 0,nickname char(20) not null default '' );

unique 非重

create table b(uid int unsigned,author_num char(20) not null default '' unique,password char(32) not null default '' )

主鍵 primary key,自增 auto_increment

create table c(arc_id int unsigned primary key auto_increment,title char(200) not null default '',content text,click smallint unsigned not null default 0 );

刪除arc_id為2的數(shù)據(jù)

delete from c where arc_id=2;

復(fù)制b表為b_bak
1.不復(fù)制內(nèi)容

create table b_bak like b;

2.把b表的數(shù)據(jù)復(fù)制到b_bak

insert into b_bak select * from b;

3.創(chuàng)建nb表同時(shí)復(fù)制數(shù)據(jù)

create table nb_bak1 select * from b;

查詢所有

select * from arc;

查詢arc_id

select arc_id from arc;

查詢指定字段

select 字段名,字段名 from 表名;

別名 as

select 字段1,字段2 as 新字段2 from 表名;

where 代表?xiàng)l件

select * from 表名 where 字段id>1 and 字段2>10;

concat連接字符串

select concat(字段1,'-',字段2) as 新字段2 from 表名;

±-------------------------------------------------+
| 新字段2 |
±-------------------------------------------------+
| 字段1的內(nèi)容-字段2的內(nèi)容 |
±-------------------------------------------------+

查詢結(jié)構(gòu)與自身比較

select 字段1,字段1>50 as 新字段2 from 表名;

±------±–+
| 字段1| 新字段2 |
±------±–+
| 100| 1 |
| 30| 0 |
| 30| 0 |
±------±–+

把字段重復(fù)的值去掉

select distinct(字段) from 表名;

查找set類(lèi)型數(shù)據(jù)

create table a(sid int unsigned primary key auto_increment,nickname char(200) not null default '',hobby set('籃球','足球','PHP','抖音') );

查找hobby中的籃球
1.find_in_set() 不常用

select * from 表名 where find_in_set('籃球',hobby);

2 &1 &2 不推薦

select * from 表名 where hobby &1;

3.like 匹配 推薦

select * from 表名 where hobby like '%籃球%';

查詢字段 為null的數(shù)據(jù)

select * from 表 where 字段 is null;

查詢并賦值一個(gè)新的數(shù)據(jù)的if使用

select click,if(click>5,'多','少') as tips from 表名;

±------±----+
| click | tips |
±------±----+
| 10 | 多 |
| 1| 少 |
| 1 | 少 |
±------±----+

order 排序 【desc 降序 asc 升序】
創(chuàng)建bd表

create table bd(bid int unsigned primary key auto_increment,name char(100) not null default '',sort smallint unsigned not null default 0 );

插入測(cè)試數(shù)據(jù)

insert into bd set name='騰訊搜索',sort=9; insert into bd set name='360搜索',sort=10; insert into bd set name='google搜索',sort=9;

修改bid為1的數(shù)據(jù)中的sort變?yōu)?12

update bd set sort=112 where bid=1;

排序,升序

select * from bd order by sort asc;

降序

select * from bd order by sort desc;

limit 截取

1.limit 1 從0截取1條 select * from bd limit 1; 2.imit 1,2 從1截取2條 select * from bd limit 1,2;

between 10 and 20 【查找10到20之間的值,包括10和20】

select * from bd where sort between 10 and 20;

查詢10或者15的數(shù)據(jù)

select * from bd where sort in(10,15);

查找以特定字符“x”開(kāi)頭的數(shù)據(jù)

select * from 表名 where 字段名 like "x%";

查找包含特定字符“x”的數(shù)據(jù)

select * from 表名 where 字段名 like "%x%";

查找包含特定字符“x”結(jié)尾的數(shù)據(jù)

select * from 表名 where 字段名 like "%x";

包含特定字符“x”開(kāi)頭后面跟一個(gè)字符的數(shù)據(jù)

select * from 表名 where 字段名 like 'x1';

把字段從左邊截取一位字符

select left(字段,1) from 表名;

把字段從第二個(gè)位置截取一個(gè)字符串

select mid(字段,2,1) from 表名;

把字段從右邊截取一位字符

select right(字段,1) from 表名;

隨機(jī)查詢一條數(shù)據(jù)

select * from 表名 order by rand() limit 1;

設(shè)置字符集utf8

set names utf8;

查看字符集變量

show variables like "%character%";

要查看的選項(xiàng)

character_set_client | gbk //客戶端 character_set_connection | gbk //連接端 character_set_results | gbk //結(jié)果端

三、mysql批量操作、統(tǒng)計(jì)與分組
錄入多條數(shù)據(jù);

insert into 表名 (字段1,字段2) values ('第一條數(shù)據(jù)1','1'),('第二條數(shù)據(jù)2','2');

清空表的所有數(shù)據(jù) 【一般不使用,通常需要做好安全機(jī)制過(guò)濾這條命令】

truncate 表名;

字段數(shù)據(jù)為1的記錄,如果沒(méi)有就添加,如果有就替換;

replace into 表名 (字段,字段2,字段3) values (1,'原有數(shù)據(jù)','新數(shù)據(jù)');

修改 【where可不加,可以修改全部數(shù)據(jù),謹(jǐn)慎操作】

update 表名 set 字段='新的數(shù)據(jù)' where 字段=條件值;

刪除【where可不加,可以刪除全部數(shù)據(jù),謹(jǐn)慎操作】

delete from 表名 where 字段=條件值;

把a(bǔ)表改名為b【修改表的名字】

alter table a rename b;

change 命令可以 改名同時(shí)改類(lèi)型

把s改成sn,然后類(lèi)型改為char(150) alter table 表名 change s sn char(150) not null default '';

modify命令改類(lèi)型

alter table 表名 modify sn char(13) not null default '' first;

modify命令 修改sn為char(30)并且放在id字段的后面

alter table 表名 modify sn char(30) not null default '' after id;

add命令追加新字段

給表追加sx字段,并且在sn字段的后面 alter table 表名 add sx enum('1','2') not null default '1' after sn;

刪除字段

alter table 表名 drop 字段名;

增加表的主鍵

alter table 表名 add primary key(id);

增加主鍵字段的自增

alter table 表名 modify id int unsigned auto_increment;

主鍵刪除需要先刪自增

alter table 表名 modify id int unsigned;

然后再刪除主鍵

alter table 表名 drop primary key;

date類(lèi)型說(shuō)明:日期格式的數(shù)據(jù)存儲(chǔ)。如2022-7-10

mysql輸出現(xiàn)在的時(shí)間

select now();

統(tǒng)計(jì)表的數(shù)據(jù)總數(shù)

select count(*) from 表名;

查找字段中最小的數(shù)據(jù)

select min(字段名稱) from 表名;

查找字段中最大的數(shù)據(jù)

select max(字段名稱) from 表名;

取得字段總和

select sum(字段名) from 表名;

取得字段平均數(shù)

select avg(字段名) from 表名;

字段分組,篩選字段【group by 、 having組合使用】

select * from 表名 group by 字段名 having 字段名='篩選條件值';

end:Mysql多表查詢

查詢多個(gè)表簡(jiǎn)單命令 【笛卡爾積(無(wú)意義的)】

select * from 表a,表b;

加限制條的 【笛卡爾積(有意義的)】

select * from 表名a as a,表名b as c where a.id=c.id;

查詢指定字段

select id,字段2,字段3 from 表名a as a,表名b as c where a.id=c.id;

內(nèi)部連接 inner join

select * from 表名a as a join 表名b as c on a.id=c.id;

inner join 關(guān)聯(lián)之后的where的使用

select * from 表名a as a join 表名b as c on a.id=c.id where a.id=20;

右連接 right join 【右表有數(shù)據(jù)就會(huì)有數(shù)據(jù)】

select * from 表名a as a right join 表名b as c on a.id=c.id;

多表查詢命令說(shuō)明:

與單表查詢一直,在表連接后,拼接條件即可查詢。

總結(jié)

以上是生活随笔為你收集整理的Mysql数据库常用命令,mysql速学实用教程。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 91福利在线导航 | www视频在线观看免费 | 亚欧精品视频一区二区三区 | 亚洲操片 | 成人一区二区三区四区 | 欧美性猛交一区二区三区精品 | 最新三级网站 | 亚洲三区在线 | 日本少妇bb | 反差在线观看免费版全集完整版 | 日日射av| 少妇高潮大叫好爽喷水 | 色爽视频 | 亚洲丝袜一区 | 肥老熟妇伦子伦456视频 | 欧美三级视频 | 国产一级一区 | 性爱视频免费 | av电影一区二区 | 成人av小说 | 久久久精品国产免费爽爽爽 | 久久精品欧美一区 | caoprom在线视频 | 亚洲自拍偷拍网 | 无码内射中文字幕岛国片 | 一区二区三区在线看 | 成全世界免费高清观看 | 国产伦理自拍 | 91情侣视频| 国产精品亚洲欧美在线播放 | 金鱼妻日剧免费观看完整版全集 | 久久久久久久一区二区三区 | 亚洲小说图片区 | 337p嫩模大胆色肉噜噜噜 | 超碰超碰在线 | 国产系列在线观看 | 影音先锋蜜桃 | 国产日产精品一区 | 成人免费一区 | 亚洲欧美中日韩 | 欧美一a一片一级一片 | 欧美日韩亚洲国产综合 | 久草网视频在线观看 | 日本人妻换人妻毛片 | www.欧美色图 | 夜色资源网 | 在线资源av | 九九九九九精品 | 天天操天天射天天爽 | 国产精品久久久久久久一区探花 | 哈利波特3在线观看免费版英文版 | 国产一级精品毛片 | 亚洲图区欧美 | 天天插av| 秋霞在线观看秋 | 久久av一区二区三 | 婷婷色综合网 | 我爱avav色aⅴ爱avav | 久久久视频在线 | 日本视频免费在线 | av合集| 日韩操操操| 国产网站在线免费观看 | 中文字幕一区二区人妻视频 | 人妻精品一区一区三区蜜桃91 | 免费在线黄网 | 亚洲熟女乱综合一区二区三区 | jizz国产在线 | 一二三四国产精品 | 成人www视频 | 已满18岁免费观看电视连续剧 | 日韩毛片 | 亚洲视频在线观看一区二区 | 欧美a视频 | 国产一级二级av | 亚洲国产成人精品女人久久久 | 特级丰满少妇一级aaaa爱毛片 | 五月婷婷丁香花 | 一级片在线 | 综合久色| 少妇导航av| 日韩视频一区二区三区四区 | 亚洲加勒比在线 | 在线观看的免费 | 宅男噜噜噜 | 亚洲欧美一二三 | 亚洲蜜桃av| 综合久久亚洲 | 一级特级黄色片 | 男人日女人的网站 | 东北少妇高潮抽搐 | 森泽佳奈作品在线观看 | 精品国产系列 | 色窝窝无码一区二区三区成人网站 | 久国久产久精永久网页 | 激情五月色综合国产精品 | 国产精品一区二区视频 | 欧美美女性视频 | 久久久久久久久久久久国产 |