01.MYSql基本操作命令

1.啟動mysql之后 登陸數(shù)據(jù)庫服務(wù)器

mysql -uroot -p

2.創(chuàng)建數(shù)據(jù)庫(注意一定是以分號結(jié)尾)

1>.創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名字;
eg: create database first_db;

2>.創(chuàng)建數(shù)據(jù)庫的時候 指定字符集;
create database 數(shù)據(jù)庫名字 character set utf8;

3>.創(chuàng)建數(shù)據(jù)庫的時候 指定字符集和校對規(guī)則;
create database 數(shù)據(jù)庫名字 character set utf8 collate 校對規(guī)則;

3.查看數(shù)據(jù)庫(注意一定是以分號結(jié)尾)

1>.查看所有數(shù)據(jù)庫
show databases;

下面的三個數(shù)據(jù)庫不要去動(一定不要動)
information_schema
performance_schema
mysql

2>.查看數(shù)據(jù)庫的定義語句
show create database 數(shù)據(jù)庫名字; 
eg: show create database day01;  就是查看數(shù)據(jù)庫day01的創(chuàng)建語句

4.修改數(shù)據(jù)庫

1>.修改數(shù)據(jù)庫字符集
alter database 數(shù)據(jù)庫的名字 character set 字符集;

5.刪除數(shù)據(jù)庫

drop database 數(shù)據(jù)庫名字;

6.其他指令

切換數(shù)據(jù)庫:    use 數(shù)據(jù)庫名字;
查看當(dāng)前正在使用的數(shù)據(jù)庫:  select database();

7.表的操作

1>.創(chuàng)建:
create table 表名(
? 列名 列的類型(長度) 列的約束,
? 列名 列的類型 列的約束
? )

? 列的類型: char / varchar
? 列的約束:
? primary key 主鍵約束
? unique : 唯一約束
? not null 非空約束

create table bb_student(
    sid int primary key,
    sname varchar(31),
    sex int,
    age int
);

2>.查看所有的表

show tables;

3>.查看表的創(chuàng)建過程

show create table 表名;

4>.查看表結(jié)構(gòu)

desc 表名;

5>.刪除表

drop table 表名;

6>.表中列的操作alter(add, modify, change , drop)

添加列: alter table 表名 add 列名 列類型 列的約束;
修改列: alter table 表名 modify 列名 列類型 列的約束;
修改列名: alter table 表名 change 舊列名 新列名 新列類型 新列約束;
刪除列: alter table 表名 drop 列名;

7>.其它操作

修改表明(一般不要去操作): rename table 舊表名  to 新表名
修改字符集(一般不要去操作):alter table 表名 character set 字符集

?8>.表中數(shù)據(jù)的操作
插入:  insert into 表名(列名,列名) values(值1,值2),(值1,值2),(值1,值2);
? 刪除: delete from 表名 [where 條件]
eg: delete from 表明 where 列名=值
? 修改: update 表名 set 列名='值' ,列名='值' [where 條件];
? 查詢(中括號表示可選): select [distinct] [列名1,列名2] from 表名 [where 條件]
注意:where 中多個條件用and連接 類似的邏輯運算符: and or not
as關(guān)鍵字: 別名 (as關(guān)鍵字可以省略)
where條件后面:
(1)關(guān)系運算符: > >= < <= != <>
(2)--判斷某一列是否為空: is null is not null
(3)in 在某范圍內(nèi)
查出列中等于1,2,5的項目:select * from 表名 where in(1,2,5);
(4)between...and... : 范圍
(5)模糊查詢: like _ : 代表單個字符; %: 代表的是多個字符
比如查出列中含有asd的數(shù)據(jù):select * from 表名 where 列名 like ’%asd%‘;
查第二個字是a的數(shù)據(jù):select * from 表名 where 列名 like ’_a%‘;

排序: order by (asc 升序, desc 降序)
eg: select * from 表名 order by 列名 desc;
分組: group by
分組之后條件過濾: having
聚合函數(shù)(注意 where之后不能接聚合函數(shù)): sum() ,avg() , count() ,max(), min()
eg: select sum(列名) from 表名;

注意 having 關(guān)鍵字 可以接聚合函數(shù) 出現(xiàn)在分組之后
select 列名A,avg(列名B) from 表名 group by 列名A having avg(列名B)>60;

而where關(guān)鍵字 不可以接聚合函數(shù),出現(xiàn)在分組之前

8.基本操作流程

1>.登陸連接到mysql:mysql -uroot -p
2>.顯示數(shù)據(jù)庫:show databases;
3>.進(jìn)入需要操作的數(shù)據(jù)庫: use 數(shù)據(jù)庫名字;
4>.查看所有的表信息:show tables;
5>.展現(xiàn)表中內(nèi)容:select * form 表名;

9.重要(寫select的順序)

S F W G H O
select。from。where。group by。having。order by。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容