MySQL的入門級篇操作

一、數據庫篇

1.創建數據庫

create database 數據庫名;
// 例如創建數據庫 db_school 
create database db_school;

2.使用數據庫

use 數據庫名;
// 例如使用數據庫 db_school 
use db_school;

3.查看所有數據庫

show databases;

4.刪除某個數據庫

drop database 數據庫名;
// 例如刪除數據庫 db_school 
drop database db_school;

二、數據表篇

1.查詢所有的數據表

show tables;

2.創建數據表

create table 數據表名(
    字段名1 字段類型,
    字段名2 字段類型
    ...
)
-- 例如創建 student 表
create table student(
    id int(5) primary key,
    name varchar(20),
    birthday datetime
)

3.查詢數據

1)查看某個表的所有數據

select * from 表名;
-- 例如查看 student表 的所有數據
select * from student;

2)查看某個表的某些數據

select 字段1,字段2 from 表名;
-- 例如查看 student表 的id、name字段
select id,name from student;

4.添加數據

insert into 表名(字段1,字段2,字段3) values(值1,值2,值3);
-- 例如添加 student表 數據
insert into 表名(id,name,birthday) values(1001,'張三','1949-10-01');

5.修改數據

update 表名 set 字段名1 = 新值1,字段名2 = 新值2 where 字段名 = 值
-- 例如修改 student表 中id爲1001的數據
update student set name='李四',birthday='1949-10-02' where id =1001;

6.刪除數據

delete from 表名 where 字段名 = 值;
-- 例如刪除 student表 中id爲1001的數據
delete from student where id=1001;

三、用戶篇

1.查看所有用戶

select host,user,authentication_string from mysql.user;

2.創建用戶

 格式:create user "用戶名"@"host" identified by "密碼";

host = "localhost", 爲本地訪問,

host = "ip" ,爲指定ip訪問,

host = "%",爲任意ip訪問

-- 創建用戶,允許本地訪問
create user '用戶名'@'localhost' identified by '密碼';

-- 創建用戶,允許指定ip訪問
create user '用戶名'@'ip地址' identified by '密碼';

-- 創建用戶,允許任何ip訪問
create user '用戶名'@'%' identified by '密碼';

3.刪除用戶

格式:drop user '用戶名'@'host';

drop user '用戶名'@'localhost';

drop user '用戶名'@'ip地址';

drop user '用戶名'@'%';

4.爲用戶授權

格式:grant 權限 on 數據庫名.表名 to '用戶名'@'host' identified by '密碼';

* 表示所有,

host = "localhost", 爲本地訪問,

host = "ip" ,爲指定ip訪問,

host = "%",爲任意ip訪問

-- 爲用戶admin,指定school數據庫進行授權   "查詢"
grant SELECT on school.* to 'admin'@'%' identified by "123456";

-- 爲用戶admin,指定school數據庫裏的student表進行授權   "查詢"
grant SELECT on school.student to 'admin'@'%' identified by "123456";

......

-- 爲用戶admin賦多個權限   "查詢/添加/刪除/修改"
grant SELECT,INSERT,DELETE,UPDATE on school.* to '用戶名'@'%';

5.查看用戶的權限

格式:show grants for '用戶名'@'host';

* 表示所有,

host = "localhost", 爲本地訪問,

host = "ip" ,爲指定ip訪問,

host = "%",爲任意ip訪問

-- 查看用戶admin的所有權限
show grants for 'admin'@'%';

6.刪除用戶的權限

格式:revoke 權限 on 數據庫名.表名 from '用戶名'@'host';

-- 刪除用戶admin在school庫下student表的INSERT權限
revoke INSERT on school.student from 'admin'@'host';

 

關於mysql用戶權限的相關細節,可以參考這個博主的文章: https://www.cnblogs.com/Csir/p/7889953.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章