mariadb sql_SQL教程:在MariaDB上学习SQL

mariadb sql

MariaDB是MySQL的流行分支,由MySQL原始版本的主要作者Monty Widenius创立。 正如Serdar Yegulalp在他的MariaDB教程中解释的那样:MariaDB入门 ,MariaDB具有“默认包含的许多强大功能,而不是仅作为附件提供,并且许多性能,可用性和安全性改进不能保证在其中显示出来。 MySQL。”

在本文中,我将向您展示如何使用SQL查询MariaDB数据库的基本元素。 在安装MariaDB(在Mac上为Mac)之后,我们将逐步完成连接数据库,加载数据以及各种查询(包括表联接)的步骤。 最后,我们将对MariaDB扩展进行简要讨论,并提供一些进一步学习的建议。

[来自InfoWorld的专家: 什么是SQL? 数据库的语言解释了 | 数据库慢吗? 通过这17条规则来提高SQL查询的速度和可伸缩性,从而提高RDBMS的速度和可伸缩性。 7个基本SQL Server安全提示 ]

安装MariaDB

Serdar在他的文章中列出了安装MariaDB的选项,并提供了一些指向MariaDB下载和安装说明的链接。 我建议您还阅读MariaDB的“关系数据库简介”和“ 入门”页面。

我使用Homebrew在iMac上安装了MariaDB。 在如下所示的安装之前,我运行了brew update以便确保获得最新版本的MariaDB。

Martins-iMac:~ mheller$ brew install mariadb
==> Installing dependencies for mariadb: openssl
==> Installing mariadb dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2p.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2p.high_sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2p: 1,793 files, 12.3MB
==> Installing mariadb
==> Downloading https://homebrew.bintray.com/bottles/mariadb-10.3.9.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mariadb-10.3.9.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/mariadb/10.3.9/bin/mysql_install_db --verbose --user=mheller --basedir=/usr/local/Cellar/mariadb/10
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To connect:
    mysql -uroot

To have launchd start mariadb now and restart at login:
  brew services start mariadb

Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mariadb/10.3.9: 651 files, 174.4MB

MariaDB安装包括MySQL实用程序和MySQL Workbench图形客户端。 由于我拥有有效的JetBrains订阅,因此我还安装了DataGrip ,这是一个多数据库客户端工具。 如果在Windows上进行此练习,则可能会在Alpha Anywhere中使用可视化SQL查询生成器和SQL命令行。

连接到您的MariaDB数据库

连接到数据库之前,请先启动服务器。 我选择将MariaDB服务器作为应用程序而非后台服务运行。

Martins-iMac:~ mheller$ mysql.server start
Starting MariaDB
.180929 19:46:59 mysqld_safe Logging to '/usr/local/var/mysql/Martins-iMac.local.err'.
180929 19:46:59 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
. SUCCESS!

您可以使用mysql命令行实用程序进行连接,如下所示:

Martins-iMac:~ mheller$ mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.9-MariaDB Homebrew

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)]>

mysql实用程序可以向您显示有关自身和服务器的帮助。

MariaDB [(none)]> \h

General information about MariaDB can be found at
http://mariadb.org

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

MariaDB [(none)]> \q
Bye

完成MariaDB服务器后,可以将其关闭并回收其资源。

Martins-iMac:~ mheller$ mysql.server stop
Shutting down MariaDB
.. SUCCESS!

在MariaDB中加载数据

数据库设计是一门艺术,而不是一门科学,需要大量的经验才能正确。 即使是专业的数据库设计人员,也经常需要随着时间的推移修改其设计以匹配应用程序数据使用模式,例如,通过为常见查询添加索引,或通过添加外键约束来强制引用完整性。

不用花费数周(或数月)来学习数据库设计,而是加载一个已经具有MariaDB / MySQL格式的库存数据库。 有很多选项,包括作为示例MySQL Workbench附带的sakila DVD存储数据库(请参见下面的屏幕截图),但是我希望您加载MySQL兼容的Northwind版本。

玛丽亚德·萨基拉 IDG

