Mysql数据库常用命令,mysql速学实用教程。
說(shuō)明:對(duì)mysql數(shù)據(jù)庫(kù)常用命令的整理
適用:mysql5.5+
一、Mysql的基本操作命令
查看所有數(shù)據(jù)庫(kù)
使用數(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ū)別
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):
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)容
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() 不常用
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表
插入測(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ù);
清空表的所有數(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)題。
- 上一篇: java两个对象赋值_一起学Java(二
- 下一篇: div固定大小文字溢出自动缩小_Figm