mysql常用語句及用法

本篇文章主要介紹mysql常用語句,感興趣的朋友參考下,希望對(duì)大家有所幫助。

1.mysql的基本操作

? ? 1.開啟mysql數(shù)據(jù)庫服務(wù)
? ? ? 格式: net start mysql

? ? ? 如: net start mysql57

? ? 2.關(guān)閉mysql數(shù)據(jù)庫服務(wù)
? ? ? 格式: net stop mysql
? ? ? 如:? net stop mysql57
? ??
? ? 3.登陸mysql服務(wù)
? ? ? 格式: mysql -u root -p?
如:? ?mysql -u root -p? 執(zhí)行成功后,在輸入密碼,
??
? ?4. 查看當(dāng)前數(shù)據(jù)庫的版本xinxi
格式: select version();

? ?5.查看當(dāng)前時(shí)間
? ? ? ?格式: select now();

? ?6.退出,

? ? ?quit,exit

2.數(shù)據(jù)庫database的操作

? ? 1.查看當(dāng)前有哪些數(shù)據(jù)庫

? ? ?格式: show databases;

? ??

? ? 2.創(chuàng)建一個(gè)數(shù)據(jù)庫

? ? ?格式: create database 數(shù)據(jù)庫名 charset=utf8;

? ? ? ?例: create database wen charset=utf8;

? ? 3.刪除一個(gè)數(shù)據(jù)庫

? ? ? ?格式: drop database 數(shù)據(jù)庫名;

? ? ? ?例: drop database wen;

? ? 4.使用數(shù)據(jù)庫

? ? ? ?格式:use 數(shù)據(jù)庫名;

? ? ? ?例: use wen;

? ? 5.查看當(dāng)前使用數(shù)據(jù)庫

? ? ? 格式: select database();

3.表table的操作

? 1.查看當(dāng)前數(shù)據(jù)庫有哪些表

? ? ???格式: show tables;

? 2.創(chuàng)建一個(gè)表

? ? ???格式: create table 表名(字段名及類型);

? ? ? ? ? ?例: create table student(id int auto_increment primary key,name varchar(20) not null,age int not null,address varchar(20),gender bit default 1);

?3.查看表的結(jié)構(gòu)

? ? ? ??格式: desc 表名;

? ? ? ??例: desc student;

? ? ? ? 4.查看創(chuàng)建表的語句

? ? ? ??格式: show create table 表名;

? ? ? ? 5.刪除表

? ? ? ? ? 格式: drop table 表名;

? ? ? ? ? 例: drop table student;

??6.修改表

? ? ? ? ? ? ? 1.修改表的名字

??格式: rename table 舊表名 to 新表名;

??例:? rename table student to students;

? ? ? ??2.修改表的結(jié)構(gòu), add | drop | change

? ? ? ???1.add 添加一個(gè)新的字段

? ????格式: alter table 表名 add 字段名 類型;

? ????例:? alter table student add phone int;?

???2.change 修改

????格式: alter table 表名 change 舊字段名 新字段名 新字段類型;

? ? ??例: alter table student change phone number varchar(20);

?3.drop 刪除?

? ? ??格式: alter table 表名 drop 字段名;

? ? ??例: alter table student drop number;

4.數(shù)據(jù)的操作

? ?1.插入數(shù)據(jù)

1.插入全列數(shù)據(jù)?

格式: insert into 表名 values(值1,值2,….)

? ?注意:值得順序需要與表的結(jié)構(gòu)對(duì)應(yīng), 主健可以寫0  

? 例: insert into student values(0,”小明”,36,”北京朝陽區(qū)”,1);

? ? ?  2.缺省插入

? ? ?   格式:insert into 表名(字段名1,字段名2,….) values(值1,值2,….) 

    ? ? ?例: insert into student(name,age,address) values(“小東”,48,”深圳南山區(qū)”);

3.插入多條數(shù)據(jù)

? ?格式:? insert into 表名 values(值1,值2,….),(值1,值2,….),(值1,值2,..)

? ? ?例:? insert into student values(0,”小云”,45,”杭州”,0),(0,”小雨”,105,”俄羅斯”,0),(0,”小清”,99,”日本”,0);

? ?2.刪除數(shù)據(jù)

? ? ? 格式: delete from 表名 where 條件

? ? ? ? 例: delete from student where age = 105;

? ? delete? from student

? 不加條件表示全部刪除, 請(qǐng)謹(jǐn)慎使用

? ?3.修改數(shù)據(jù)

? ? ? 格式:update 表名 set 字段名1=值,字段名2=值,…? where 條件;

? ? ? ? ? ?update student set gender = 1 where name = “小青”;

? ?update student set age = 52,address=”深圳” where name = “小東”;

? ?4.查看所有數(shù)據(jù)

? ? ? 格式: select * from 表名;

? ? ? 例: select * from student;

5.查詢數(shù)據(jù)

? 1.查詢格式

? ? ? 1.查詢所有

? ? ? 格式: select * from 表名;

