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; 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'; |
查询jobinfo表,将符合条件的字段cost求和 统计20181010这天jobinfo表有多少条目 |