MySQL

一 用戶

1. 用戶登錄

使用終端登錄

01登錄.png

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

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

注:
執(zhí)行命令時結(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)限:可以是一個列表,不同的權(quán)限使用逗號分割;所有權(quán)限使用all privileges
// 數(shù)據(jù)庫/表:指定的數(shù)據(jù)庫/表
// [identified by '密碼']:可選參數(shù),在為新用戶分配權(quán)限時設(shè)置密碼
// with max_queries_pre_hour:每小時最大查詢數(shù)量
// with max_connections_pre_hour:每小時最大連接數(shù)量
// with max_updates_pre_hour:每小時最大更新數(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ù)庫名
// 選擇一個特定的數(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 ('美國', '英國');
// 查找出生地在某個集合中的數(shù)據(jù)值,用操作符in;不在某個集合中用操作符not in

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

四 關(guān)系

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

// 將用戶和評論兩個表組織在一起
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. 三個表的關(guān)聯(lián)

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

select film_name, people_name, job from film, people, film_people 
// 從三個表中查找并顯示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)讀取三個數(shù)據(jù)表,and多條件,like匹配

select film_name, people_name, job from film, people, film_people 
// 從三個表中查找并顯示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)讀取三個數(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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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