linux mysql 入门_Linux MySQL基础

在实际生活中我们难免要与大量的数据打交道,而这就需要使用到数据库,数据库是相关联的数据的集合,有较少的数据冗余,而且数据库使数据和程序相互独立,这使得数据更加安全和可靠,也最大限度的保证了数据的正确性,同时也使数据可以并发使用并能同时保证一致性。在Linux中我们常用mysql和mariadb这两个数据库,mariadb是前者的升级版,不过二者皆可使用。下面我们就centos7来讨论以下数据库

一.数据库的安装

数据库的使用需要安装数据库的客户端和服务器,但是在centos7中由于yum安装库中没有可以直接使用的mysql-server安装包,所以用yum安装我们只能选择安装mariadb-server,不过这两个数据库在操作上是一样的,虽然安装的是mariadb但是完全可以当做mysql来使用。若想要在centos7上安装mysql-server我们需要自己从网站下载,配置安装。

以下我们来用yum安装mariadb

yum -y install mariadb mariadb-server //我们需要安装两个包,所以索性就在一条命令一起安装

安装好后我们只需systemctl start mysql 启动服务即可,然后我们就可以在系统中直接键入mysql后回车来进入我们的数据库系统。在root用户下我们可以直接进入mysql,但如果在其他用户下则需要输入用户名和密码

二.数据库基本操作

1.基本语句

在我们键入mysql后就进入了mysq中,但此时我们还没有进入数据库,显示如下

[root@centos7 ~]# mysql

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connectionid is 16Server version:5.5.60-MariaDB MariaDB Server

Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.

MariaDB [(none)]>

在当前状态下我们可以进行数据库级别的操作和一些查看当前系统信息的命令:

查询系统信息:

//查询当前数据库版本

MariaDB [hellodb]> selectversion();+----------------+

| version() |

+----------------+

| 5.5.60-MariaDB |

+----------------+

1 row in set (0.00 sec)

//查询当前用户和主机

MariaDB [(none)]> selectuser();+----------------+

| user() |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.00 sec)

//查询系统中所有数据库

MariaDB [(none)]>show databases;+--------------------+

| Database |

+--------------------+

| information_schema |

| hellodb |

| mysql |

| performance_schema |

| test |

+--------------------+

5 rows in set (0.00 sec)

数据库操作

//创建数据库

MariaDB [(none)]> create database xuexiao; Query OK, 1 row affected (0.01 sec)

//使用或进入某数据库

MariaDB [(none)]>use xuexiao;

Reading table informationforcompletion of table and column names

You can turn off this feature to get a quicker startup with-A

Database changed

MariaDB [xuexiao]> //进入数据库后在中括号中会有相应标识

//删除数据库

MariaDB [xuexiao]>drop database xuexiao;

Query OK,0 rows affected (0.01sec)

MariaDB [(none)]>

//显示一个数据库中所有的表,不指定数据库则默认当前数据库

MariaDB [(none)]>show tables from hellodb;+-------------------+

| Tables_in_hellodb |

+-------------------+

| classes |

| coc |

| courses |

| scores |

| students |

| teachers |

| toc |

+-------------------+

7 rows in set (0.01 sec)

2.对表的操作

对表的操作需要当前在一个数据库中。

对表整体的操作

//创建表,在创建时至少设置一个列

MariaDB [xuexiao]> create table students (id tinyint unsigned, lesson varchar(20) not null default "linux" , score tinyint unsigned, primary key (id,lesson));

Query OK,0 rows affected (0.01sec)/*创建一个表“student”其中包含了id,lesson,score这三个列,tinyint,varchar为数据类型,varchar后的20表示字符最大长度,unsigned,not null为数据书否可以为空,其中unsigned表示可以为空,default “linux”表示在不指定的情况下默认为“linux”,primary key指定了关键字。*/

//查询一个表的字段信息

MariaDB [xuexiao]>desc student;+--------+---------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------+---------------------+------+-----+---------+-------+

| id | tinyint(3) unsigned | NO | PRI | 0 | |

| lesson | varchar(20) | NO | PRI | linux | |

| score | tinyint(3) unsigned | YES | | NULL | |

+--------+---------------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

//修改一个表的表名

MariaDB [xuexiao]>alter table students rename to student;

Query OK,0 rows affected (0.00 sec)

//删除一个列

MariaDB [xuexiao]>alter table student drop lesson;

