mysql数据导入导出:

导入:
把系统的文件的内容,保存到数据库的表里

导入数据的基本格式:
mysql> load data infile "文件名" into table 表名 fields terminated by '分隔符' lines terminated by '\n';

实例:把系统用户信息保存到hydra01库下的userinfo表里
mysql> create table userinfo(name char(20),password char(1),uid int(2),gid int(2),comment varchar(50),homedir varchar(60),shell varchar(25),index(name));
mysql> load data infile "/etc/passwd" into table hydra01.userinfo fields terminated by ":" lines terminated by '\n';(导入数据)
mysql> alter table userinfo add id int(2) auto_increment primary key first;(添加编号)
mysql> desc userinfo;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | MUL | NULL | |
| password | char(1) | YES | | NULL | |
| uid | int(2) | YES | | NULL | |
| gid | int(2) | YES | | NULL | |
| comment | varchar(50) | YES | | NULL | |
| homedir | varchar(60) | YES | | NULL | |
| shell | varchar(25) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+

导出:
把数据库中表里的记录存储到系统文件里

导出数据的基本格式:
格式一:mysql> select * from 库.表 into outfile "文件名";
格式二:mysql> select * from 库.表 into outfile "文件名" fields terminated by "符号";

实例:把mysql库下user表里的所有记录保存到系统文件xx.txt里
mysql> select * from mysql.user into outfile "xx.txt";
[root@mysql ~]# find / -name "xx.txt"
/var/lib/mysql/xx.txt(导出的内容默认存放在mysql下)

也可以导出到指定目录下
[root@mysql ~]# mkdir mysqldata
[root@mysql ~]# chown mysql /root/mysqldata(更改属主)
mysql> select * from mysql.user into outfile "/root/mysqldata/xx2.txt";

——————————————————————————————————————————————————————————————————

管理表记录

向表中插入新记录
一次向表中插入一条新记录,给新记录的每个字段都赋值
格式:mysql> insert into 库名.表名 values(字段值列表);
实例:给hydra01库的userinfo表插入一条新信息
mysql> insert into hydra01.userinfo values(26,"hydra","x",2003,20003,"hail hydra","/home/hydra","/bin/bash");

一次向表中插入多条新记录,给新记录的每个字段都赋值
格式:mysql> insert into 库名.表名 values(字段值列表),values(字段值列表),(字段值列表);
实例:给hydra01库的userinfo表插入多条新信息
mysql> insert into hydra01.userinfo values(29,"FBI","x",1937,1937,"hail hydra","/home/hydra","/bin/bash"),(30,"CIA","x",1937,1937,"hail hydra","/home/hydra","/bin/bash");

一次向表中插入多条新记录,给新记录的指定字段赋值
格式:mysql> insert into 库名.表名(字段名列表) values(字段值列表);
实例:给hydra01库的userinfo表插入新信息,并只给指定字段赋值
mysql> insert into hydra01.userinfo(name,password,uid,gid,comment,homedir,shell)values("Anonymousx","x",1937,1937,"teacher","/home/Anonymousx","/sbin/nogin");

查看效果
mysql> select * from userinfo;
+----+------------+----------+------+-------+------------------------------+---------------------+----------------+
| 26 | hydra | x | 2003 | 20003 | hail hydra | /home/hydra | /bin/bash |
| 27 | FBI | x | 2003 | 2003 | hail hydra | /home/hydra | /bin/bash |
| 28 | CIA | x | 2003 | 2003 | hail hydra | /home/hydra | /bin/bash |
| 29 | FBI | x | 1937 | 1937 | hail hydra | /home/hydra | /bin/bash |
| 30 | CIA | x | 1937 | 1937 | hail hydra | /home/hydra | /bin/bash |
| 31 | Anonymous | NULL | NULL | NULL | NULL | NULL | NULL |
| 32 | Anonymousx | x | 1937 | 1937 | teacher | /home/Anonymousx | /sbin/nogin |
+----+------------+----------+------+-------+------------------------------+---------------------+----------------+

