MySQL之資料庫的常用語句
阿新 • • 發佈:2019-01-13
Mysql資料庫的常用語句
一、常用資料庫的命令
1.show databases; 檢視所有的資料庫 2.create database test; 建立一個叫test的資料庫 3.drop database test;刪除一個叫test的資料庫 4.use test;選中庫 ,在建表之前必須要選擇資料庫 5.show tables; 在選中的資料庫之中檢視所有的表 6.create table 表名 (欄位1 型別, 欄位2 型別); 7.desc 表名;檢視所在的表的欄位 8.drop table 表名; 刪除表 9.show create databases 庫名;檢視建立庫的詳細資訊 10.show create table 表名; 檢視建立表的詳細資訊
二、修改表的命令
1.修改欄位型別 alter table 表名 modify 欄位 欄位型別;
2.新增新的欄位 alter table 表名 add 欄位 欄位型別
3.新增欄位並指定位置 alter table 表名 add 欄位 欄位型別 after 欄位;
4.刪除表字段 alter table 表名 drop 欄位名;
5.修改指定的欄位 alter table 表名 change 原欄位名字 新的欄位名字 欄位型別
三、對欄位的命令
1.增加資料(insert)3種方式 insert into 表名 values(值1,值2,...)(很少用) insert into 表名(欄位1,欄位2...) values(值1,值2,....);(較常用) insert into 表名(欄位1,欄位2...) values(值1,值2,....),(值1,值2,....),(值1,值2,....); 2.刪除資料(delete) delete from 表名 where 條件 注意:where 條件必須加,否則資料會被全部刪除 3.更新資料(update) update 表名 set欄位1 = 值1, 欄位2 = 值2 where 條件 3.篩選重複值的欄位 select distinct 欄位 from 表名 #注意:where 必須加,否則資料全部修改 4.查詢資料(select) 1.查詢表中的所有資料 select * from 表名 2.指定資料查詢 select 欄位 from 表名 3.集合 id [not] in(1,2)模糊查詢 :like '%a%'; 4.根據條件查詢出來的資料 select 欄位 from 表名 where 條件 (最常用的) where 條件後面跟的條件 關係:>,<,>=,<=,!= 邏輯:or, and 區間:id between 4 and 6 ;閉區間,包含邊界 5.結果集排序 1, 通過欄位來排序 例如 :select * from star orser by money desc, age asc; 6.限制結果集 select 欄位 from 表 order by 欄位 排序關鍵詞(desc | asc) 排序關鍵詞 desc 降序 asc 升序(預設) 2, 多欄位排序 select 欄位 from 表 order by 欄位1 desc |asc,...欄位n desc| asc; select 欄位 from 表 limit 數量; 例如:select sum(id) from star 8.分組 select * from 表名 limit 偏移量,數量 說明: 1.不寫偏移量的話就是預設的為0 2.實現分頁的時候必須寫偏移量 偏移量怎麼計算?: limit (n-1)*數量 ,數量 7.常用的統計函式 sum,avg,count,max,min 只分組:select * from 表 group by 欄位 例子: select count(sex) as re,sex from star group by sex having re > 3; 分組統計: select count(sex) from star group by sex; 分組後結果集的過濾