MySQL

一 用戶

1. 用戶登錄

使用終端登錄

01登錄.png

查看數(shù)據(jù)庫

02查看數(shù)據(jù)庫.png

注:
執(zhí)行命令時(shí)結(jié)尾加分號(;)
退出登錄命令Ctrl+c / exit


2.創(chuàng)建用戶

create user '用戶名'@'主機(jī)名' identified by '密碼'
創(chuàng)建用戶.png

使用新用戶登錄

新用戶登錄.png
新用戶數(shù)據(jù)庫.png

3.分配權(quán)限-Grant

grant 權(quán)限 on 數(shù)據(jù)庫/表 to '用戶‘@'主機(jī)名' [identified by '密碼'] 
// 權(quán)限:可以是一個(gè)列表镰踏,不同的權(quán)限使用逗號分割;所有權(quán)限使用all privileges
// 數(shù)據(jù)庫/表:指定的數(shù)據(jù)庫/表
// [identified by '密碼']:可選參數(shù)郎哭,在為新用戶分配權(quán)限時(shí)設(shè)置密碼
// with max_queries_pre_hour:每小時(shí)最大查詢數(shù)量
// with max_connections_pre_hour:每小時(shí)最大連接數(shù)量
// with max_updates_pre_hour:每小時(shí)最大更新數(shù)量
// with max_user_connections:最大用戶連接數(shù)量
分配權(quán)限.png
flush privileges; 
// 刷新MySQL的系統(tǒng)權(quán)限相關(guān)表

4.顯示用戶列表

select user from mysql.user;
顯示用戶列表.png

5.顯示用戶權(quán)限

select 用戶, 主機(jī), 權(quán)限 form 數(shù)據(jù)庫.表
顯示用戶權(quán)限.png
show grants for '用戶'@'主機(jī)名'
顯示用戶權(quán)限2.png

6.吊銷用戶權(quán)限

revoke 更新, 刪除 on 數(shù)據(jù)庫.表 from '用戶名'@'主機(jī)名'
吊銷用戶權(quán)限.png

7. 重設(shè)密碼與刪除用戶

set password for '用戶名'@'主機(jī)名' = password('******');
// 重設(shè)密碼

drop user '用戶名'@'主機(jī)名';
// 刪除用戶
刪除用戶.png

二 數(shù)據(jù)庫

1. 創(chuàng)建著淆、使用、刪除數(shù)據(jù)庫

create database 數(shù)據(jù)庫
// 創(chuàng)建數(shù)據(jù)庫

drop database 數(shù)據(jù)庫
// 刪除數(shù)據(jù)庫

show databases
// 顯示全部數(shù)據(jù)庫

2. 創(chuàng)建數(shù)據(jù)表

use 數(shù)據(jù)庫名
// 選擇一個(gè)特定的數(shù)據(jù)庫

create table 數(shù)據(jù)表名(每欄數(shù)據(jù)名及屬性)
// 創(chuàng)建數(shù)據(jù)表名

show tables;
// 顯示全部數(shù)據(jù)表

describe 數(shù)據(jù)表名
// 顯示指定數(shù)據(jù)表內(nèi)容
創(chuàng)建數(shù)據(jù)表.png

3. 添加數(shù)據(jù)欄

alter table film add id INT(10) first;
// 在film數(shù)據(jù)表中添加id欄到第一欄

alter table film add film_content TEXT after film_name;
// 添加film_content欄在film_name后面
添加數(shù)據(jù)欄.png

設(shè)置主鍵

alter table film add PRIMARY KEY (id);
設(shè)置主鍵.png

4. 修改或刪除數(shù)據(jù)欄和數(shù)據(jù)表

alter table film change id film_id INT(10);
// 更改id欄名稱為film_id

alter table film rename to movie;
// 更改數(shù)據(jù)表名稱

alter table movie drop film_content;
// 刪除film_content數(shù)據(jù)欄

drop table movie;
// 刪除數(shù)據(jù)表

5. 重新創(chuàng)建數(shù)據(jù)庫與數(shù)據(jù)表

create database 數(shù)據(jù)表 charset=utf8;
// 設(shè)置數(shù)據(jù)庫默認(rèn)字符集

create table people() default charset=utf8;
// 設(shè)置數(shù)據(jù)表默認(rèn)字符集

people_id INT(10) unsigned not null auto_increment
// unsigned:整型
// not null:不可為null
// auto_increment:自增
// primary key(people_id):設(shè)置主鍵
重新創(chuàng)建數(shù)據(jù)表.png

三 查詢

1. 插入數(shù)據(jù)-insert

insert into 數(shù)據(jù)表 values(value1, value2, ...);
// 添加所有數(shù)據(jù)欄的值

insert into 數(shù)據(jù)表(attr1, attr3) values(value1, value3);
// 添加指定數(shù)據(jù)欄的值
插入數(shù)據(jù).png

2. 選擇數(shù)據(jù)-select

select * from 數(shù)據(jù)表
// 顯示所有數(shù)據(jù)欄的值

select column1, column2... from 數(shù)據(jù)表
// 顯示指定數(shù)據(jù)欄的值

select * from people where people_location = '美國';
// 顯示通過條件的數(shù)據(jù)值

select * from people order by people_birth desc;
// 顯示排序后的數(shù)據(jù)值

3. 更新與刪除數(shù)據(jù)-update&delete

update 表名稱 set 字段='值' where 字段='值'
// 更新數(shù)據(jù)

delete from 表名稱 where 字段='值'
更新與刪除數(shù)據(jù).png

4. 限制結(jié)果的數(shù)量與偏移-limit&offset

select * from people where people_location = '美國' limit 3;
// 設(shè)置最多顯示3行數(shù)據(jù)

