注:本文爲mysql基礎知識的總結,基礎點不少如果有些不足,還請自行搜索。持續更新mysql
1、mysql簡介sql
數據庫簡介數據庫
數據庫是計算機應用系統中的一種專門管理數據資源的系統函數
數據庫是一組通過計算機處理後的數據,存儲在多個文件中,而管理數據庫軟件被稱爲數據庫管理系統----DBMSui
而MYSQL ORACLE等就是數據庫管理系統 spa
mysql是一款數據庫管理軟件 無償使用命令行
sql設計
sql(結構化查詢語言) 數據庫管理系統經過sql語言來管理數據庫中的數據。sql語言是一種數據庫查詢和長鬚設計語言。其主要用於存取數據、查詢數據、更新數據和管理關係數據庫系統。其中包含如下幾種code
DDL語句:數據定義語言主要用於定義數據庫、表、視圖、索引和觸發器等。包括created、 alter和drop語句blog
DML語句:數據操縱語言主要用於插入數據、查詢數據、更新數據和刪除數據。包括insert語句、select語句、update語句和delete語句
DCL語句:數據控制語言主要用於控制用戶的訪問權限。其中包括grant語句和revoke語句。
2、mysql命令行操做
一、進入 mysql
mysql -u root -p
退出數據庫
mysql退出三種方法:
mysql > exit;
mysql > quit;
mysql > \q
二、查看全部數據庫
show databases;
三、建立數據庫
CREATE DATABASE 數據庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
使用數據庫
use feng;
四、顯示選擇數據庫全部表
show tables;
五、顯示某個表結構
desc table;
show column from table;
六、建立表
#基本語法 create table name( 列名 類型 是否爲空 默認值 自增 主鍵, 列名 類型 是否爲空 )ENGINE=InnoDB DEFAULT CHARSET=utf8 not null #不爲空 default num #默認值 auto_incrememt #自增 primary key #主鍵
七、刪除表
drop table 表名
八、清空表
delete from 表名 #表還在,數據清空 truncate table 表名
九、修改表
#添加列: alter table 表名 add 列名 類型 #刪除列: alter table 表名 drop column 列名 #修改列: alter table 表名 modify column 列名 alter table 表名 change 原列名 新列名 類型;
#添加主鍵 alter table 表名 add primary key(列名); #刪除主鍵 alter table 表名 drop primary key; alter table 表名 modify 列名 int,drop primary key; # 修改默認值: ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; # 刪除默認值: ALTER TABLE testalter_tbl ALTER i DROP DEFAULT; # 更改表名 rename table 原表名 to 新表名;
十、表內容的操做
插入內容
insert into 表名 (字段) values (信息)
刪除內容
delete from 表名字 [where 條件]
修改內容
update 表名 set 列名=表達式 where 條件 update 表名 set(列名)=(子查詢) where 條件
查找內容
select * from 表名 select * from 表名 where 條件
條件 #and 多個條件 select * from 表 where name='feng' and age>25 ; #between 區間 年齡在20-25 select * from 表 where age between 20 and 25; #in 多個元素中 select * from 表 in (22,21,23,25); #not 不在多個元素中 select * from 表 no in (20,21); #在子查詢結果中 select * from 表 in (select age from 表2);
通配符 #% %feng feng結尾的name select * from 表 where name like '%feng' #_ f_ f開頭的name select * from 表 where name like 'f_'
限制 select * from 表 limit 5, //前五行 select * from 表 limit 0,5; //從0開始的五行 select * from 表 limit 5 offset 2; //從 第二行開始的五行
排序 select * from 表 order by 列 asc //從小到大 select * from 表 order by 列 desc //從大到小 select * from 表 order by 列1 desc,列2 asc //列1 從大到小 列2 小到大
分組 select name from 表 group by name #根據time分組 select name,time from 表 group by name, time # 根據name,timselect name,time,id from 表 where id>10 group by name,time order by id desc #獲取name time id按照name time 分組從大道小排序
select name,age from 表 group by age having max(id)<2 //獲取name age按照age分組 獲取最大id<2的數據
一些函數
count(*)
sum (score) //表示和
max(score) //表示最大數
min(score) //表示最小數
having #表示前面查詢後結果,在處理用having 進行條件篩選
#:group by 必須在where以後, order by 以前
連表 select a.name,a.phone,b.name from a,b where a.xid= b.xid 有對應關係則顯示全部 select a.name,a.phone,b.name from a inner join b on a.xid=b.xid
有對應則顯示,若是b中沒有,則值爲null select a.name,a.phone,b.name from a left join b on a.xid=b.xid
有對應則顯示,若是a中沒有,則值爲null select a.name,a.phone,b.name from a right join b on a.xid=b.xid