—————————————————————————————————————————————————————————————————————————————————————

查询表
格式:
select 字段名列表 from 库名.表名 where 条件;

条件的表示方式:
数值比较:
条件格式:字段名 符号 数值
= != < > <= >=
实例:mysql> select * from userinfo where id <=10;

字符比较:
条件格式:字段名 符号 "值"
= != "
实例:mysql> select * from userinfo where shell!="sbin/nologin";

范围匹配:
条件格式:字段名 符号 匹配
between..adn.. /在..之间
in (值列表) /在里面
not in(值列表) /不在里面
实例:mysql> select * from userinfo where uid between 10 and 20;

逻辑匹配:
条件格式:字段名 符号 匹配 (多个查询条件时使用)
and:多个条件同时匹配
or:多个条件,匹配某一条件就可以
!:取反
实例:mysql> select * from userinfo where name="mysql" or uid=3000 ;

匹配空:
条件格式:字段名 符号 匹配
is null
实例:mysql> select * from userinfo where name is null;

匹配非空:
条件格式:字段名 符号 匹配
is not null
实例:mysql> select * from userinfo where name is not null;

模糊查询:
条件格式:字段名 like '表达式'
%:匹配0个或多个字符
_:匹配任意一个字符
实例:mysql> select * from userinfo where name like '____';

正则匹配:
条件格式:字段名 regexp '正则表达式'
^:开头
$:结尾
.:任意字符
*:任意字符
[]:区间
实例:mysql> select * from userinfo where uid regexp '[0-100]';

数学计算:
条件格式:字段名 计算 as 起名字 from 计算的表;
+ - * / %(取于)
实例:mysql> select uid,gid,(uid+gid)/2 as zhi from userinfo;

聚集函数:
条件格式:select xxx(字段名) from 表;
max(字段名)求最大值
min(字段名)求最小值
avg(字段名)求评价值
sum(字段名)求和
count(字段名)统计值个数
实例:mysql> select max(uid) from userinfo;

给查询结果排序:
格式:order by 字段名 排序方式;
排序方式:
asc:升序(默认排序方式)
desc:降序
实例:mysql> select name,uid from userinfo where uid >=10 and uid <=50 order by uid desc;

给查询结果分组:
格式:group by 字段名
实例: mysql> select shell from userinfo where uid <=10 group by shell;

限制显示查询结果显示的记录数inmit:
格式:limit 行数;
实例:mysql> select name,uid from userinfo where uid <=8 limit 2;

综合测试:
1.输出userinfo表中uid号最大的用户信息
测试:mysql> select * from userinfo order by uid desc limit 1;

2.输出表中uid号是两位数的最大的用户名和uid号
测试:mysql> select name,uid from userinfo where uid regexp '^..$' order by uid desc limit 1;

查看表中符合条件的记录,【所有】字段的值
格式:mysql> select * from 库名.表名 wher 条件;
实例:
mysql> select * from userinfo where id <=10;(数值比较)
mysql> select * from userinfo where shell!="sbin/nologin";(字符比较)
mysql> select * from userinfo where uid between 10 and 20;(范围匹配)
mysql> select * from userinfo where uid in (5,15,25);(范围匹配)
mysql> select * from userinfo where name in ("apache","mysql");(范围匹配)
mysql> select * from userinfo where name not in ("apache","mysql");(范围匹配)
mysql> select * from userinfo where name regexp '[0-9]';(正则)

