一、MySQL数据库mariadb的安装
关闭防火墙和selinux
#systemctl stop firewalld
安装服务端和客户端mariadb
# yum install mariadb-server mariadb -y
mariadb配置文件位置
# vi /etc/my.cnf
开启服务
# systemctl start mariadb
设置成开机自启动
# systemctl enable mariadb
添加编码格式为utf-8
重启服务,进入数据库
# systemctl restart mariadb
# mysql
二、MySQL数据库的使用
查看库
MariaDB [(none)]> show databases;
查找库存放的位置
MariaDB [(none)]> show global variables like "%datadir%";
Linux中mariadb数据库的默认存放位置
# cd /var/lib/mysql/
进入到某个库
MariaDB [(none)]> use mysql
查看当前在哪个库
MariaDB [mysql]> select database();
创建库
MariaDB [mysql]> create database aa;
删除库
mysql> drop database brdb_kjxy;
查看库中的表
MariaDB [aa]> show tables;
创建表
分开写
MariaDB [aa]> create table bb (
-> id int,
-> name varchar(10),
-> city varchar(10));
写在一行
MariaDB [aa]> create table cc (id int,name varchar(10),city varchar(10));
删除表
MariaDB [aa]> drop table cc;
cc表存在,则删除,不存在,则不执行
MariaDB [aa]> drop table if exists cc;
一次删除多张表,用逗号隔开
MariaDB [aa]> #drop table bb,cc;
查看表的结构
MariaDB [aa]> describe bb;
MariaDB [aa]> desc aa;
查看表的属性
MariaDB [aa]> show create table bb\G
修改表的属性,以修改编码为例
MariaDB [aa]> alter table bb character set gbk;
MariaDB [aa]> show create table bb\G
MariaDB [aa]> alter table bb charset=utf8;
MariaDB [aa]> show create table bb\G
修改系统的编码,
老版本的方法
#vi /etc/sysconfig/i18n
rh7
#vi /etc/locale.conf
# locale
添加列,删除列
MariaDB [aa]> alter table aa add phone char(10);
MariaDB [aa]> alter table aa drop phone;
调整插入顺序
MariaDB [aa]> alter table aa add phone char(10) first;
MariaDB [aa]> alter table aa add phone char(10) after name;
修改表的结构
MariaDB [aa]> alter table aa modify name varchar(10);
MariaDB [aa]> alter table aa change city city varchar(11);
MariaDB [aa]> alter table aa change city addr varchar(10);
修改表名
MariaDB [aa]> rename table aa to cc;
插入数据
insert into 表名 (列1,列2,列3,……) values (值1,值2,值3,……)
MariaDB [aa]> insert into cc (id,name,phone,addr) values (2,'bob',8888888,'beijing');
MariaDB [aa]> insert into cc (id,name) values (3,'mary');
MariaDB [aa]> insert into cc values (4,'zhangsan',6666666,'shanghai');
一次插入多个数据
MariaDB [aa]> insert into cc values
-> (5,'lisi',1111111,'hangzhou'),
-> (5,'wangwu',2222222,'nanjing');
查看表中所有数据
select * from 表名
MariaDB [aa]> select * from cc;
查看具体的列
select 列1,列2,…… from 表名;
MariaDB [aa]> select id,name from cc;
MariaDB [aa]> select id,name,addr from cc;
查看具体的某一行
MariaDB [aa]> select * from cc where id=3;
MariaDB [aa]> select * from cc where id>=4;
MariaDB [aa]> select * from cc where id>4;
MariaDB [aa]> select * from cc where id<=2;
and
MariaDB [aa]> select * from cc where id>=2 and id<=4;
or
MariaDB [aa]> select * from cc where id<=2 or id>=4;
in
MariaDB [aa]> select * from cc where id in (2,3,4);
MariaDB [aa]> select * from cc where id=2 or id=3 or id=4;
between
MariaDB [aa]> select * from cc where id between 2 and 4;
在数据库里面除了变量和数字之外,其他都得使用''引起来(单引号)
查看具体的某个数据
MariaDB [aa]> select * from cc where name='tom';
模糊匹配,使用通配符进行匹配的话,就不能使用= 而要使用like
MySQL里的两个通配符
_(下划线)表示一个字符
%(百分号)表示任意字符
MariaDB [aa]> select * from cc where name like 'm%';
查看MySQL里面所有的参数,通常用like和通配符过滤查看
MariaDB [aa]> show variables;
查看数据库存放位置
MariaDB [aa]> show variables like '%data%';
查看搜索引擎
MariaDB [aa]> show variables like '%engine%';
修改默认的搜索引擎去配置文件去改,要把下划线改成划线。
# vi /etc/my.cnf
把cc表复制给aa表(aa表原来没有)
MariaDB [aa]> create table aa as select * from cc;
把aa表数据插入到bb表(bb表存在)
MariaDB [aa]> insert into bb select * from aa;
清空表
MariaDB [aa]> delete from cc;
MariaDB [aa]> truncate table dd;
删除表中某个数据
delete from 表 where xxxx;
MariaDB [aa]> delete from bb where id>=4;
修改表中某个数据
update 表名 set 列=新值 where xxxx;
MariaDB [aa]> update bb set name='tom11111';
MariaDB [aa]> update bb set name='bob222' where id=1;
表连接
例如:
创建两张表,
一个是id和name表
一个是id和sale表
且某个字段进行统一
想知道lisi的工资是多少
设计表的时候,某个字段比如id一定要进行统一
MariaDB [aa]> select id from bb where name='lisi';
MariaDB [aa]> select * from cc where id=1;
可知lisi的工资为4000
子查询
MariaDB [aa]> select * from cc where id=(select id from bb where name='lisi');
笛卡尔积:
n,m
n*m
MariaDB [aa]> select * from bb join cc;
缺点,有许多不需要的数据
过滤(类似于提取公因式)来完成表连接
MariaDB [aa]> select * from bb inner join cc where bb.id=cc.id;
MariaDB [aa]> select * from bb join cc where bb.id=cc.id;
连接两张表,查询lisi的工资
MariaDB [aa]> select name,sale from bb join cc where bb.id=cc.id and name='lisi';
如果两张表的某列是相同的
MariaDB [aa]> select * from bb join cc using(id);
创建一个root用户,并设置密码
MariaDB [aa]> grant all on *.* to root@'%' identified by 'redhat';