1. 程式人生 > >mysql 操作命令小結

mysql 操作命令小結

show databases;

create database mydb;

顯示資料庫

建立資料庫

use xxdatabse; 使用xxdatabse資料庫
show tables; 顯示某個資料庫下的表格
show create table student; 顯示student表結構
show tables like "student"; 顯示資料庫裡是否有student表,若無可以增加建立邏輯
select * from student; 顯示student表裡的全部學生資訊
drop table student; 刪除student表
insert into student(field1,field2...) values(?,?,...); 向student表插入一條資訊,值可以使用佔位符
update student set age=24 where id=3; 將student表中學號id=3的學生年齡更新為24
delete from student where id=2; 刪除學生表中id為2的學生資訊
truncate table student; 清空student表中的全部資訊
create table student(id VARCHAR(10) not NULL,age INYEGER, name VARCHAR(25), primary key(id)); 建立表student,主鍵為id
replace into student(id,name,age) values(1, '小紅', 18);  del+insert結合的原子操作,資料庫存在含主鍵的資訊就更新,否則插入資訊

USE mysql;
ALTER USER "root"@"localhost" IDENTIFIED WITH mysql_native_password BY "密碼";

FLUSH PRIVILEGES;

在命令視窗執行。解決navicat客戶端建立連結報“client does not support authentication protocol requested...”錯誤

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm

sudo yum install mysql-server

mysql

centos安裝mysql
use mysql;
update user set password=password('new password') where user='root';
flush privileges;
修改root密碼
mysql -u root -p;
use mysql;
create user 'test'@'localhost' identified by 'pwd';
flush privileges;
grant select privileges on testdb.* to test@localhost identified  by '1234';
管理員身份登陸
切換資料庫
建立使用者,其中localhost可指定具體IP
重新整理
授權test使用者擁有testdb資料庫下所有表的查詢許可權,連結密碼1234

UPDATE hdfsvcore SET totalCost= (totalCost - LEAST(vcorecost, memcost));

UPDATE hdfsvcore SET totalCost=ROUND(GREATEST(vcorecost, memcost),2);

UPDATE director SET dirSize= SUBSTRING(dirSize,1,LENGTH(dirSize)- 3);

UPDATE jobinfo SET memory= SUBSTRING(memory,1,LENGTH(memory)- 2) WHERE locate('GS', memory);

更新資料庫某欄位的值減去另一列的值;

四捨五入,保留2位小數;

字串擷取,將dirSize末尾三個字元截斷,SUBSTRING是從1開始計數,而不是0;

將jobinfo裡memory欄位含有“GS”字元去掉末尾兩字元,locate判斷是否包含某字串;

INSERT INTO dirtwo(date, levelDir, dirSize, upDir, nodeServer, ownner) SELECT date, levelDir, dirSize, upDir, nodeServer, ownner FROM dirtwo_copy; 將一個表的條目插入到另一個表

ALTER TABLE vcorecost ADD vmem FLOAT;

ALTER TABLE diruser MODIFY sumSize FLOAT;

ALTER TABLE director convert to character set  utf8;

ALTER TABLE director CHANGE levelDir  levelDir VARCHAR(225) CHARACTER SET utf8 NOT NULL;

增加表字段vmem;

將sumSize改成float型;

修改director表的編碼為utf-8;

將director表的levelDir的字元長度變成225,並設定其編碼為utf-8;

SELECT pro.clusterName,pro.userName,pro.productType,pro.dept,jobinfo.cost,

jobinfo.type,jobinfo.queueName,jobinfo.vcores,jobinfo.timeConsuming,jobinfo.memory,

jobinfo.`level`,jobinfo.resourceWasted FROM (SELECT distinct userName,clusterName, productType, dept FROM cluster_user) as pro INNER JOIN jobinfo ON jobinfo.date='20181009' AND jobinfo.clusterName=pro.clusterName AND jobinfo.ownner=pro.userName;

級聯查詢。取出興趣資料,先從cluster_user中拿到需要資料重新命名成臨時表pro,再連和jobinfo表做篩選,where條件是兩個表的date和clusterName以及ownner一致;

SELECT sum(cost) FROM jobinfo WHERE date='20181010' AND ownner='searchtool';

SELECT COUNT(*) FROM jobinfo WHERE date='20181010';
SELECT  * FROM jobinfo ORDER BY LENGTH(jobName) DESC LIMIT 10;
 

查詢jobinfo表,將符合條件的欄位cost求和

統計20181010這天jobinfo表有多少條目
選擇滿足條件的前10條資料,按jobName的長度排序