查看表中符合条件记录,【指定】字段的值
格式:mysql> select 指定字段 from 库名.表名 wher 条件;
实例:
mysql> select id from userinfo where id <=10;(数值比较)
mysql> select name from userinfo where shell="sbin/nologin";(字符比较)
mysql> select name,id from userinfo where uid between 10 and 20;(范围匹配)
mysql> select name from userinfo where name="mysql" and uid=3000 ;(逻辑匹配)
mysql> select name from userinfo where name="mysql" or uid=3000 ;(逻辑匹配)
mysql> select name from userinfo where name is null;(匹配空)
mysql> select name from userinfo where name is not null;(匹配非空)
mysql> select name from userinfo where name like '____';(模糊查询)
mysql> select max(uid) from userinfo;(聚集函数)
mysql> select uid,gid,(uid+gid)/2 as zhi from userinfo;(数学计算)

—————————————————————————————————————————————————————————————————————————————————————

嵌套查询:把内层查询结果作为外层查询的查询条件。
格式:where 条件(子查询);
实例:把userinfo表中uid字段的值小于次字段平均值的用户名和uid号显示出来
测试:mysql> select name,uid from userinfo where uid < (select avg(uid) from userinfo);
实例:嵌套查询可以跨库查询
测试:mysql> select name from userinfo where name in (select user from mysql.user where user="root" and host="localhost");

复制表:(原表的索引不会被复制)
格式:create table 新名字 select * from 被复制的表
测试:mysql> create table userinfox select * from userinfo;
实例:复制userinfo表的前十条
测试;mysql> create table userinfox1 select * from userinfo limit 10;

复制表结构:(复制表结构不会复制数据)
格式:格式:create table 新名字 select * from 被复制的表 where 1 = 2;
测试:mysql> create table userinfox2 select * from userinfo where 1 = 2;

多表查询:
格式一:select 字段名 from 表名,表名;
格式二:select 字段名 from 表名,表名 where 条件;
测试:mysql> select * from userinfox,hydra;
测试:mysql> select userinfo.name,userinfox.name from userinfo,userinfox where userinfo.uid = userinfox.uid;

连接查询:
左连接查询:left join ...on(以左边的表为主显示查询结果)
格式:select * from 左表 left join 右表 on 条件;
测试:mysql> select * from userinfo left join userinfox on userinfo.name=userinfox.name;

右链接查询:right join ...on(以右边的表为主显示查询结果)
格式:select * from 左表 right join 右表 on 条件;
测试:mysql> select * from userinfo right join userinfox on userinfo.name=userinfox.name;

修改表记录
批量修改:
格式:update 库名.表名 set 字段名=值,字段名=值;
测试:mysql> update userinfox set uid=0,gid=0;
修改符合条件记录字段的值:
格式:update 库名.表名 set 字段名=值,字段名=值 where 条件;
测试:mysql> update userinfox set homedir="/opt" where name="/bin/bash";

删除表记录
删除所有记录:
格式:delete from 表名;
测试:delete from hydra01;
删除符合条件的记录:
格式:delete from 表名 where 条件;
测试:mysql> delete from userinfo where id is not null;

————————————————————————————————————————————————————————————————————————