? ? ? 2.按條件查詢

? ? ? 格式: select 字段名1,字段名2,…? from 表名 where 條件;

? ? ? ? ?1.select后跟的是查詢結(jié)果需要顯示的字段名?

2. * 表示所有的字段名都要顯示

3. from 后跟的是表名,表示從那個(gè)表查

4. where 后跟的是篩選條件

5. 可以使用 字段名 as 別名? ?來給字段名取個(gè)別名

? ? ? 例: 顯示 name和age

? ? ? ? ?select name,age from student;

select name as “姓名” from student;

? 2.條件中的運(yùn)算

? ? ? 1.比較運(yùn)算符?

? ? ? ? ?> 大于

>= 大于等于

=? 等于

!= 不等于

格式: select 字段名1,字段名2,…? from 表名 where 字段名 比較運(yùn)算符? 值;

例: select * from student where age

? ??

? ? ?2. 邏輯運(yùn)算符

? ? ? ? and? 且

or? ?或者

not? ?非

? ? ? ?格式: select 字段名1,字段名2,…? from 表名 where 字段名 比較運(yùn)算符? 值 邏輯運(yùn)算符 字段名 比較運(yùn)算符? 值 …;

? ? ? ?例: select * from student where age 40;

? ? ? ? ? select * from student where not (age 40);

? ? 3.模糊運(yùn)算符 like

? ? ? ? % 表示多個(gè)任意字符

_? 表示一個(gè)任意字符

需求: 匹配所有姓小的人
?格式: select * from student where name like “小%”;
?格式: select * from student where name like “小_”;
?需求: 匹配名字中有 “小”字的
? ? ???select * from student where name like “%小%”;

? ??4.范圍查詢?
? ? ????in (多個(gè)值 ) 判斷一個(gè)值 是否是多個(gè)值中的一個(gè)
? ? ? ??between? 值1(包含)? and 值2(包含)? ?判斷一個(gè)值是否在 值1與值2之間
? ? ????格式: select 字段名1,字段名2,…? from 表名 where 字段名 范圍運(yùn)算符? 值;
? ? ???例: 找出? 25或者45或者80或者 90??
? ? ??? select * from student where age in (25,45,80,90);
? ???? ?需求:找出25 到 45之間的數(shù)據(jù)
? ? ? ??select * from student where age between 25 and 45;
? ? ? ?
? ??5. 空判斷
? ? ????is null 為空
? ? ? ??is not null 不為空
? ? ? ??格式: select 字段名1,字段名2,…? from 表名 where 字段名 is null;
? ? ? ??插入數(shù)據(jù): insert into student(name,age) values(“小·超”,60);
?例: select * from student where address is null;
? ???select * from student where address is not null;
?
??6.去除重復(fù)的值??
? ??格式:? select distinct 字段名 from 表名 where 條件;
????例: select distinct gender from student;
??
??7.聚合函數(shù)
? ???count(*)? ?求當(dāng)前結(jié)果總共有多少條數(shù)據(jù)
? ???sum(列名)? 求列名對(duì)應(yīng)列的 和
? ???avg(列名)? 求當(dāng)前列的平均值
? ???max(列名)? 求當(dāng)前列的最大值
? ???min(列名)? 求當(dāng)前列的最小值
? ???例: 求當(dāng)前表總共有多少條數(shù)據(jù)?
? ? ? ??select count(*) from student;
? ? ? ??求年齡最小的?
? ? ? ??select min(age) from student;

??8.分組? group by
???格式: select 字段名…? from 表名 where? 條件 group by? 字段名

? ?查看有多少種性別

? ?例: select gender from student? group by gender;

? ? ? ? ?需求:統(tǒng)計(jì) 男生 和? 女生 各有多少個(gè)
? ? ? ? ?select gender,count(*) from student group by gender;
? ? ? ? ?需求: 統(tǒng)計(jì)所有女生的個(gè)數(shù)?
? ? ? ? ?例: select gender,count(*) from student group by gender having gender = 1;
? ? ? ? ?where 查詢條件,? ?是先執(zhí)行的查詢條件
? ? ? ? ?having 查詢條件? ? 是在查詢結(jié)果的基礎(chǔ)上在查詢
? ?
?9. 排序
? ? ? ? 格式: select 字段名…? from 表名 where 條件 order by 字段名1,字段名2…
? ? ? 例: 年齡小到大
? ? ? ?select? * from student? order by age;
? ? ? 默認(rèn)是從小到大排列
? ? ? ?asc? 從小到大
? ? ? desc? 從大到小
? ? ? select? * from student? order by age asc;

10. 分頁??

? ? ? ?格式: select 字段名… from 表名 where 條件? limit 起始值,多少條數(shù)據(jù)
? ? ? ?起始值可以從 0 開始
? ? ? ?例: select * from student limit 0,3;? ??

相關(guān)推薦:

MySQL常用語句簡介

MySQL常用語句簡介

MySQL常用語句簡介

以上就是

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊5 分享