Query OK,0 rows affected (0.02sec)

Records:0 Duplicates: 0 Warnings: 0

//添加一个列

MariaDB [xuexiao]> alter table student add phone char(11) after id;

Query OK,0 rows affected (0.03sec)

Records:0 Duplicates: 0 Warnings: 0

//修改一个列

MariaDB [xuexiao]> alter table student change phone phonenu int;

Query OK,0 rows affected (0.01sec)

Records:0 Duplicates: 0 Warnings: 0

//添加唯一性约束,唯一性约束即在当前表中的这个数据值不能重复,比如电话号码,邮箱地址等

MariaDB [xuexiao]>alter table student add unique key (phonenu);

Query OK,0 rows affected (0.08sec)

Records:0 Duplicates: 0 Warnings: 0

//删除唯一性约束(索引)

MariaDB [xuexiao]>alter table student drop index phonenu;

Query OK,0 rows affected (0.01sec)

Records:0 Duplicates: 0 Warnings: 0

//创建索引

MariaDB [xuexiao]>create index age_index on student(phonenu);

Query OK,0 rows affected (0.02sec)

Records:0 Duplicates: 0 Warnings: 0

//查询索引

MariaDB [xuexiao]>show index from student;+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |

| student | 1 | age_index | 1 | phonenu | A | 0 | NULL | NULL | YES | BTREE | | |

| student | 1 | score_index | 1 | phonenu | A | 0 | NULL | NULL | YES | BTREE | | |

| student | 1 | class_index | 1 | phonenu | A | 0 | NULL | NULL | YES | BTREE | | |

| student | 1 | phone_index | 1 | class | A | 0 | NULL | NULL | YES | BTREE | | |

+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

6 rows in set (0.00 sec)

3.对数据的操作

insert

//向表中插入完整的全部数据

MariaDB [xuexiao]> insert into student values(1,2,12345678,99);

Query OK,1 row affected, 1 warning (0.00sec)

MariaDB [xuexiao]> select *from student;+----+-------+------------+-------+

| id | class | phonenu | score |

+----+-------+------------+-------+

| 1 | 2 | 12345678 | 99 |

+----+-------+------------+-------+

1 rows in set (0.00 sec)

//向表中插入部分数据

MariaDB [xuexiao]> insert into student (id,score)values(2,88);

Query OK,1 row affected (0.00sec)

MariaDB [xuexiao]> select *from student;+----+-------+------------+-------+

| id | class | phonenu | score |

+----+-------+------------+-------+

| 1 | 2 | 12345678 | 99 |

| 2 | NULL | NULL | 88 |

+----+-------+------------+-------+

2 rows in set (0.00 sec)

//多行插入

MariaDB [xuexiao]> insert into student (id,score)values(3,77),(4,66),(5,55);

Query OK,3 rows affected (0.00sec)

Records:3 Duplicates: 0 Warnings: 0MariaDB [xuexiao]> select *from student;+----+-------+------------+-------+

| id | class | phonenu | score |

+----+-------+------------+-------+

| 1 | 2 | 12345678 | 99 |

| 2 | NULL | NULL | 88 |

| 3 | NULL | NULL | 77 |

| 4 | NULL | NULL | 66 |

| 5 | NULL | NULL | 55 |

+----+-------+------------+-------+

5 rows in set (0.00 sec)

update

//更新修改表中数据

MariaDB [xuexiao]> update student set phonenu='12345679', class="3" where id=3;

Query OK,1 row affected, 1 warning (0.00sec)

Rows matched:1 Changed: 1 Warnings: 1MariaDB [xuexiao]> select * from student; //查看表中所有数据

+----+-------+------------+-------+

| id | class | phonenu | score |

+----+-------+------------+-------+

| 1 | 2 | 12345678 | 99 |

| 2 | NULL | NULL | 88 |

| 3 | 3 | 12345679 | 77 |

| 4 | NULL | NULL | 66 |

| 5 | NULL | NULL | 55 |

+----+-------+------------+-------+

5 rows in set (0.00 sec)

delete

//删除数据

MariaDB [xuexiao]> delete from student where id=5;

Query OK,1 row affected (0.01sec)

MariaDB [xuexiao]> select *from student;+----+-------+----------+-------+

| id | class | phonenu | score |

+----+-------+----------+-------+

| 1 | 2 | 12345678 | 99 |

