MYSQL的基礎使用之MARIADB安裝、表的 創建 刪除 修改

初學mysql的小白,首次寫博客,爲了能讓自己記得更加深刻,也是以一種小白的角度交代初學者的經歷和困難,也算是記錄自己成長的步伐吧

 

我首次用mysql是在ubuntu上 ,現在用的是linux 中的Red Hat 分支的centOS 7 ,安裝時發現通常用的都是MariaDB 來代替mysql,通過資料查詢發現Mariadb是mysql的其中的一種分支,由mysql的創始人帶領的團隊所開發的mysql分支的一種版本,因爲mysql受到被Oracle收購後的日漸封閉與緩慢的更新,衆多Linux發行版逐漸拋棄了這個人氣開源數據庫,使MySQL在各大Linux發行版中的失勢

由於不滿MySQL被Oracle收購後的日漸封閉與緩慢的更新,衆多Linux發行版逐漸拋棄了這個人氣開源數據庫,而轉向了MariaDB,

雖然PostgreSQL一直被當作MySQL的直接競爭對手,然而真正給予其致命一擊的似乎更像是MariaDB,而以後給Mysql致命一擊的也將是MariaDB。

言歸正傳,我們來具體講講MariaDB ,其實MariaDb的操作與Mysql的操作基本一樣,只是基於Mysql進行了性能的提升,目前MariaDB的更新速度已經遠遠超越了Oracle團隊的速度,畢竟是Mysql創始人帶領的團隊,怎麼不讓人放心。

 

MariaDb的安裝

linux下 通過

yum install mariadb mariadb-server #詢問是否要安裝,輸入Y即可自動安裝

mariaDBde 服務的基本命令

複製代碼
[root@127 ~]# systemctl start mariadb.service #啓動MariaDB

[root@127 ~]# systemctl stop mariadb.service #停止MariaDB

[root@127 ~]# systemctl restart mariadb.service #重啓MariaDB

[root@127 ~]# systemctl enable mariadb.service #設置開機自動啓動
[root@127 ~]# systemctl disenable mariadb.service #設置開機自啓關閉
複製代碼

  

初始化root密碼

複製代碼
[root@127 ~]# mysql_secure_installation  #爲初始化賬戶root添加密碼

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):這裏填寫root密碼,如果是第一次初始化密碼爲空直接敲回車(回車前)

複製代碼
複製代碼
Enter current password for root (enter for none):這裏填寫root密碼,如果是第一次初始化密碼爲空直接敲回車 (回車後↓)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password: 這裏填寫新的密碼
Re-enter new password: 這裏填寫重複的密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
# 這裏表示修改密碼成功
複製代碼

 

數據庫登錄

[root@127 ~]# mysql -uroot -proot 

       # mysql -u這裏是填寫的用戶名(默認爲root &我是空格& -p這裏填寫的是密碼(默認爲空)

 

複製代碼
這表示已經進入MariaDB
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.50-MariaDB MariaDB Server

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

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 通過/h可以查看很多命令
MariaDB [(none)]> 這裏是輸入sql語句的入口
複製代碼

以上是MariaDB的安裝


上一篇主要講的是關於Mysql的分支MariaDB在Linux下的安裝 順利安裝完成的小夥伴,就可以接着來試試SQL的魅力了

 紅色爲命令 藍色爲自定義名

查看數據庫

 

複製代碼
MariaDB [(none)]> show databases; #查看數據庫列表 
+--------------------+
| Database           |
+--------------------+
| information_schema | #數據庫名稱 例:本地有四個數據庫 (數據庫裏保存的是table 數據表)
| human              |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
複製代碼

 

首先我們先創建一個數據庫

 

創建數據庫 

  

MariaDB [(none)]> create database data_name; #創建數據庫   data_name爲創建的自定義數據庫名字 
                                 (注:創建的database不加s後綴,新手可別把查看數據庫命令的databases搞混了)
Query OK, 1 row affected (0.00 sec) #表示SQL語句執行成功

 

 然後我們進入剛纔創建的數據庫

  

MariaDB [(none)]> use data_name; #進入數據庫庫 data_name表示進入的數據庫名
Database changed #當前進入的數據庫已經更改

 

這裏首先要講的是簡單的創建

在數據庫中創建一個數據表

  

MariaDB [data_name]> create table table_name (id int not null ,name varchar(20)); #創建簡單數據表
        table_name 表示創建的數據表名字  id表示這個數據表第一列的列名 int表示這列的可插入數據爲整型 not null 設置約束爲不可爲空5種約束 
  name表示第二列的列名  varchar(20)爲字符類型 20是這列數據字符長度只能最多是20     
Query OK, 0 rows affected (0.06 sec)

 

查看一下我們數據庫裏新建的table表

 

