mysql

数据库
层次型数据库
网状型数据库
关系型数据库/非关系数据库
关系型数据库:把复杂的数据库结构归类为二元关系(二维表格式)
常用的关系型数据库:mysql,oracle
非关系型数据库:没有固定的表结构,解决高并发,数据库压力
常用的非关系型数据库:memcachedb,redis,mongo DB
/etc/my.cnf 配置文件
(centos7)mysql安装
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum install mysql-community-server
systemctl restart mysqld
(centos6)
yum list installed | grep mysql检查是否已经安装MySQL和其依懒
wget http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm
rpm -ivh mysql57-community-server
yum install mysql-server
mysql -u root
set password for 'root'@'localhost'=password('wangwei');
给用户远程权限
grant all privileges on *.*to hailin@"%"identified by "wanuwei";
添加账号
grant all privileges on *.*to zhen@'localhost'identified by 'wangwei';
修改密码
update mysql.user set password=password('wangwei') where user='root';
创建数据库
create database wangwei;
删除数据库
drop database wangwei;
列出所有数据库
show databases;
选择指定数据库d
use hailin
查看表结构
describe hailin
修改数据库端口
/etc/my.cnf
port=
添加MySQL中数据库远程操作
use hailin
grant all privileges on *.*to hailin@"%"identified by "wangwei"
删除MySQL用户
drop user hailin;
存储引擎 :myISAM innoDB MEMORY MERGE NOB
数据库默认myISAM,myISAM有三种不同存储方式:静态表,动态表,压缩表(默认静态表)
myISAM扩展名(.frm .myD .myI)(myISAM后面的空格保存会被去掉)
查看数据库所有用户selsect user,host from mysql.user;
创建表列子:
create table mysql.hailin(class_no varchar(20),date_start date);
查看数据库字符集
show create database hailin;
查看表创建语句
show create table hailinG;
不管表是否存在,语句都会执行成功不会报错
drop table if exists hailin;
修改数据库字符集
alter database hailin character set gdk;
查看表结构
desc hailin;
显示所有列查看表数据
select * from hailinG;
指定那个一列查看表数据
select user,host from hailinG;
查看指定相同前缀或者后缀的表
show tables like '%_data';
查看用户密码
select host,user,authentication_string from mysql.user;(8.0以上版本)
修改表
修改列结构
alter table hailin add class int;(添加一个列)
alter table hailin drop class;(删除列)
alter table hailin modify class varchar(20);(修改列属性)
alter table hailin change class fenshu int;(修改指定一列的名字和定义)
alter table hailin character set utf8;(修改表字符集)
修改表名
rename table hailin to zheng;
rename table hailin to zheng,wangwei to hailin;(多个表名修改)
rename table hailin to mysql.zheng(跨数据库移动重命名表)
数据
insert into hailin (name,class_no) values ('xiaowang','gaowang1')
insert into hailin values (wangwei,mingtian,2019);(不指定插入的字节)
select * from hailin where fushu>=72;(查看成绩大于等于72)
delete from hailin where fushu=100;(删除分数等于100的数据)
update hailin set fenshu=100 where fenshu>=72(把分数大于等于72修改为100)
show variables like 'character_set%';(查看字符集变量)
校对规则
ci不区分大小写 cs区分大小写 bin字节比较
show collation like 'utf8%';(查看utf8字符集校对规则)
create table v3 (name varchar(20)) character set gbk collate gbk_chinese_ci;(创建表设置字符集和校对规则)
select * from cs order by name;(数据以校对规则排列查看);
create table hailin (hailin tinyint unsigned,name tinyint );(创建无字符的表)
tinyint 1个字节 有符号 最小-128,最大127;无符号 最小0,最大255
create table hailin (name tinyint(2) zerofill);(创建表显示最小宽度为2,只会影响小于显示宽度,前导零填充)
float单精度,默认精度为6位左右,CPU也起到影响作用
decimal 双精度 默认精度为16位左右,会四舍五入,支持zerofill,
create table hailin (a float(5,2),b decimal(8,2));(创建一个表a字段为8位,小数精度到2位
b字段8位,小数精度3位)
insert into hailin values (0.123E3,456.12E2);(0.123*10^3,456.12*10^2)
date
time(负数表示以前的时间)
datetime
timestamp
select a,b+0 from hailin;(b去掉符号)
create table z (a time);
insert into z values ('5 12:00:00')(表示5天12小时)

