关闭防火墙和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


查看库

MariaDB [(none)]> show databases;

查找库存放的位置

show global variables like "%datadir%";

库的存放位置

# cd /var/lib/mysql/

进入到某个库

MariaDB [(none)]> use mysql

查看当前在哪个库

MariaDB [mysql]> select database();

创建库

MariaDB [mysql]> create database aa;

查看库中的表

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';

MariaDB基础命令的更多相关文章

  1. 学习 git基础命令

    缘起 年后到了新公司,由于个人意愿到了一个海外的项目组,除了自己从Java技术栈转了C#技术栈外,很多技术都是第一次使用,学习压力不小啊. 自己也就先从常用的技术开始学起,比如C#,AngularJS ...

  2. Linux安全基础:shell及一些基础命令

    1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...

  3. LINUX二十个基础命令

    LINUX二十个基础命令 一. useradd命令 1.命令格式: useradd 选项 用户名 2.命令功能: 添加新的用户账号 3.常用参数: -c comment 指定一段注释性描述.-d 目录 ...

  4. Docker - Docker基础命令及使用

    Docker Docker - 官网 Docker - Hub GitHub - Docker Docker中文社区 Docker基础命令 Docker 查看帮助信息:docker --help 查看 ...

  5. vim基础命令

    2015-06-04 by komilevim基础命令 打开一个文件 vim index.php 几种模式说明Normal Mode也就是最一般的普通模式,默认进入vim之后,处于这种模式.Inser ...

  6. linux学习之——基础命令

    Linux体系基础命令: Linux是一个命令行组成的操作体系!精华在命令行,岂论图形界面成长到什么水平这个原理是不会变的,Linux命令有许多壮大的效用:从简单的磁盘操作.文件存取.到举办庞大的多媒 ...

  7. windows下使用redis,Redis入门使用,Redis基础命令

    windows下使用redis,Redis入门使用,Redis基础命令 >>>>>>>>>>>>>>>> ...

  8. Linux系统基础命令

    这是看itercast的学习笔记 Linux系统基础命令 日期时间 命令date用以查看.设置当前系统时间:格式化显示时间: +%Y--%m--%d 命令hwclock(clock)用以显示硬件时钟时 ...

  9. 第四节,Linux基础命令

    第四节,Linux基础命令 命令是系统操作员对系统传入的指令,传入指令后回车,系统接收到指令做出相应的行为 1.查看用户位于系统什么位置 [pmd]检查操作用户位于系统的什么位置 命令         ...

随机推荐

  1. Vue 1组件的使用

    在components新建一个vue组件: <template> <div> <h1>{{ msg }}</h1> </div> </ ...

  2. 【shell编程】之基础知识-函数

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...

  3. top-adx-apps

    中国地区top adx和流量平台 12719 unity ios 12001 mopub ios 6599 unity android 5277 mobfox ios 3855 mopub andro ...

  4. linux运维注意事项

    防火墙和selinux一定要注意 服务器或应用出现错误是一定要查看服务器运行状态是否正确,及其相关运行日志 不管什么时候,能不要特殊字符就不要用特殊字符,避免不必要的错误 在看部署文档的时候一定要认真 ...

  5. linux磁盘检测和修复

    显示磁盘和闪存的信息,以及分区信息 [root@bogon shell]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 s ...

  6. 使用patroni 构建高可用的pg 数据库

    patroni 是一个基于zk.etcd .consul 等的pg ha 模版,我们可以使用这个工具,快速的搭建一套 pg 的高可用方案 环境准备 mac 操作系统 安装基础差组件 brew inst ...

  7. PowerDesigner ---- 数据库设计(物理模型CDM和概念模型PDM)

    前言 上一篇介绍了个PowerDesigner工具的安装和汉化,现在我就说一下怎么用这个PowerDesigner建数据库吧.   内容 第一种方法:概念模型转物理模型 1.首先新建模型--选择概念模 ...

  8. How to make a user a local admin on just one computer

    log in to each "test" PC as the local admin Go to "Control Panel", "User Ac ...

  9. [转]linux中vim命令

    在vi中按u可以撤销一次操作 u      撤销上一步的操作 ctrl+r 恢复上一步被撤销的操作 在vi中移动光标至: 行首:^或0 行尾:$ 页首:1G(或gg) 页尾:G(即shift+g) 显 ...

  10. css中权重与继承

    出处:http://blog.csdn.net/xf616510229/article/details/53613212