当我们在操作数据库的时候,难免会遇到数据导入导出的一些操作,今天突然学到了这个知识点,特意来给大家分享。
我用的是data的这条数据
1.使用数据
mysql> use data;
Database changed
2.显示数据库的表名
mysql> show tables;
+----------------+
| Tables_in_data |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
3.显示表中的数据
mysql> select * from user;
+----+-------+----------+
| id | login | password |
+----+-------+----------+
| 1 | 张三 | 123445 |
| 2 | 李四 | 123234 |
| 3 | 王武 | 1234 |
+----+-------+----------+
3 rows in set (0.00 sec)
4.删除表中id为2的这条数据
mysql> delete from user where id = 2;
Query OK, 1 row affected (0.00 sec)
5.现在再来显示表中的数据
mysql> select * from user;
+----+-------+----------+
| id | login | password |
+----+-------+----------+
| 1 | 张三 | 123445 |
| 3 | 王武 | 1234 |
+----+-------+----------+
2 rows in set (0.00 sec)
6.现在我们来导出这个数据库的数据,导出到c盘下面的r11.sql的这个文件里面,我们可以用下面这条命令
C:\Users\Administrator.YVDE-20150812RQ>mysqldump -uroot data>c:\r11.sql
7.当执行这条语句后没有错误信息,应该你的数据就导入成功了,我们再来看看c盘下面的这个文件。(可是这个文件有点长)
-- MySQL dump 10.13 Distrib 5.6.17, for Win64 (x86_64)
--
-- Host: localhost Database: data
-- ------------------------------------------------------
-- Server version 5.6.4-m7
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(10) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `user`
--
LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` VALUES (1,'张三','123445'),(3,'王武','1234');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2015-12-11 9:48:46
8.这是我们明显的可以看到INSERT INTO `user` VALUES (1,'张三','123445'),(3,'王武','1234');
这一行中没有id为2的这条数据,说明导入已经成功了。
9.我们再次进入数据库看看
C:\Users\Administrator.YVDE-20150812RQ>mysql -uroot
10.现在我们再删除data这个数据库
mysql> drop database data;
Query OK, 1 row affected (0.01 sec)
11.现在我们在来建立数据库data2
mysql> create database data2;
Query OK, 1 row affected (0.00 sec)
12.我们再来使用data2
mysql> use data2;
Database changed
13.现在我们可以看到这个表里面没有任何数据。
mysql> show tables;
Empty set (0.00 sec)
14.现在我们可以使用一条命令help
mysql> help
它显示的是以下命令
? (\?) 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.
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.
notee (\t) Don't write into outfile.
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.
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.
你可以看到ource的这条命令,它的解释是在数组中得到数据库名并执行数据库中的脚本文件。
15.我们可以执行这条命令,mysql> source c:\r11.sql,注意rll.sql是我们刚才导入的数据库文件
16.现在我们可以显示这张表
mysql> show tables;
+-----------------+
| Tables_in_data2 |
+-----------------+
| book |
| user |
+-----------------+
2 rows in set (0.00 sec)
17.我们可以打开user这张表,它显示的是如下信息
mysql> select * from user;
+----+-------+----------+
| id | login | password |
+----+-------+----------+
| 1 | 张三 | 123445 |
| 3 | 王武 | 1234 |
+----+-------+----------+
2 rows in set (0.00 sec)
这就是数据库的导入导出操作,其实看来还是比较简单的,重要的也就两条sql语句。