insert into z (name,time) values (www,12:00:00) 
utf8限制字符不能超过21845
latin1(这个字符集可以not null表空间达到最大)
varchar创建数类超过255,会用2个字节表示长度,1个表示null
所有的字段都是not null 才能创建表
create table hailin (name enum('female','male'));(创建一个表字段包含female,male)
create table hailin (name set('zhuqiu','lanqiu'));
select * from tmp_4 order by name ; 升序
select * from tmp_4 order by name desc; 降序
select * from tmp_4 limit 1; 限制查看记录的数量为1
select * from tmp_4 limit 0,2; 从第1条开始,显示两条(第一条为0)
select distinct class_date,class_id from tmp_4;去除重复
(select * from tmp_4 where class_id=6) union (select * from tmp_4 where sj_id=3);联合查询
create table tmp_4 (sd_id int primary key auto_increment,class_id int , foreign key(class_id) references tmp_3(class_id) on update cascade on delete set restrict );
union 会把重复结果过滤掉,union all 加all 就不会过滤掉重复结果
select class_name from tmp_0 where class_date=(select max(class_date) from tmp_0);子查询
in not in =any
select * from tmp_0 where exists (select * from tmp_1 where tmp_0.class_id=class_id);判断出跟其他表有相同记录的信息
select * from tmp_0 inner join tmp_1 on tmp_0.class_id=tmp_1.class_id;内连接
select tmp_0.class_name,tmp_1.class_name from tmp_0 left outer join tmp_1 on tmp_0.class_id=tmp_1.class_id;外连接
select * from tmp_0 cross join tmp_1;交叉连接
select * from tmp_0 inner join tmp_1 on tmp_0.class_id=tmp_1 and day > 15;
select * from tmp_0 as t0 inner join tmp_1 as t1 where/on t0.class_id=t1.class_id; 表取别名查询
select t0.class_name as t0_name ,t1.class_date as t1_date, t1.class_name as t1_name from tmp_0 as t0 inner join tmp_1 as t1 on t0_name=t1_name; 表和字段别名
select * from tmp_0 left join tmp_1 on tmp_0.class_id=tmp_1.class_id ;左连接
select * from tmp_0 right join tmp_1 on tmp_0.class_id=tmp_1.class_id; 右连接
select * from tmp_0 natural join tmp_1;自然连接
select * from tmp_0 as t0 right join tmp_1 as t1 on t0.class_name=t1.cla1.class_name;右连接
select * from tmp_0 natural join tmp_1;自然连接(natural left join,natural right join)
select * into outfile 'tmp' fields terminated by ' ' enclosed by 'x' lines terminated by ' ' starting by 'sj:' from tmp_1; 设置字符结尾,行结尾,行开头,保持记录到指定文件
load data infile 's' into table tmp_0; 导入记录
dumpfile 二进制数据备份
insert into tmp_0 set cla='十点十分';
insert into tmp_0 (class_id,class_name) values (3,'说的好') on duplicate key update class_name='说的话'; 主键冲突时可以判断直接修改插入
insert into tmp_1 (xs_name,class_name) select * from tmp_0;把查询的结果插入
replace into tmmp_1 values (hailin);主键冲突直接替换
delete from tmp_0 limit 10;
delete from tmp_0,tmp_1 using tmp_0 join tmp_1 on tmp_0.name_id=tmp_1.name_id where days=2;多表数据删除
truncate tmp_0; 初始化表
update tmp_0 join tmp_1 on tmp_0.class_name=tmp_1.class_name set xs_name='sdfs' ,name='sdfs' where class_id=1;多表更新
mysqldump -uroot -p tmp_name > /root/tmp.sql; 备份数据库
source /root/tmp.sql; 还原数据库
mysqldump -uroot -p tmp_name www /root/tmp.sql 备份表
source /root/tmp.sql 还原表
create view tmp_www as select class_id,class_name from www;创建视图表,指定表只能看那些字段
alter view tmp_mm (m1,m2,m3) as select * from mm;修改视图表字段
create view zheng as select mm.class_name,www.hh from mm cross join www; 任何查询语句都适合创建视图表
select c_name ,sum(days) from tmp_1 group by c_name;每个班级代课总天数,用班级分组
select c_name.days from tmp_1 group by c_name order by desc days;代课最多的教师
alter table class convert to character set utf8;修改整个表数据的字符集
update class set class_qian=class_qian-900;修改数据
set autocommit=0 关闭自动提交
commit; 成功就提交结果到数据库
rollback;失败就回滚到原来数据
start transaction开启事务 关闭自动提交
事务特性(ACID)原子性,一致性,隔离性,永久性
create trigger hai after update on class for each row update z set class_id=1;创建触发器,名字,事件,执行代码
create trigger hai after update on class for each row update z set class_id=class_id+(old_class_id-new_class_id);
begin开始 end结束
delimiter $$
create trigger hailin ofter insert on wwww for each row
begin update class set class_id=class_id+1;
update class set class_name=class_name+20;
end
$$ delimiter;创建触发器

select Host,User from mysql.user;查看所有用户

grant select,update on dcim.* to my@'%'identified by 'my'; 给my账号 数据库dcim所有表的查看和更新权限

查看数据库数据大小

进入information_schema 数据库

use information_schema

查看所以数据大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

查看指定数据库大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='databasename';

查看数据库中表大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='databasename' and table_name='tablename';

查看正在执行的进程

show processlist;

查看查询语句用了什么访问索引

explain select * from mysql.user ;

查看查询语句用了多少成本完成的

show status like "last_query_cost";

去除免密

UPDATE mysql.user SET authentication_string = PASSWORD('mypassword'), plugin = 'mysql_native_password' WHERE User = 'root' AND Host = 'localhost';
原文地址:https://www.cnblogs.com/yunweiweb/p/11243106.html