数据库基础之03(数据导入导出,增删改查,条件查询、多表查询、mysql管理工具)

https://blog.csdn.net/Perfect11_1/article/details/81155335#5-3%E3%80%81limit%20%E9%99%90%E5%88%B6%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E6%98%BE%E7%A4%BA%E7%9A%84%E8%A1%8C%E6%95%B0

目录

1、数据导入、导出

1-1、自定义导出、导入数据 文件存储路径

  1-2、导入数据的步骤:

1-3、数据导出:

2、管理表记录

2-1、添加新记录 insert  into

2-2、查询记录  select

2-3、更新记录字段的值  update

2-4、删除记录  delete   (删除表里的行),删则至少是一行

2-5、增删改查注意事项: 

 

3、对记录做select  update  delete时可以使用的基本匹配条件的表示方式:

3-1、数值比较   >   ,  >=   ,  <   ,  <=    ,  =     ,  !=

 3-2、字符比较:=   !=       is  null       is  not  null  

3-3、逻辑比较  and(与)   or(或)     !  (非)

3-4、( ) 提高执行顺序

3-5、范围内匹配

4、对记录做select  update  delete时可以使用的高级匹配条件的表示方式:

4-1、where   字段名   like    '表达式';

4-2、正则匹配

4-3、计算 操作  +     -       *       /       %  (字段类型 必须是数值类型)

4-4、聚集函数(MySQL服务自带的对数据做统计的命令);

5、操作查询结果  分组 排序  限制显示函数  过滤数据  

5-1、sql查询  group  by   字段名; 查询分组

5-2、sql查询  order   by   字段名   排序方式;  排序    asc   desc               

5-3、limit 限制查询结果显示的行数

5-4、having 条件   在查询结果里查找数据

6、多表查询  ***

6-1、    复制表 (作用: 备份表 、 快速建表)

 

6-2、  多表查询

6-2-1、格式 1   :  select  字段名列表  from 表名列表[  where  条件];

6-2-2、格式2  嵌套查询: 把内层的查询结果作为外层查询的查询条件

6-2-3、格式3 连接查询

7、MySQL图形管理工具

7-1 :部署运行环境lamp 或 lnmp

7-2 :安装软件phpMyAdmin

7-3 : 创建配置,指定管理数据库服务器

1、数据导入、导出
数据导入:把系统文件的内容存储到数据库服务器的表里--------有格式规律
数据导出:把数据库服务器的表里的记录存储到系统文件里---有格式规律
 

1-1、自定义导出、导入数据 文件存储路径
mysql> show  variables  like  "secure_file_priv";----来查看当前定义的导出、导入数据文件存储路径
 
如何自定义数据导出、导入文件存储的目录
[root@host50 ~]# mkdir /data
[root@host50 ~]# chown mysql /data/----该目录的所有者必须为mysql,否则mysqld程序执行者mysql将对其没有相关权限
]#vim /etc/my.cnf
[mysqld]
secure_file_priv="/data"----定义的导出、导入数据文件存储路径为/data
:wq
]#systemctl   restart mysqld----重起服务生效
mysql> show  variables  like  "secure_file_priv";---复查看是否更改成功

 
 1-2、导入数据的步骤:
把系统文件拷贝指定的数据导出、导入文件存储目录下
创建存储文件内容的表
导入数据 (命令格式)
mysql> system  cp /etc/passwd   /data/
mysql> system ls /data
passwd
 
create  table  db3.user(name  char(50),password  char(1),uid  int(2),gid  int(2),comment   varchar(150),homedir   char(150),
shell   char(50),index(name));
MySQL> load data infile "/data/passwd" into table db3.user fields terminated by ":" lines  terminated by  "\n";----将/mydata/passwd文件中的内容导入db3库的user表(每列之间通过:来分隔,每行之间通过换行来分隔)
 

1-3、数据导出:
mysql>   sql查询命令   into  outfile  "目录名/文件名";
select  * from t1  into  outfile  "/mydata/t1.txt";----列间不加分隔符的导出
select  * from t1  into  outfile  "/mydata/t2.txt" fields  terminated by "#";------列间通过加#分隔符来导出
 mysql> select  * from t1  into  outfile  "/mydata/t3.txt" fields  terminated by "#"   lines terminated by "\n";----列间通过加#分隔符行间通过换行来导出
 

2、管理表记录
通过四种方式:
添加新记录 insert  into
查询记录  select
更新记录字段的值  update
删除记录  delete
 

2-1、添加新记录 insert  into
insert  into  user     values
(42,"bob","x",2000,2000,"student user","/home/bob","/bin/bash"),(43,"bob","x",2000,2000,"student user","/home/bob","/bin/bash");----添加多行可以通过逗号分隔
 
insert into  user (name,shell,uid)    values("tom","/sbin/nologin",1928),("alice","/sbin/nologin",1948);----一次可以只给部分字段赋值
 

