Mysql数据库常用命令,mysql速学实用教程。
說明:對mysql數據庫常用命令的整理
 適用:mysql5.5+
一、Mysql的基本操作命令
 查看所有數據庫
使用數據庫
use mysql;顯示數據庫中的表
show tables;創建數據庫 編碼為utf8
create database 數據庫英文名稱 charset utf8;刪除數據庫
drop database 數據庫英文名稱;創建表 需先use數據庫庫名
use 表所在的數據庫名;創建a表,包含字段sid
create table a(sid tinyint);向表a插數據
insert into a set sid=1; insert into a set sid=2;查詢表a的數據
select * from a;刪除表a
drop table a;創建新a表
create table a(sid tinyint,money decimal(5,2));插入數據
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;字段類型說明:
 1.char(數字)字符串類型,只能存多少個字符
 2.char和varchar區別
3.前導零 zerofill 偶爾會把類型變正數范圍
tinyint(10) 存1位數字,會補充9個零4.非負 unsigned,把字段類型變正數范圍
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') );單條數據存入實例:
insert into a set sid=1,nickname ='chtml.cn',sex=1,hobby='籃球,排球,PHP';二、Mysql的進階操作命令
 查看表結構:
default 字段默認值,一般not null 和 default 和結合起來使用的
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的數據
delete from c where arc_id=2;復制b表為b_bak
 1.不復制內容
2.把b表的數據復制到b_bak
insert into b_bak select * from b;3.創建nb表同時復制數據
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 代表條件
select * from 表名 where 字段id>1 and 字段2>10;concat連接字符串
select concat(字段1,'-',字段2) as 新字段2 from 表名;±-------------------------------------------------+
 | 新字段2 |
 ±-------------------------------------------------+
 | 字段1的內容-字段2的內容 |
 ±-------------------------------------------------+
查詢結構與自身比較
select 字段1,字段1>50 as 新字段2 from 表名;±------±–+
 | 字段1| 新字段2 |
 ±------±–+
 | 100| 1 |
 | 30| 0 |
 | 30| 0 |
 ±------±–+
把字段重復的值去掉
select distinct(字段) from 表名;查找set類型數據
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的數據
select * from 表 where 字段 is null;查詢并賦值一個新的數據的if使用
select click,if(click>5,'多','少') as tips from 表名;±------±----+
 | click | tips |
 ±------±----+
 | 10 | 多 |
 | 1| 少 |
 | 1 | 少 |
 ±------±----+
order 排序 【desc 降序 asc 升序】
 創建bd表
插入測試數據
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的數據中的sort變為112
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的數據
select * from bd where sort in(10,15);查找以特定字符“x”開頭的數據
select * from 表名 where 字段名 like "x%";查找包含特定字符“x”的數據
select * from 表名 where 字段名 like "%x%";查找包含特定字符“x”結尾的數據
select * from 表名 where 字段名 like "%x";包含特定字符“x”開頭后面跟一個字符的數據
select * from 表名 where 字段名 like 'x1';把字段從左邊截取一位字符
select left(字段,1) from 表名;把字段從第二個位置截取一個字符串
select mid(字段,2,1) from 表名;把字段從右邊截取一位字符
select right(字段,1) from 表名;隨機查詢一條數據
select * from 表名 order by rand() limit 1;設置字符集utf8
set names utf8;查看字符集變量
show variables like "%character%";要查看的選項
character_set_client | gbk //客戶端 character_set_connection | gbk //連接端 character_set_results | gbk //結果端三、mysql批量操作、統計與分組
 錄入多條數據;
清空表的所有數據 【一般不使用,通常需要做好安全機制過濾這條命令】
truncate 表名;字段數據為1的記錄,如果沒有就添加,如果有就替換;
replace into 表名 (字段,字段2,字段3) values (1,'原有數據','新數據');修改 【where可不加,可以修改全部數據,謹慎操作】
update 表名 set 字段='新的數據' where 字段=條件值;刪除【where可不加,可以刪除全部數據,謹慎操作】
delete from 表名 where 字段=條件值;把a表改名為b【修改表的名字】
alter table a rename b;change 命令可以 改名同時改類型
把s改成sn,然后類型改為char(150) alter table 表名 change s sn char(150) not null default '';modify命令改類型
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類型說明:日期格式的數據存儲。如2022-7-10
mysql輸出現在的時間
select now();統計表的數據總數
select count(*) from 表名;查找字段中最小的數據
select min(字段名稱) from 表名;查找字段中最大的數據
select max(字段名稱) from 表名;取得字段總和
select sum(字段名) from 表名;取得字段平均數
select avg(字段名) from 表名;字段分組,篩選字段【group by 、 having組合使用】
select * from 表名 group by 字段名 having 字段名='篩選條件值';end:Mysql多表查詢
查詢多個表簡單命令 【笛卡爾積(無意義的)】
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;內部連接 inner join
select * from 表名a as a join 表名b as c on a.id=c.id;inner join 關聯之后的where的使用
select * from 表名a as a join 表名b as c on a.id=c.id where a.id=20;右連接 right join 【右表有數據就會有數據】
select * from 表名a as a right join 表名b as c on a.id=c.id;多表查詢命令說明:
與單表查詢一直,在表連接后,拼接條件即可查詢。總結
以上是生活随笔為你收集整理的Mysql数据库常用命令,mysql速学实用教程。的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: java两个对象赋值_一起学Java(二
- 下一篇: div固定大小文字溢出自动缩小_Figm