| 2 | NULL | NULL | NULL |

| 3 | 3 | 12345679 | 77 |

| 4 | NULL | NULL | NULL |

+----+-------+----------+-------+

4 rows in set (0.00 sec)

select

数据库的主要功能也是我们最常用的功能

格式:

select 列名from表名where条件(条件的格式为“列名in限制”,其中字符串类型需要引号,且忽略大小写)

MariaDB [xuexiao]> select * from student; //*代表全部列

+----+-------+----------+-------+

| id | class | phonenu | score |

+----+-------+----------+-------+

| 1 | 2 | 12345678 | 99 |

| 2 | NULL | NULL | NULL |

| 3 | 3 | 12345679 | 77 |

| 4 | NULL | NULL | NULL |

+----+-------+----------+-------+

4 rows in set (0.00 sec)

MariaDB [xuexiao]> select id,score from student; //只查找指定列

+----+-------+

| id | score |

+----+-------+

| 1 | 99 |

| 2 | NULL |

| 3 | 77 |

| 4 | NULL |

+----+-------+

4 rows in set (0.00 sec)

MariaDB [xuexiao]> select id as student_id,score+10,class+10class from student;+------------+----------+-------+

| student_id | score+10 | class |

+------------+----------+-------+

| 1 | 109 | 12 |

| 2 | NULL | NULL |

| 3 | 87 | 13 |

| 4 | NULL | NULL |

+------------+----------+-------+

4 rows in set (0.00sec)/*select id as student-id(as 表示以别名显示,但不会改变原表,as可省略),score+10(表示显示出的数据在原数据基础上加10,表头也由原来的score变为score+10),class+10 class(在加10的基础上以别名显示)from student ;*/

注意:

where只能处理表中的原数据,对于处理过的数据要用having

select中列名可用别名代替也可以运用表达式:

select 条件写法:

列名=value //当为单个数据时用等号,多个值用in

between 10 and 20 表示只显示数值在10<=x<=20之间的数据

in(10,20)表示只显示数值为10和20的数据

not in(10,20)与上相反

去重

select distinct 列名from 表名;

模糊匹配

select 列名 from 表名 where 列名 like ‘ka_’; //下划线表示单个任意,%表示任意长度的任意

函数

用法:直接在语句中调用

例如:select upper(name) from student;

upper(列名)大写

lower(列名)小写

abs(列名)取绝对值

length(列名)计算字段长度

mod(n,m)n/m取余

ceil(n)取不小于n的整数

floor(n)取不大于n的整数

round(n,m)保留到小数点后m位,不写m或者m=0则四舍五入为整数,若m=-1则小数点前一位四舍五入

truncate(n,m)保留到小数点后m位但是截断,不四舍五入,若m=-1则小数点前一位截断即为0

concat(列名,’ age is ’,列名)合成两列为一列显示并可在中间添加字符串

insert(列名,n,m,’aaa’)将指定列从第n个字符开始往后替换m个字符,将这m个字符替换为‘aaa’

lpad(列名,n,*)满足n位则向左截取,不满足则在左补*

rpad(列名,n,*)右

replace (列名,‘m’,‘n’)m替换为n

substr(列名,n,m)从第n位开始往后取m位,不写m则取到最后

count(列名)返回表中满足where条件行的数量,没有where则输出总行数

avg(列名)取平均值,null不参与运算

max(列名)取最大值

min(列名)取最小值

sum(列名)求和

null函数:

ifnull(列名,0)如果值为null则当做0

coalesce(列名,0)取出第一个为null的值,显示其值为null

日期相关函数:

now()取出当前时间

curdate()取出当前日期

curtime()取出当前时间

year()取出年

month()取出月

day()取出日

dayname()取出星期几

str_to_date(‘24-11-2018’,’%d-%m-%Y’)字符转日期

date_format(birthday,’%Y年%m月%d日’)日期转字符

分组处理

select 列名1 from 表名 group by 列名2 以列名2为组显示列名1

select  courseid , avg(score) from  scores  group  by  courseid

排序处理

select * from 表名 order by 列名1,列名2 desc ;以列名1排序由小到大,在列名2中,列名1相同的情况下由大到小排序

select * from 表名 order by 列名1 limit 10;以列名1排序,且只显示前10行

select * from 表名 order by 列名1 limit 10,3; 以列名1排序,显示10行后的3行即11,12,13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值