Linux MySQL基礎

在實際生活中咱們不免要與大量的數據打交道,而這就須要使用到數據庫,數據庫是相關聯的數據的集合,有較少的數據冗餘,並且數據庫使數據和程序相互獨立,這使得數據更加安全和可靠,也最大限度的保證了數據的正確性,同時也使數據能夠併發使用並能同時保證一致性。在Linux中咱們經常使用mysql和mariadb這兩個數據庫,mariadb是前者的升級版,不過兩者皆可以使用。下面咱們就centos7來討論如下數據庫mysql

一.數據庫的安裝

數據庫的使用須要安裝數據庫的客戶端和服務器,可是在centos7中因爲yum安裝庫中沒有能夠直接使用的mysql-server安裝包,因此用yum安裝咱們只能選擇安裝mariadb-server,不過這兩個數據庫在操做上是同樣的,雖然安裝的是mariadb可是徹底能夠當作mysql來使用。若想要在centos7上安裝mysql-server咱們須要本身從網站下載,配置安裝。linux

如下咱們來用yum安裝mariadbsql

yum -y install mariadb mariadb-server //咱們須要安裝兩個包,因此索性就在一條命令一塊兒安裝數據庫

 

安裝好後咱們只需systemctl start mysql 啓動服務便可,而後咱們就能夠在系統中直接鍵入mysql後回車來進入咱們的數據庫系統。在root用戶下咱們能夠直接進入mysql,但若是在其餘用戶下則須要輸入用戶名和密碼centos

二.數據庫基本操做

1.基本語句

在咱們鍵入mysql後就進入了mysq中,但此時咱們尚未進入數據庫,顯示以下安全

[root@centos7 ~]# mysql

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

Your MariaDB connection id is 16

Server 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 clear the current input statement.

 

MariaDB [(none)]>

在當前狀態下咱們能夠進行數據庫級別的操做和一些查看當前系統信息的命令:服務器

 

查詢系統信息:

//查詢當前數據庫版本

MariaDB [hellodb]> select version();

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

| version()      |

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

| 5.5.60-MariaDB |

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

1 row in set (0.00 sec)
//查詢當前用戶和主機

MariaDB [(none)]>  select user();

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

| 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 information for completion 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.01 sec)

 

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.01 sec)

/*建立一個表「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.02 sec)               

Records: 0  Duplicates: 0  Warnings: 0
//添加一個列

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

Query OK, 0 rows affected (0.03 sec)               

Records: 0  Duplicates: 0  Warnings: 0
//修改一個列

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

Query OK, 0 rows affected (0.01 sec)               

Records: 0  Duplicates: 0  Warnings: 0
//添加惟一性約束,惟一性約束即在當前表中的這個數據值不能重複,好比電話號碼,郵箱地址等

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

Query OK, 0 rows affected (0.08 sec)

Records: 0  Duplicates: 0  Warnings: 0
//刪除惟一性約束(索引)

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

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0
//建立索引

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

Query OK, 0 rows affected (0.02 sec)

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.00 sec)

 

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.00 sec)

 

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.00 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

MariaDB [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.00 sec)

Rows matched: 1  Changed: 1  Warnings: 1

 

MariaDB [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.01 sec)

 

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

數據庫的主要功能也是咱們最經常使用的功能less

格式:函數

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+10 class from student;

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

| student_id | score+10 | class |

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

|          1 |      109 |    12 |

|          2 |     NULL |  NULL |

|          3 |       87 |    13 |

|          4 |     NULL |  NULL |

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

4 rows in set (0.00 sec)

/*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

相關文章
相關標籤/搜索