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 ...
随机推荐
- java IO(六):额外功能处理流
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- c#后台调用API
前两周赶上项目第一个版本上线,着实忙了一把,毕竟只有两个人负责.如今已完结,总算喘了一口气,现在任务就是写API.测API,许久之前写过JS前台调用 项目API,也写过后台调用开放的手机号归属地查询, ...
- Redis进阶实践之九 独立封装的RedisClient客户端工具类
一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己写了两个工具类,但是这两个工具类,没有提供一致的接口,是为了使用的独立 ...
- (三)surging 微服务框架使用系列之我的第一个服务(审计日志)
前言:前面准备了那么久的准备工作,现在终于可以开始构建我们自己的服务了.这篇博客就让我们一起构建自己的第一个服务---审计日志. 首先我们先创建两个项目,一个控制台的服务启动项目,一个业务的实现项目. ...
- 在线生成PDF的网站-HTML 转 PDF 在线
http://pdf.df5d.com/ (服务器问题,演示暂停了,但是 下面介绍的组件还是可以使用的) 将前面用到的wkhtmltopdf用一个服务器程序集成在一起,接受一个URL参数,在生成一 ...
- JMeter基础教程1:若隐若现的参数化
1. 什么是参数化? 在开始学习JMeter参数化之前,我们先了解下什么是参数化: 参数化是自动化测试脚本的一种常用技巧.简单来说,参数化的一般用法就是将脚本中的某些输入使用参数来代替,在脚本运行时指 ...
- java之静态代理和动态代理
我们以几个问题,来开始我们今天的学习,如果下面几个问题,你都能说出个一二,那么恭喜你,你已经掌握了这方面的知识.1,什么是代理模式?2,Java中,静态代理与动态代理的区别?3,Spring使用的是J ...
- OS模块的常用内置方法
chdir 修改当前工作目录到指定目录 Change the current working directory to the specified path. chmod 修改一个文件的访问权限 Ch ...
- Eclipse远程调试应用程序
第一步,在应用程序的配置文件run.xml中加入下面的配置项,启动应用程序: <target name="run" depends="checkBuilderFai ...
- Git教程:
使用前配置: git init git config --global user.name "yanpeng1314" git config --global user.email ...