MySQL 基本数据库管理命令 - 第一部分
数据库是一组以电子方式存储的结构化数据。即使没有计算机,我们的祖先就知道数据库的概念,但是创建和维护这样的数据库是非常繁琐的工作。在一个100页的手工数据库中,如果你要搜索所有工资低于10k的员工,试想一下这会有多困难,那么。
在当今世界,您无法逃脱数据库。目前,世界各地有数百万个数据库正在运行,用于存储和获取各种数据,无论是战略数据、员工记录还是网络技术。
数据库通常被称为后端进程,因为它既对最终用户不可见,也不直接与数据库交互。他们工作于前端进程,即PHP、VB、ASP.NET等,并要求前端处理数据库在后端。
有几种数据库服务器和客户端可用,例如Oracle、MySQL、MySQLi、MariaDB、MongoDB等。所有这些的语法或多或少不太一样。掌握其中一个意味着可以控制其中的大多数,并且学习数据库的查询非常简单且有趣。
让我们从数据库的简单查询开始。我们将使用默认情况下与大多数 Linux 发行版捆绑在一起的 MySQL,如果您的情况默认情况下未安装它,您可以从存储库手动安装它。
数据库查询是一段简单的代码,它被发送到数据库以根据需要获取自定义和精确的结果。
安装MySQL数据库
使用“yum”或“apt”包管理器安装MySQL数据库。
yum install mysql mysql-client mysql-server (on Yum based Systems)
apt-get install mysql mysql-client mysql-server (on Apt based Systems)
启动MySQL
启动 MySQL 数据库服务:
service mysqld start
or
service mysql start
安装好 MySQL 数据库后,您将进入配置页面,要求您设置 admin 密码等。完成安装并启动服务器后,请转到您的 MySQL 提示。
mysql -u root -p
将 root 替换为您配置的用户名,并在出现提示时输入密码,如果登录凭据正确,您将进入您的MySQL< 眨眼时提示。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 195
Server version: 5.5.31-0+wheezy1 (Debian)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
现在,在此提示符下执行查询非常有教育意义且有趣。
创建数据库howtoing
mysql> create database tecmint ;
Query OK, 1 row affected (0.02 sec)
mysql>
注意:它报告查询正确,意味着数据库已创建。您可以验证新创建的数据库为。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tecmint |
| test |
+--------------------+
9 rows in set (0.00 sec)
mysql>
注意:注意上面输出中您的数据库。
选择数据库
现在您需要选择要处理的数据库。
mysql> use tecmint;
Database changed
mysql>
在 MySQL 中创建表
在这里,我们将创建一个名为“minttec”的表,其中包含三个字段:
mysql> CREATE TABLE minttec (
-> id Int(3),
-> first_name Varchar (15),
-> email Varchar(20)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql>
注意:上面的查询显示OK,这意味着表已创建,没有任何错误。要验证该表,请运行以下查询。
mysql> show tables;
+-------------------+
| Tables_in_tecmint |
+-------------------+
| minttec |
+-------------------+
1 row in set (0.00 sec)
mysql>
到目前为止一切进展顺利。是的!您可以查看在表“minttec”中创建的列,如下所示:
mysql> show columns from minttec;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| first_name | varchar(15) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
这简直就是一个魔法。无论如何,我会告诉你声明的类型及其含义。
- Int 是整数
- Varchar 是具有定义的可变长度的字符。 Type后面的值是该字段最多可以存储数据的长度。
好的,现在我们需要在“first_name”列之后添加一个“last_name”列。
mysql> ALTER TABLE minttec ADD last_name varchar (20) AFTER first_name;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
现在,在您的表中验证它。
mysql> show columns from minttec;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| first_name | varchar(15) | YES | | NULL | |
| last_name | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
在 MySQL 中添加列
现在,我们将在右侧添加一列,例如在电子邮件右侧添加“国家/地区”列。
mysql> ALTER TABLE minttec ADD country varchar (15) AFTER email;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
验证上面的列插入查询。
mysql> show columns from minttec;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| first_name | varchar(15) | YES | | NULL | |
| last_name | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| country | varchar(15) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql>
在字段中插入值
向字段中插入值怎么样?
mysql> INSERT INTO minttec VALUES ('1' , 'Ravi' , 'Saive' , '[email ' , 'India' );
Query OK, 1 row affected (0.02 sec)
mysql>
在上表中一次插入超过 1 个值怎么样?
mysql> INSERT INTO minttec VALUES ('2' , 'Narad' , 'Shrestha' , '[email ' , 'India' ), ('3' , 'user' , 'singh' , '[email ' , 'Aus' ), ('4' , 'tecmint' , '[dot]com' , '[email ' , 'India' );
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
验证上述插入。
mysql> select * from minttec;
+------+------------+-----------+-------------------+---------+
| id | first_name | last_name | email | country |
+------+------------+-----------+-------------------+---------+
| 1 | Ravi | Saive | [email | India |
| 2 | Narad | Shrestha | [email | India |
| 3 | user | singh | [email | Aus |
| 4 | tecmint | [dot]com | [email | India |
+------+------------+-----------+-------------------+---------+
4 rows in set (0.00 sec)
mysql>
删除字段中的值
假设上面输出中的第三个条目无效,我们需要删除第三个条目。
mysql> DELETE FROM minttec WHERE id = 3;
Query OK, 1 row affected (0.02 sec)
验证以上操作。
mysql> select * from minttec;
+------+------------+-----------+-------------------+---------+
| id | first_name | last_name | email | country |
+------+------------+-----------+-------------------+---------+
| 1 | Ravi | Saive | [email | India |
| 2 | Narad | Shrestha | [email | India |
| 4 | tecmint | [dot]com | [email | India |
+------+------------+-----------+-------------------+---------+
3 rows in set (0.00 sec)
更新字段中的值
需要编辑 id (=4)。
mysql> UPDATE minttec SET id = 3 WHERE first_name = 'tecmint';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
验证上述查询。
mysql> UPDATE minttec SET id = 3 WHERE first_name = 'tecmint';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
注意:执行上述查询并不是一个好主意。它将把 id 更改为“4”,其中第一个名称是“howtoing”。使用多个列与 where 子句来获得最小错误始终是一个好主意,如下所示:
mysql> UPDATE minttec SET id = 6 WHERE first_name = 'tecmint'AND last_name = '[dot]com';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
删除MySQL中的列
让我们需要删除(删除)我们认为不重要的列,在这里说“国家”。
mysql> ALTER TABLE minttec drop country;
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql>
验证该表。
mysql> select * from minttec;
+------+------------+-----------+-------------------+
| id | first_name | last_name | email |
+------+------------+-----------+-------------------+
| 1 | Ravi | Saive | [email |
| 2 | Narad | Shrestha | [email |
| 6 | tecmint | [dot]com | [email |
+------+------------+-----------+-------------------+
3 rows in set (0.00 sec)
mysql>
重命名 MySQL 中的表
您不认为我们的表名“minttec”不太相关吗?将其更改为 howtoing_table 怎么样?
mysql> RENAME TABLE minttec TO tecmint_table;
Query OK, 0 rows affected (0.03 sec)
mysql>
列出所有表
查看当前数据库下的所有表。
mysql> show tables;
+-------------------+
| Tables_in_tecmint |
+-------------------+
| tecmint_table |
+-------------------+
1 row in set (0.00 sec)
mysql>
该表已重命名。现在,无需任何复杂的工具,只需一行命令即可备份上述 MySQL 数据库。在终端而不是 mysql 提示符下运行以下代码。
mysqldump -u root -p tecmint > tecmint.sql
check the dumped file on your desktop which would have contents something like
-- MySQL dump 10.13 Distrib 5.5.31, for debian-linux-gnu (i686) --
-- Server version 5.5.31-0+wheezy1 --
Dump completed on 2013-09-02 12:55:37
维护 MySQL 数据库的备份始终是一个好主意。恢复备份的 MySQL 数据同样是一行简单的代码,您需要在终端提示符下运行,而不是在 mysql 提示符下运行。
但是,首先我们将删除数据库以验证我们的恢复是否完美。
删除数据库
mysql> drop database tecmint;
Query OK, 1 row affected (0.02 sec)
检查数据库服务器上的数据库“howtoing”。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| my_database |
| mysql |
| performance_schema |
| phpmyadmin |
| sisso |
| test |
+--------------------+
7 rows in set (0.00 sec)
mysql>
伟大的!数据库丢失了,但我们不用担心,我们有备份。
恢复数据库
要恢复丢失的数据库,请运行以下命令。
mysql -u root -p tecmint < tecmint.sql
Enter password:
ERROR 1049 (42000): Unknown database 'tecmint'
哎呀!错误,嘿,我们还没有创建数据库 howtoing。因此,转到 mysql 提示符并创建一个数据库“howtoing”。
mysql> create database tecmint;
Query OK, 1 row affected (0.00 sec)
mysql>
现在是时候在 shell 提示符下运行恢复命令了(严格来说)。
mysql -u root -p tecmint < tecmint.sql
Enter password:
验证您的数据库。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| tecmint |
| test |
+--------------------+
8 rows in set (0.00 sec)
验证数据库的内容。
mysql> show tables from tecmint;
+-------------------+
| Tables_in_tecmint |
+-------------------+
| tecmint_table |
+-------------------+
1 row in set (0.00 sec)
mysql>
验证恢复的表的内容。
mysql> select * from tecmint_table;
+------+------------+-----------+-------------------+
| id | first_name | last_name | email |
+------+------------+-----------+-------------------+
| 1 | Ravi | Saive | [email |
| 2 | Narad | Shrestha | [email |
| 6 | tecmint | [dot]com | [email |
+------+------------+-----------+-------------------+
3 rows in set (0.00 sec)
这绝对不是结束,我们将介绍主键、外键、多表和运行查询的概念在本文的下一部分中使用简单的 PHP 脚本。
不要忘记告诉我们您在阅读本文时的感受。非常感谢您的评论。保持健康和调整,保持与Tecmint的联系。