sakila DVD存储数据库随MySQL Workbench一起提供。

浏览到Northwind扩展的Google Code存档,并下载文件Northwind.MySQL5.sql。 罗斯文(Northwind)最初是Microsoft的样本,但Valon Hoti已将其改编为MariaDB。 我之所以喜欢Northwind,主要是因为我曾经使用它来向开发人员讲授SQL,并逐渐熟悉了数据。 原始数据库图如下所示。

mariadb Northwind实体关系图 IDG

罗斯文(Northwind)数据库的实体关系图。

像这样的实体关系图来自工具。 该图像看起来很像是由SQL Server Management Studio生成的。 本质上,矩形是表格,列显示在矩形内,主键和外键用键图标标记。 表之间的连接是外键约束。

您可以使用mysql命令行,MySQL Workbench或您拥有的任何其他MySQL客户端将数据库加载到MariaDB中。 重新启动服务器后,我启动了MySQL Workbench并尝试连接到MariaDB。

mariadb连接警告 IDG

当我测试连接时,正如我所料,我得到了上面的警告,因为MySQL Workbench团队没有针对MariaDB进行测试。 无论如何,我仍然继续,并确认该连接有效:

mariadb连接正常 IDG

我打开了连接并打开了刚刚下载SQL文件:

mariadb工作台打开创建脚本 IDG

为了成功运行数据库定义脚本,我必须关闭该选项以防止错误。 然后执行脚本给了我一个很好的结果:

mariadb创建罗斯文 IDG

假设您到此为止,我们现在可以尝试使用select语句查询数据库。 我将从MySQL Workbench切换到mysql命令行,因为我发现了Workbench中的一些错误。

MariaDB中的简单SQL查询

首先,让我们尝试查看一个表。 从命令行运行mysql -uroot并使用Northwind数据库,如下所示。

请注意,现在使用没有密码的root用户是安全的,因为这是仅本地数据库,我们不需要设置安全性。 如果你改变了MariaDB的配置,以允许远程访问,请至少创建根强密码。

MariaDB [(none)]> use northwind;
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

数据库中定义了哪些区域? select查询应如下所示。 我从上图获得了表名。

MariaDB [northwind]> select * from region;
+----------+----------------------------------------------------+
| RegionID | RegionDescription                                  |
+----------+----------------------------------------------------+
|        1 | Eastern                                            |
|        2 | Westerns                                           |
|        3 | Northern                                           |
|        4 | Southern                                           |
+----------+----------------------------------------------------+
4 rows in set (0.000 sec)

那很简单。 星号表示返回表中的所有字段。 假设我们只是想按字母顺序查看区域名称。 这需要指定所需的字段名称(我们从上一个查询中获知的名称),并添加一个order by子句。 默认是升序排列。

MariaDB [northwind]> select RegionDescription from region order by RegionDescription;
+----------------------------------------------------+
| RegionDescription                                  |
+----------------------------------------------------+
| Eastern                                            |
| Northern                                           |
| Southern                                           |
| Westerns                                           |
+----------------------------------------------------+
4 rows in set (0.037 sec)

现在让我们看一下产品类别及其ID:

MariaDB [northwind]> select CategoryID, CategoryName from categories;
+------------+----------------+
| CategoryID | CategoryName   |
+------------+----------------+
|          1 | Beverages      |
|          2 | Condiments     |
|          3 | Confections    |
|          4 | Dairy Products |
|          5 | Grains/Cereals |
|          6 | Meat/Poultry   |
|          7 | Produce        |
|          8 | Seafood        |
+------------+----------------+
8 rows in set (0.000 sec)

您可能想知道为什么我没有显示所有字段。 碰巧的是,类别中的最后一个字段是旧Windows格式的图像,如果在Mac上的mysql显示它,则会看到一堆看起来很随意的字符。

现在,让我们看一下饮料产品,刚才看到的是category_id=1 。 这需要一个where子句。

翻译自: https://www.infoworld.com/article/3323393/sql-tutorial-learn-sql-on-mariadb.html

mariadb sql

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值