MYSQL的基础使用之MARIADB安装、表的 创建 删除 修改 - Go语言中文社区

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_nameG # 这里吧;改成了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

 

这次的就到这,如果还缺少什么,或写错什么欢迎指出!


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u013248434/article/details/65939024
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 10:36:33
  • 阅读 ( 1120 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