2-2、查询记录  select
select  字段名列表 from   表  where   条件;
select  nam,uid,shell   from  user;

2-3、更新记录字段的值  update
update   表   set  字段名=值,字段名=值 ,.... where  条件;
update  user  set  password="A",gid=1000;


2-4、删除记录  delete   (删除表里的行),删则至少是一行
delete  from  表 where  条件;
delete  from  user  where   name="bob";
 

2-5、增删改查注意事项: 
字段值要与字段类型相匹配
读与字符类型的字段,要使用单引或双引
若不使用where等来限定条件,会更新所有记录
限定条件时,只更新匹配条件的记录
 
3、对记录做select  update  delete时可以使用的基本匹配条件的表示方式:
3-1、数值比较   >   ,  >=   ,  <   ,  <=    ,  =     ,  !=
where    字段名    符号   数值
select  * from user where id <= 10;
selct  name,uid  from  user where  uid=9;
select  name,uid,gid  from user where  uid=gid;

 

 3-2、字符比较:=   !=       is  null       is  not  null  
where  字段名    符号  “值”
select  name from user where  name="apache";
select  name,shell from user where  shell != "/bin/bash";

匹配空    is  null  
匹配空    is  not  null  
 
select  id,name  from user  where  name is null;-----匹配name为null(空)的
select  id,name  from user  where  name="null";-----匹配name里的值为null的
select  id,name  from user  where  name="";-----匹配name里的值为空值的
 注意空值和空完全不是一回事

 

3-3、逻辑比较  and(与)   or(或)     !  (非)
select   *  from user   where  name="root"   and   uid=1  and  shell="/bin/bash";
select  name,uid  from user   where  name="root"  or   uid=1;
 

3-4、( ) 提高执行顺序
select  name,uid  from user    
where  ( name="root" or   name="bin"  )  and    uid=1;
 

3-5、范围内匹配
 
select  name from user 

where  name  in ("root","sync","lucy","bob"); 
select  name,uid from user where  uid  in (1,7,27,1000); 
select  name,shell  from user where  shell  not in ("/bin/bash","/sbin/nologin");
 
select  *  from   user where  uid  between  10 and 20;
select  *  from   user where  uid >=10 and uid<=20;
 
distinct  (只适合select查询使用)-------可以统计字段值分多少类
 
select   distinct shell  from user;
select   distinct shell  from user  where  uid <= 500;
 

4、对记录做select  update  delete时可以使用的高级匹配条件的表示方式:
 

4-1、where   字段名   like    '表达式';
_  匹配任意一个字符     
% 匹配零个或多个字符
select  name  from user  where name like '___' ;匹配所有四个字符及四个字符以上的name字段
select  name  from user  where name like '%a%' ;---匹配name字段值至少包含一个a的
select  name  from user  where name like '%___%' ;-----匹配所有四个字符及四个字符以上的name字段
select  id,name  from  user where name like '%' ;
 

4-2、正则匹配
where  字段名  regexp  '正则表达式';
 
select  name from user  
where  name  regexp  '^a.*t$';----匹配name字段值为a开头t结尾的

select  name from user  
where  name  regexp  '[0-9]';----匹配所有包含数字的name字段
 
select name,uid from user  
where  uid regexp  '....';----匹配所有四个字符及四个字符以上的name字段
 
select name,uid from user  
where  uid regexp  '^....$';----匹配所有只包含四个字符的name字段
 

4-3、计算 操作  +     -       *       /       %  (字段类型 必须是数值类型)
 
update  user  set  uid=uid+1  where id<=10;
select  name,uid,gid , uid + gid  jieguo  from user where name="root";----列出uid+gid的结果,并且该字段命名为jieguo
select  name,uid,gid , (uid + gid)  /  2   pjz  from user where name="root";--列出(uid+gid)/2的结果,并且该字段命名为plj
select  name ,age , 2018-age  s_year from user  where name ="root";-----列出2018-age的结果,并且该字段命名为s_year

 

4-4、聚集函数(MySQL服务自带的对数据做统计的命令);
注意:字段类型 必须是数值类型
select  sum(uid) from user where id<=10;-----对所有uid<10的uid字段的值求和
select  avg(uid) from user where id<=10;-----对所有uid<10的uid字段的值求和后做平均
select min(uid) from user where id<=10;----列出所有uid<10的uid字段的最小值
select max(uid) from user where id<=10;----列出所有uid<10的uid字段的最小值
select count(name) from user where  shell="/bin/bash";-----列出那么字段中非空的总行数
select count(*) from user;-------列出user表的总行数
 注意函数不能用来做条件查询:
 


5、操作查询结果  分组 排序  限制显示函数  过滤数据  
 
 

5-1、sql查询  group  by   字段名; 查询分组
select  shell  from user   group  by  shell;------根据shell字段值列出所有的shell字段值类型
 
select  distinct shell  from user;-----全表查询列出所有shell字段值类型-----相对比较慢
 

 