select * from people limit 3 offset 1; 
// 設(shè)置最多顯示3行數(shù)據(jù)潜的,從開始處偏移一行

select * from people limit 1, 3
// 同上

5. 操作符

select * from people where people_birth > '1960-01-01';
// 查找出生年月在1960之后的數(shù)據(jù)值

select * from people where people_location in ('美國', '英國');
// 查找出生地在某個(gè)集合中的數(shù)據(jù)值骚揍,用操作符in;不在某個(gè)集合中用操作符not in

select * from people where people_name like ('李%');
// 查找匹配模式后的數(shù)據(jù)值

四 關(guān)系

1. 關(guān)聯(lián)-join

// 將用戶和評論兩個(gè)表組織在一起
select user_name, review_content from user, review where user.user_id = review.user_id;
// user, review == cross join 交叉關(guān)聯(lián)

select user_name, review_content from user inner join review on user.user_id = review.user_id;
// inner join 內(nèi)部關(guān)聯(lián)

select user_name, review_content from user inner join review on user.user_id = review.user_id where user.user_id = 1;
// 設(shè)置條件user.user_id = 1;

2. 左關(guān)聯(lián)

select user_name, review_content from user left join review on user.user_id = review.user_id;
// left join 左關(guān)聯(lián):把左側(cè)user_name的所有信息都顯示出來

3. 統(tǒng)計(jì)啰挪、平均信不、分組

select count(review_id) from review;
// 從review數(shù)據(jù)表里面統(tǒng)計(jì)review_id的總數(shù)

select film_id, count(review_id) from review group by film_id;
// 以film_id為基礎(chǔ),統(tǒng)計(jì)每一部電影的review總數(shù)

select film_id, avg(review_rate) from review group by film_id;
// 以film_id為基礎(chǔ)亡呵,計(jì)算每一部電影的review_rate平均數(shù)

select review.film_id, film.film_name, avg(review_rate) from review, film where review.film_id = film.film_id group by review.film_id;
// 以film_id為基礎(chǔ)抽活,以review.film_id=film.film_id為條件,列出對應(yīng)的電影的film_id和電影的film_name锰什,并計(jì)算出每一部電影的review_rate的平均數(shù)

4. 三個(gè)表的關(guān)聯(lián)

交叉關(guān)聯(lián)讀取三個(gè)數(shù)據(jù)表下硕,and多條件

select film_name, people_name, job from film, people, film_people 
// 從三個(gè)表中查找并顯示film_name,people_name,job之間的關(guān)系
where 
film_people.film_id = film.film_id 
// 條件一:表film_people和表film的film_id相同
and 
film_people.people_id = people.people_id;
// 條件二:表film_people和表people的people_id相同

交叉關(guān)聯(lián)讀取三個(gè)數(shù)據(jù)表丁逝,and多條件,like匹配

select film_name, people_name, job from film, people, film_people 
// 從三個(gè)表中查找并顯示film_name,people_name,job之間的關(guān)系
where 
film_people.film_id = film.film_id 
// 條件一:表film_people和表film的film_id相同
and 
film_people.people_id = people.people_id
// 條件二:表film_people和表people的people_id相同
and
film_name = '無間行者'
// 條件三:只顯示film_name='無間行者'的相關(guān)信息
and
people_name like '馬丁%'梭姓;
// 條件四:將搜索結(jié)果用“馬丁”來匹配

交叉關(guān)聯(lián)讀取三個(gè)數(shù)據(jù)表霜幼,總票房order排序、只顯示job='導(dǎo)演'

select sum(film_box) as total_box, people_name from film, people, 
// 將計(jì)算后的票房存到別名total_box
film_people 
where
film_people.film_id = film.film_id
and
film_people.people_id = people.people_id
and
job = '導(dǎo)演'
group by people_name
// 以people為基礎(chǔ)
order by total_box desc;
// 以票房我基礎(chǔ)降序排序
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末誉尖,一起剝皮案震驚了整個(gè)濱河市罪既,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌铡恕,老刑警劉巖琢感,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異探熔,居然都是意外死亡猩谊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門祭刚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牌捷,“玉大人,你說我怎么就攤上這事涡驮“瞪” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵捉捅,是天一觀的道長撤防。 經(jīng)常有香客問我,道長棒口,這世上最難降的妖魔是什么寄月? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮无牵,結(jié)果婚禮上漾肮,老公的妹妹穿的比我還像新娘。我一直安慰自己茎毁,他們只是感情好克懊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著七蜘,像睡著了一般谭溉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上橡卤,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天扮念,我揣著相機(jī)與錄音,去河邊找鬼碧库。 笑死柜与,一個(gè)胖子當(dāng)著我的面吹牛巧勤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播旅挤,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼踢关,長吁一口氣:“原來是場噩夢啊……” “哼伞鲫!你這毒婦竟也來了粘茄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤秕脓,失蹤者是張志新(化名)和其女友劉穎柒瓣,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吠架,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡芙贫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了傍药。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磺平。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖拐辽,靈堂內(nèi)的尸體忽然破棺而出拣挪,到底是詐尸還是另有隱情,我是刑警寧澤俱诸,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布菠劝,位于F島的核電站,受9級特大地震影響睁搭,放射性物質(zhì)發(fā)生泄漏赶诊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一园骆、第九天 我趴在偏房一處隱蔽的房頂上張望舔痪。 院中可真熱鬧,春花似錦锌唾、人聲如沸辙喂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽巍耗。三九已至,卻和暖如春渐排,著一層夾襖步出監(jiān)牢的瞬間炬太,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工驯耻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亲族,地道東北人炒考。 一個(gè)月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像霎迫,于是被迫代替她去往敵國和親斋枢。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容