溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL常用語法

發(fā)布時間:2020-07-20 04:23:02 來源:網(wǎng)絡(luò) 閱讀:309 作者:P1no 欄目:數(shù)據(jù)庫

MySQL常用語法數(shù)據(jù)庫級別操作

#查看有哪些數(shù)據(jù)庫
show databases; 

#創(chuàng)建數(shù)據(jù)庫
create database 【數(shù)據(jù)庫名字】; 

#顯示數(shù)據(jù)庫的字符集
show create database 【數(shù)據(jù)庫名字】; 

#字符集不一致時導(dǎo)致數(shù)據(jù)庫亂碼的罪魁禍?zhǔn)?,使用help create database還可以看到校對規(guī)則。
create database 【數(shù)據(jù)庫】 default character set【字符集,如utf8,gbk】;

#刪除數(shù)據(jù)庫
drop database 【數(shù)據(jù)庫】;

#切換數(shù)據(jù)庫
use 【數(shù)據(jù)庫】;

#查看當(dāng)前數(shù)據(jù)庫
select database(); 

#查看有哪些表
show tables;

#顯示指定數(shù)據(jù)庫的表
show tables from 【數(shù)據(jù)庫】;



用戶管理設(shè)置密碼

#創(chuàng)建用戶
create user '用戶'@'主機(jī)域';

#設(shè)置密碼
set password for '用戶'@'主機(jī)域' = password('密碼');

#創(chuàng)建用戶
create user '用戶'@'主機(jī)域' identified by '密碼';

#刪除系統(tǒng)多余賬戶
drop user '用戶名'@'主機(jī)域'; 

#刪除指定用戶
delete from mysql.user where user='用戶' and host='主機(jī)域';

#刷新更新
flush privileges;



賦予收回權(quán)限

#授權(quán)
grant all on 【數(shù)據(jù)庫】.【*或表】 to '用戶'@'主機(jī)域';

#所有權(quán)限,可單獨給usage連接,主要select,create,update,insert,delete。
grant all privileges on 【數(shù)據(jù)庫】 to '用戶'@'主機(jī)域';

#查看該用戶的權(quán)限
show grants for 用戶@主機(jī)域;

#收回權(quán)限
revoke 【權(quán)限】 on 【數(shù)據(jù)庫】.【*或表】 from 用戶@主機(jī)域;



表操作

#新建表
create table test (字段1 類型(數(shù)) 參數(shù)1,2.. comment ‘中文注釋’,字段2 類型(數(shù)) 參數(shù)1,2..);

#類型:int 整數(shù),double 雙精度浮點型,char 定長字符串優(yōu)化效率,varchar 變長字符串,date 日期型
#參數(shù):primary key主鍵,not null不為空,auto_increment自動增長

#查看表的結(jié)構(gòu)
desc 【表】;

#顯示建立這個表的語句
show create table 【表】; 

#插入記錄
insert into 【表】(【字段1】,【字段2】) values(【值1】,【值2】), (【值1】,【值2】)…;

#若沒寫字段則按照表里面字段的順序來,可同時插入需多條,一次插入多條屬于優(yōu)化。



查詢記錄

select 【列名】 from 【表】 where 【字段】='【%可匹配任意】';

#查看表
select * from test; 

#查看前兩行
select * from test limit 2; 

#從第二行開始查接下來的兩行
select * from test limit 2,3;

#id降序【asc升序】排列再查前兩行
select * from test order by id desc limit 2; 

#范圍查詢
select * from test where id >2 and【or】 id<4; 

#多表查詢
select t1.id t1.name t2.age from t1,t2 where t1.name=t2.name and t1.name='zhangsan';



更新數(shù)據(jù)

#更新記錄:要跟where,不然就掛了
update 【表】 set 【列】=【值】 where 【列】='【值】';

#刪除記錄:要跟where,不然就掛了
delete from 【表名】 where id >3;

#清空數(shù)據(jù)
truncate table 【表】;

#插入字段
alter table 【表】 add 【字段名】 【類型參數(shù)】 after 【字段】;
add添加,change改變,drop刪除

#更改表名
rename table 【原表名】 to 【改名】;
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI