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. 将centos_yum源更换为阿里云(官方文档)

    http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0.d6ykiD 1.备份 mv /etc/yum.repos.d/CentOS ...

  2. 【转】Shell执行MySql操作

    mysql  -hhostname -Pport -uusername -ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作m ...

  3. Apache 配置SSI速记

    1. 启用模块 httpd.conf LoadModule filter_module modules/mod_filter.so 2. <Directory 的Options配置中增加Incl ...

  4. web前端性能优化问题

    常用的几大优化解决: 页面内容的优化 减少http请求 途径: 1>启用http/2--越来越多的浏览器都开始支持 HTTP/2.HTTP/2 为同一服务器的并发连接问题带来了很多好处.换句话说 ...

  5. 对Javascript到底了解多少,一测便知道

    笔者在这里附上一段代码,请读者思考一下程序的运行结果: console.log(a); //??? a(); var a=3; function a(){ console.log(10); } con ...

  6. 【解决问题】SSH连不上Ubuntu虚拟机解决办法

    1. 安装openssh-client Ubuntu默认缺省安装了openssh-client,apt-get安装即可 sudo apt-get install openssh-client 2. 安 ...

  7. 配置python虚拟环境Virtualenv及pyenv

    pyenv pyenv 可以让机器安装各种不同版本的python pyenv install --list 查看可以安装的python版本 pyenv versions 查看已安装的python版本 ...

  8. 一个 rsync同步文件脚本

    #/bin/bash cd /root/phone echo "update guanwang phone version" git pull ]; then echo " ...

  9. 微信小程序 页面跳转传递数据

    点击view 跳转页面 <view class="album_image" data-album-obj="{{item}}" bindtap=" ...

  10. 基于JDK1.8的ArrayList剖析

    前言 本文是基于JDK1.8的ArrayList进行分析的.本文大概从以下几个方面来分析ArrayList这个数据结构 构造方法 add方法 扩容 remove方法 (一)构造方法 /** * Con ...