1. 程式人生 > 其它 >四、SQL語言3(DQL+DCL)

四、SQL語言3(DQL+DCL)

6.DQL

  目的

    在mysql管理軟體中,可以通過sql語句中的dql語言來實現資料的 查詢操作

      如網際網路使用者查詢餘額,查詢裝備,查詢商品的操作。

  

  查看錶結構

    desc 表名;

  查詢所有列

    select * from 表名;

  查部分列

    select 列1,列2,列4 from 表名;  select id,name from t3;

  示例:

    建立素材

    建立database

    create database company;

    建立表employee5

    create table company.employee5(

id int primary key AUTO_INCREMENT not null,

name varchar(30) not null,

sex enum('male','female') default 'male' not null,

hire_date date not null,

post varchar(50) not null,

job_description varchar(100),

salary double(15,2) not null,

office int,

dep_id int

);

    查看錶結構

    desc company.employee5;

    插入資料

    insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id)values

('jack','male','20180202','instructor','teach',5000,501,100),

('tom','male','20180203','instructor','teach',5500,501,100),

('robin','male','20180202','instrcutor','teach',8000,501,100),

('alice','female','20180202','instructor','teach',7200,501,100),

('aofa','male','20180202','hr','hrcc',600,502,101),

('harry','male','20180202','hr',NULL,6000,502,101),

('emma','female','20180206','sale','salecc',20000,503,102),

('christine','female','20180205','sale','salecc',2200,503,102),

('zhuzhu','male','20180205','sale',NULL,2200,503,102),

('guoguo','male','20180205','sale','',2200,503,102);

    查看錶內容

    select * from employee5;

    檢視部分列

    select name,salary from employee5;  只看名字與其對應工資

    通過四則運算檢視部分列

    select name,salary,salary*12 from employee5;  看名字與其對應的月薪與年薪

    單條件查詢where

    select name from employee5 where post='hr';  只看職位是hr的名字

    多條件查詢and/or

    select * from employee5 where post='hr' and salary>1000;

    select name,salary from employee5 where salary=6000 or salary=8000;

    關鍵字between and  在什麼之間

    select name,salary from employee5 where salary between 5000 and 15000;

    關鍵字between and 可以加not  不在什麼之間

    select name,salary from employee5 where salary not between 5000 and 15000;

    關鍵字in集合查詢

    select name,salary form employee5 where salary in (4000, 5000, 6000, 9000);

    關鍵字is null

    select * from employee5 where salary job_description is null;

    關鍵字like模糊查詢

    select * from employee5 where name like 'al%';  萬用字元%代表多個任意字元,shell裡是*,mysql裡是%

    select * from employee5 where name like '%ice';

    select * from employee5 where name like 'a____'  萬用字元‘_’代表1個任意字元,shell裡是'?',mysql裡是'_'

    查詢排序

    升序(從低到高)

    select * from employee5 order by salary ASC;

    降序(從高到低)

    select * from employee5 order by salary DESC;

    工資最高前五

    select * from employee5 order by salary DESC limit 5;

 

7.DCL

  許可權級別

    Global level  所有庫,所有表的許可權

    Database level  某個資料庫中的所有表的許可權(常用)

    Table level  庫中的某個表的許可權

    Column level  表中的某個欄位 的許可權(基本不用)

  建立使用者

    show databases;

    use mysql;

    show tables;

    create user  user1@'localhost'  identified by 'User1@123';  建立使用者user1

    create user  user1@'192.168.153.%'  identified by 'User1@123'  授權給192.168.153之下的所有ip

  

  刪除使用者

    show databases;

    use mysql;

    show tables;

    drop user 'user1'@‘localhost’;  刪除使用者:user1@localhost

  修改使用者密碼

    示例1

    root修改自己密碼

    mysql -uuser1 -p'User1@123'  登入user1

    select user();  可以看到使用者是誰

    show databases;  可以發現該使用者沒有其他庫的許可權

    示例2

    set password=password('User1@12345');  重設密碼+密文儲存

    flush privileges;  重新整理許可權

    示例3

    mysql -uroot -p'dsfjlk12dhe-' password 'Root@123'

    

  丟失root使用者密碼

    vim /etc/my.cnf  開啟my.cnf

    [mysqld]

    skip-grants-tables  編輯條件登入不需要密碼

    :wq  儲存

    systemctl restart mysqld  重啟mysqld服務

    mysql -uroot  登入成功

    update mysql.user set authentication_string=password('Qianfeng@123') where user='root' and host='localhsot';  重設密碼+密文

    flush privileges;  重新整理許可權

    \q  退出mysql

    mysql -uroot  登入失敗

    mysql -uroot -p'Qianfeng@123'  登陸成功

    \q  退出mysql

    vim /etc/my.cnf  開啟my.cnf

    #skip-grants-tables  把之前編輯的註釋掉

    :wq  儲存

    systemctl restart mysqld  重啟mysqld服務

    mysql -uroot =p'Qianfeng@123'  登陸成功

  登入mysql

    mysql -P 3306 -u root -p‘Qianfeng@123’

    -P  mysql伺服器埠  預設3306

    -u  指定使用者名稱  預設root

    -p  指定登入密碼  預設為空密碼

    ss -anpt | grep mysql  可以檢視mysql的埠號