mysql数据库导入导出 查询 修改表记录
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数据库导入导出 查询 修改表记录的更多相关文章
- MYSQL数据库导入导出(可以跨平台)
MYSQL数据库导入导出.sql文件 转载地址:http://www.cnblogs.com/cnkenny/archive/2009/04/22/1441297.html 本人总结:直接复制数据库, ...
- MySQL数据库 外键,级联, 修改表的操作
1.外键: 用来建立两张表之间的关系 - 一对多 - 多对多 - 一对一 研究表与表之间的关系: 1.定义一张 员工部门表 id, name, gender, dep_name, dep_desc - ...
- MYSQL 数据库导入导出命令
MySQL命令行导出数据库 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serve ...
- linux系统上Mysql数据库导入导出操作
需求:把MySQL数据库目录中的dz数据库备份到/home/dz_bak.sql ,然后再新建一个数据库dzbak,最后把/home/dz_bak.sql 导入到数据库dzbak中.操作如下:以下操作 ...
- 【原创】MySql 数据库导入导出(备份)
啥不说了,两周前刚刚做过mysql导入导出的结果现在又忘了.. 更可悲的是竟然同样的三篇blog,现在看起来还是如当初一样费劲,里面的内容..所以自己写个记录一下 环境:*nix 权限:有相关表的写读 ...
- MySql数据库导入导出
1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 存放位置 比如: mysqldump -u root -p project > c:/a. ...
- MYSQL 数据库导入导出命令
在不同操作系统或MySQL版本情况下,直接拷贝文件的方法可能会有不兼容的情况发生.所以一般推荐用SQL脚本形式导入.下面分别介绍两种方法. MySQL命令行导出数据库 1,进入MySQL目录下的bin ...
- mysql 数据库导入导出方法总结
一般形式:mysqldump -h IP -u 用户名 -p 数据库名 > 导出的文件名 (1)-p 后面不能加password,只能单独输入如1中那样 (2)mysqldump是在cmd下的命 ...
- linux下mysql数据库导入导出命令
首先linux 下查看mysql相关目录root@ubuntu14:~# whereis mysqlmysql: /usr/bin/mysql---- mysql的运行路径 /etc/mysql ...
随机推荐
- IIS与Apache禁止IP地址直接访问网站
一.IIS 防止恶意域名指向解决方法 首先单击"开始"-"设置"-"控制面板"-"管理工具",找到"Inter ...
- Python之Django rest_Framework补充
一.什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为"表征状态转移" ...
- 05_Linux网络配置及CRT远程
占位占位占位占位占位占位占位占位
- ASP.NET控件GridView的使用& Xml操作注意事项
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6729478.html 文章主要内容 ...
- 如何更改图片的背景色(PS、证件照之星)
如何更改图片的背景色(PS.证件照之星) 1.1 证照之星教你如何给证件照换背景 证照之星教你如何给证件照换背景?这个问题困扰很多人,如果你不了解证照之星,一款专业的证件照片制作软件,你肯定就无法自 ...
- 不要用for循环去遍历LinkedList
ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...
- 济南清北学堂游记 Day 2.
在大佬云集的地方被直线碾压是什么样的体验? 大概就是210和1030的差别. 大概就是高质量机械键盘和空气的区别. 回来的路上,我一直在想,我到底是不是一个高三的? 大概也是能找到以前在家和学校训练时 ...
- log4cpp退出时内存泄露的修复方案
1.缘由 一直对log4cpp非常有好感,就在自己的项目中集成了log4cpp1.1.1版本,并围绕着它建立了一系列的封装函数方便外部调用.写完了一个测试代码后,忽然想看看自己写的程序有没有内存泄露问 ...
- PHP数组基本排序算法和查找算法
关于PHP中的基础算法,小结一下,也算是本博客的第一篇文章1.2种排序算法冒泡排序:例子:个人见解 5 6 2 3 7 9 第一趟 5 6 2 3 7 9 5 2 6 3 7 9 5 2 3 6 7 ...
- angularjs中类似textarea的换行、空格处理
背景 今天测试人员测试出来一个显示数据的页面,没有换行. 原因剖析 这个页面是从一个<textarea>的页面拿到的数据,存到数据库中后再返回来的. 1. 知道这点之后,就有了调查方向了: ...