複製代碼
MariaDB [data_name]> show tables; #查看當前數據庫的所有數據表(包括視圖表)
+---------------------+
| Tables_in_data_name |
+---------------------+
| table_name          | # table數據表的名字
+---------------------+
1 row in set (0.00 sec)
複製代碼

 

 

 查看數據表的結構的兩種方式

方法一(適合查看此table數據表有什麼列名和約束)

describe 可以用desc縮寫來代替

 

複製代碼
MariaDB [data_name]> describe table_name; #查看table數據表結構的方法一 (直觀可以看清楚數據表的列與綁定的約束)
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra | #field :列名   type:插入數據類型  null:是否爲非空約束 NO說明不能爲空(插入數據如果爲空,插入SQL會失敗)
+-------+-------------+------+-----+---------+-------+ #KEY: 主鍵約束或外鍵約束 (空表示沒有約束)     default:表示當插入數據爲空時默認插入  (空代表w爲null)
| id    | int(11)     | NO   |     | NULL    |       | #Extra: 中出現的信息一般不是太重要  對Extra等列意義深究的請查看別人的博客點擊此處 
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
複製代碼

 

 

 方法二(適合查看細節 查詢約束名的好辦法)

 

複製代碼
MariaDB [data_name]> show create table table_name; #查看table數據表的創建的SQL語句 (可以看出當時這個數據表創建而使用的SQL語句方便查閱約束名和設置的約束其他的細節)
+------------+-------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                  |
+------------+-------------------------------------------------------------------------------------------------------------------------------+
| table_name | CREATE TABLE `table_name` (      # ENGINE: 表示這個表類型,目前Mysql最常用的是MyISAM、InnoDB兩種表類型 各有各優勢 兩表的區別點擊這裏
         |   `id` int(11) NOT NULL,        # CHARSET: 表示這個表的字符編碼爲Latin1(不支持中文)可以設置主流的gb2312 或utf8修改編碼的具體方式點擊這裏
        |   `name` varchar(20) DEFAULT NULL  # 有關中文字符編碼的一勞永逸具體的設定解決辦法在這
        |    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
複製代碼

 

是不是感覺上面的表好難受,怎麼看都不爽,治療強迫症的患者們,myslq 提供了強迫症的治療方法 ↓

  

複製代碼
MariaDB [data_name]> show create table table_name\G # 這裏吧;改成了\G 發現顯示的樣式又不同了,更加養眼了(強迫症的福音) \G不光是這裏,
                                所有顯示的的SQL語句都可以用\G來代替;

*************************** 1. row ***************************
       Table: table_name
Create Table: CREATE TABLE `table_name` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
複製代碼

 

************************************** 我是動感光波****************************************

 

數據表的字段修改 

喝杯茶,休息一下

 

有一天項目經理說忘了告訴你這個表缺少重要的一個字段,友誼的小船說翻就翻,但是別擔心重做,SQL提供了反悔的機會可以任意添加或刪除列

 

數據表字段的添加

  

MariaDB [data_name]> alter table table_name add phone varchar(11); # 往數據表table_name添加一列 phone的 數據類型爲字符串 最大長度爲11
Query OK, 0 rows affected (0.06 sec)               
Records: 0  Duplicates: 0  Warnings: 0

 

 數據表字段的刪除

 

MariaDB [data_name]> alter table table_name drop phone; # 刪除數據表table_name中的phone列
                                  (這比添加簡單多了,果然毀滅比創造要簡單多了 但是一切的創造不都是建立在毀滅之後嗎?)
Query OK, 0 rows affected (0.06 sec)               
Records: 0  Duplicates: 0  Warnings: 0

 

如果是一個列的設置出錯刪除再修改一遍不會是很麻煩嗎?這裏有懶人辦法 , 刪除添加二合一 小霸王修改機  

 數據表字段的修改

MariaDB [data_name]> alter table table_name change name username varchar(20);  # 更改數據表table_namename字段名 爲username 更改數據類型varchar 最大長度爲20
Query OK, 0 rows affected (0.05 sec)                                 後面可以修改添加約束
Records: 0  Duplicates: 0  Warnings: 0

 

 

  這裏還有其他方法,可以改變類型卻不能改變字段名的SQL語句方法

  alter table table_name modify name char(20);  

這種方法可以用change的新舊名字相同的方法來實現相同的效果,即change爲小萬能

 

最後一個 懶人神器  ——模板建表

 

一  拓展表的好助手

 

MariaDB [data_name]> create table copy1_table_name like table_name;  # 複製表結構table_name創建copy1_table_name 表(這種方法不復制此表的內部數據)
Query OK, 0 rows affected (0.06 sec)

 

二 備份表的好助手

  

MariaDB [data_name]> create table copy2_table_name as select * from table_name;   #複製表結構table_name創建copy1_table_name 表(這種方法複製此表的內部數據)
 Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0

 

這次的就到這,如果還缺少什麼,或寫錯什麼歡迎指出!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章