5-2、sql查询  order   by   字段名   排序方式;  排序    asc   desc               
select name,uid from  user where uid>=10  and uid<=500  
order by  uid desc ;-------按降序排列,字段值为null的作为最小来排
 

5-3、limit 限制查询结果显示的行数
sql查询 limit   数字;-------原型为:sql查询 limit    0 ,x; 显示查询结果从行号为0的行开始后的前 x行,因为这里的第一行行号为0
sql查询 limit   数字1 , 数字2; 显示指定范围内的行;(0 表示第1行)------从(数字1+1)行开始后的前(数字2)行
                      
select  name,shell from user where uid  <=500  limit  1,5
 
select  * from user  limit  4,3;

 

5-4、having 条件   在查询结果里查找数据
sql查询   having  条件;
 
select name,uid from user  where uid >=1000  
having name   is  null;-------列出uid大于等于1000的name字段值为null的行的name字段和uid字段的值
 
select name,uid from user  where uid >=1000  
having  uid=65534;------列出uid大于等于1000的name字段值为65534的行的name字段和uid字段的值
 
select name,uid from user  where uid >=1000   
having name="bob";------列出uid大于等于1000的name字段值为bob的行的name字段和uid字段的值
 

6、多表查询  ***

6-1、    复制表 (作用: 备份表 、 快速建表)
 
mysql>   create  table  表名  sql查询;--------快速建表

create  table  user2  select  * from db3.user;---user表做母版复制user2表
show  tables;
select  * from user2;
 
create  table  user3  select  name,uid,shell from db3.user  order by  uid  desc limit  5;---复制一个表user3包含user表前5行中name,uid,shell字段的列
select  * from user3;
 
select  * from  user  where  shell="abc";
create  table  user4   select  * from  db3.user  where shell="abc";-----复制一个表user4包含user表中shell字段值为abc的行
 
select  * from user4;
desc  user4;
 

6-2、  多表查询
6-2-1、格式 1   :  select  字段名列表  from 表名列表[  where  条件];

create table  t1   select  name,uid,shell from db3.user limit 3;
select   *  from t1;
 
create  table   t2   
select  name,password,uid,homedir from db3.user limit  5;
select   *  from t2;
 
笛卡尔集            3 * 5  =  15------总行数为两张表行数的乘积
select  *  from  t1,t2;
 
select  t1.name ,t2.name  from  t1,t2;
select  t1.* ,t2.password,t2.homedir   from  t1,t2;
 
select  t1.* ,  t2.password , t2.homedir   from  t1,t2
where  t1.uid  =  t2.uid;


6-2-2、格式2  嵌套查询: 把内层的查询结果作为外层查询的查询条件
select  字段名列表  from  表名  where  条件 (select  字段名列表  from  表名  where  条件);
 
select name   from db3.user order by  uid   limit  1;
 
select   avg(uid)  from db3.user;
select name,uid  from db3.user where uid<( select   avg(uid)  from db3.user);
 
 
select  name from db4.t1;
select  name  from db3.user where  name in (select  name from db4.t1);
 

6-2-3、格式3 连接查询
左连接查询 : 以左边的表为主显示查询结果
select 字段名列表   from   表名A   left  join  表名B  on   条件;
 
右连接查询 : 以右边的表为主显示查询结果
select 字段名列表   from   表名A  right  join 表名B  on   条件;
 
create  table   db4.t3  select name,uid,shell  from db3.user  limit 5;
select  * from db4.t3;
 
create  table   db4.t4  select name,uid,shell  from db3.user  limit 7;
select  * from db4.t4;
 
select  * from   t3  left join  t4 on  t3.uid = t4.uid;
 
select  * from   t3  right join  t4 on  t3.uid = t4.uid;
 
select  t3.name,t4.name from   t3  right join  t4 on  t3.uid = t4.uid;
 
 

7、MySQL图形管理工具
7-1 :部署运行环境lamp 或 lnmp
rpm  -q httpd
yum  -y  install  httpd
systemctl   start  httpd   
systemctl   enable  httpd   
 
rpm  -q  php
rpm  -q  php-mysql
yum  -y  install  php  php-mysql
systemctl   restart  httpd
 

7-2 :安装软件phpMyAdmin
tar  -zxvf  phpMyAdmin-2.11.11-all-languages.tar.gz
ls  phpMyAdmin-2.11.11-all-languages
 
mv  phpMyAdmin-2.11.11-all-languages  /var/www/html/phpadmin
 
cd  /var/www/html/
 

7-3 : 创建配置,指定管理数据库服务器
]#cd  phpadmin
]#cp config.sample.inc.php    config.inc.php
]#vim   config.inc.php
<?php
.....
17 $cfg['blowfish_secret'] = 'pljabc';
31 $cfg['Servers'][$i]['host'] = 'localhost';
......
?>
:wq
步骤4 : 客户端访问
打开浏览器输入URL   

 http://192.168.4.50/phpadmin
用户 root   
密码 123456

 

相关推荐

网友评论(0)