mysql数据库导入导出 查询 修改表记录的更多相关文章

  1. MYSQL数据库导入导出(可以跨平台)

    MYSQL数据库导入导出.sql文件 转载地址:http://www.cnblogs.com/cnkenny/archive/2009/04/22/1441297.html 本人总结:直接复制数据库, ...

  2. MySQL数据库 外键,级联, 修改表的操作

    1.外键: 用来建立两张表之间的关系 - 一对多 - 多对多 - 一对一 研究表与表之间的关系: 1.定义一张 员工部门表 id, name, gender, dep_name, dep_desc - ...

  3. MYSQL 数据库导入导出命令

    MySQL命令行导出数据库 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serve ...

  4. linux系统上Mysql数据库导入导出操作

    需求:把MySQL数据库目录中的dz数据库备份到/home/dz_bak.sql ,然后再新建一个数据库dzbak,最后把/home/dz_bak.sql 导入到数据库dzbak中.操作如下:以下操作 ...

  5. 【原创】MySql 数据库导入导出(备份)

    啥不说了,两周前刚刚做过mysql导入导出的结果现在又忘了.. 更可悲的是竟然同样的三篇blog,现在看起来还是如当初一样费劲,里面的内容..所以自己写个记录一下 环境:*nix 权限:有相关表的写读 ...

  6. MySql数据库导入导出

    1.导出整个数据库     mysqldump -u 用户名 -p 数据库名 > 存放位置     比如:     mysqldump -u root -p project > c:/a. ...

  7. MYSQL 数据库导入导出命令

    在不同操作系统或MySQL版本情况下,直接拷贝文件的方法可能会有不兼容的情况发生.所以一般推荐用SQL脚本形式导入.下面分别介绍两种方法. MySQL命令行导出数据库 1,进入MySQL目录下的bin ...

  8. mysql 数据库导入导出方法总结

    一般形式:mysqldump -h IP -u 用户名 -p 数据库名 > 导出的文件名 (1)-p 后面不能加password,只能单独输入如1中那样 (2)mysqldump是在cmd下的命 ...

  9. linux下mysql数据库导入导出命令

    首先linux 下查看mysql相关目录root@ubuntu14:~# whereis mysqlmysql: /usr/bin/mysql----   mysql的运行路径 /etc/mysql ...

随机推荐

  1. gd库的安装

    gd库简介 主要用途编辑 在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等.在PHP处理图像,可使用GD库,而GD库开始时是支持GIF的,但由于 ...

  2. [DeeplearningAI笔记]改善深层神经网络1.1_1.3深度学习使用层面_偏差/方差/欠拟合/过拟合/训练集/验证集/测试集

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1 训练/开发/测试集 对于一个数据集而言,可以将一个数据集分为三个部分,一部分作为训练集,一部分作为简单交叉验证集(dev)有时候也成为验 ...

  3. MonogoDB 练习一

    1.解析文件,仅处理 FIELDS 字典中作为键的字段,并返回清理后的值字典列表 需求: 1.根据 FIELDS 字典中的映射更改字典的键 2.删掉"rdf-schema#label&quo ...

  4. VBA小技巧

    运用VBA时,可以构造一些函数去实现诸如printf的方便函数. Public Function printf(mask As String, ParamArray tokens()) As Stri ...

  5. ABP官方文档翻译 6.7 CSRF/XSRF保护

    CSRF/XSRF保护 介绍 HTTP动词 非浏览器客户端 ASP.NET MVC 特征 集成 布局视图 配置 ASP.NET Web API 特征 集成 集成到ASP.NET MVC客户端 集成到其 ...

  6. ABP官方文档翻译 5.1 Web API控制器

    ASP.NET Web API控制器 介绍 AbpApiController基类 本地化 其他 过滤器 审计日志 授权 反伪造过滤器 工作单元 结果包装和异常处理 结果缓存 校验 模型绑定器 介绍 A ...

  7. R语言dplyr包初探

    昨天学了一下R语言dplyr包,处理数据框还是很好用的.记录一下免得我忘记了... 先写一篇入门的,以后有空再写一篇详细的用法. #dplyr learning library(dplyr) #fil ...

  8. 在ConcurrentModificationException异常上的联想

    1.什么是ConcurrentModificationException? 大家都听说过快速报错fast-fail吧,fast-fail的发生就是说明发生了ConcurrentModification ...

  9. 阿里云学习之IOT物联网套件(配置篇)

    文档时间:2018.-1-24 首注:此文章是参照以下文章的整合与补充: https://bbs.aliyun.com/read/309106.html?amp;displayMode=1&p ...

  10. 好用的Google漏洞爬虫:Google Mass Explorer

    这是一款基于谷歌搜索引擎的自动化爬虫. 爬虫介绍 爬虫大体机制就是: 先进行一次谷歌搜索,将结果解析为特定格式,然后再提供给exp使用. 大家可以尝试使用–help来列出所有参数. 这个项目笔者会持续 ...