频道栏目
首页 > 资讯 > Linux > 正文

MySQL进阶篇-Darius-51CTO博客

19-05-06        来源:[db:作者]  
收藏   我要投稿

MySQL基础篇

2.1 数据类型

MySQL中定义数据字段的类型对你数据库的优化是非常重要的。

2.1.1 数值类型

MySQL进阶篇

2.1.2 日期和时间类型

MySQL进阶篇

2.1.3 字符串类型

MySQL进阶篇

整型

?tinyint,占1字节,有符号:-128~127,无符号位:0~255

浮点型

?float([m[,d]]) 占4字节,1.17E-38~3.4E+38

字符型

?char([m]):固定长度的字符,占用m字节

2.2 常用select命令

使用select命令查看mysql数据库系统信息:

select now();  

-- 打印当前的日期

select curdate();  

-- 打印当前的时间

select curtime();  

-- 打印当前数据库

select database();  

-- 打印MySQL版本

select version();  

-- 打印当前用户

select user();  

--查看系统信息

show variables;  
show global variables;  
show global variables like '%version%';  
show variables like '%storage_engine%'; 默认的存储引擎  

like模糊搜索还可用户where字句,例如

select * from students where stname like '%l%1%2%3%';  

除了like 还有not like

--查看系统运行状态信息

show status;  
show global status like 'Thread%';  

2.3 导出,导入数据库

2.3.1 导入数据库

导入数据库前必须创建一个空数据库

mysql -e 'create database book' -uroot -p123456  
或者登陆 mysql   
create database book;  

导入(方法一)

mysql -uroot -p123456 book < book.sql  
mysql> use book;  
mysql> show tables;  
+----------------+  
| Tables_in_book |  
+----------------+  
| books          |  
| catego  
+----------------+  

导入(方法二)

create database book;  
mysql> use book;      
mysql> source /root/book.sql  #sql脚本的路径  
mysql> show tables;  
+----------------+  
| Tables_in_book |  
+----------------+  
| books          |  
| category       |  
+----------------+  

2.3.2 导出数据库

导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名

mysqldump -uroot -p123456 book>book2.sql  

扩展知识

如何把一个select的结果导出到文本

select * into outfile '/tmp/123.txt' from books; 此处有个文件访问权限问题,mysql用户是可以访问/tmp路径的,所以这里放到tmp下  
select * from books into outfile '/tmp/456.txt';  
其实就是备份数据库  

2.4 sql查询语句进阶

在我们刚导入的book数据库进行测试

2.4.1 查看表的内容:

mysql> select * from category;  
mysql> select * from books;  
mysql> select * from books\G  

2.4.2 查看字段类型:

desc 表名

mysql> desc books;  

2.4.3 逻辑运算符:

and or not

mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;  
+--------------------------------------+--------------------------+-------+  
| bName                                | publishing               | price |  
+--------------------------------------+--------------------------+-------+  
| Illustrator 10完全手册    | 科学出版社 |    50 |  
| FreeHand 10基础教程   | 北京希望电子出版  |    50 |  
| 网站设计全程教程  | 科学出版社 |    50 |  
| ASP数据库系统开发实例导航    | 人民邮电出版社   |    60 |  
| Delphi 5程序设计与控件参考 | 电子工业出版社   |    60 |  
| ASP数据库系统开发实例导航    | 人民邮电出版社   |    60 |  
+--------------------------------------+--------------------------+-------+  

2.4.4 算术运算符:

= 等于

大于

WHERE column IN (value1,value2,...)  
WHERE column NOT IN (value1,value2,...)  

Not in 与in相反

找出价格大于60的记录

mysql> select bName,price from books where price>60;  

找出价格为60的

mysql> select bName,price from books where price=60;  

找出价格不等于60的

mysql> select bName,price from books where price<>60;  

找出价格是60,50,70的记录

mysql> select bName,price from books where price in (50,60,70);  

找出价格不是60,50,70的记录

mysql> select bName,price from books where price not in (50,60,70);  

2.4.5 排序:

升序:order by “排序的字段” asc 默认

mysql> select bName,price from books where price  in (50,60,70) order by price asc;  
+------------------------------------------------+-------+  
| bName                                | price |  
+------------------------------------------------+-------+  
| Illustrator 10完全手册    |    50 |  
| FreeHand 10基础教程   |    50 |  
| 网站设计全程教程  |    50 |  
| ASP数据库系统开发实例导航    |    60 |  
| Delphi 5程序设计与控件参考     |    60 |  
| ASP数据库系统开发实例导航    |    60 |  
mysql> select bName,price from books where price  in (50,60,70) order by price desc;  
+--------------------------------------+-----------------+  
| bName                                | price |  
+--------------------------------------+-----------------+  
| ASP数据库系统开发实例导航    |    60 |  
| Delphi 5程序设计与控件参考     |    60 |  
| ASP数据库系统开发实例导航    |    60 |  
| Illustrator 10完全手册            |    50 |  
| FreeHand 10基础教程               |    50 |  
| 网站设计全程教程              |    50 |  

多个字段排序

select bName,price from books where price  in (50,60,70) order by price desc,bName desc;  

2.4.6 范围运算:

[not]between ....and....

mysql> select bName,price from books where price not between 30 and 60 order by price desc; 

注:

(30,60) >30 and <60  
[30,60] >=30 and <=60  

2.4.7 模糊匹配查询:

字段名 [not]like '通配符' ----》% 任意多个字符

查找书名中包括"程序"字样记录

mysql> select bName from books where bName like '%程序%';  
不含有  
mysql> select bName from books where bName not like '%程序%';  

2.4.8 MySQL子查询:

概念:在select 的where条件中又出现了select

选择 类型名为“网络技术”的图书:

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='网络技术');  

选择类型名称为“***”的图书;

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='***');  

2.4.9 Limit限定显示的条目:

SELECT * FROM table LIMIT [offset,] rows   
                            偏移量  行数  

  LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1):

比如select * from table limit m,n语句

查出category表中第2条到第6行的记录。

mysql> select * from category limit 1,5;  
+---------+--------------+  
| bTypeId | bTypeName    |  
+---------+--------------+  
|       2 | 网站       |  
|       3 | 3D动画     |  
|       4 | linux学习  |  
|       5 | Delphi学习 |  
|       6 | ***       |  
+---------+--------------+  

查看所有书籍中价格中最低的三条记录

mysql> select bName,price from books order by price asc limit 0,3;  
+-----------------------------+-------+  
| bName                       | price |  
+-----------------------------+-------+  
| 网站制作直通车       |    34 |  
| ***与网络安全       |    41 |  
| 网络程序与设计-asp |    43 |  

我们将子查询和限制条目,算术运算结合起来查询

mysql> select bName,price from books where publishing="电子工业出版社" order by price asc limit 0,1;  
mysql> select bName,price from books where price<(select price from books where publishing="电子工业出版社" order by price asc limit 0,1);  

或者

mysql> select bName,price from books where price<all(select price from books where publishing="电子工业出版社");  

2.4.10 连接查询:

以一个共同的字段,求两张表当中符合条件的并集。 通过共同字段把这两张表连接起来。

内连接

select 字段  from 表1 inner join 表2  on 表1.字段=表2.字段  

内连接:根据表中的共同字段进行匹配

select a.bname,a.price,b.btypename from books a inner join category b on a.btypeid=b.btypeid;  
实际使用中inner可省略掉  
跟WHERE 子句结果一样  
select a.bname,a.price,b.btypename from books a, category b where a.btypeid=b.btypeid;  

外连接 (分为左外连接;右外连接)

左连接: select 字段 from a表 left join b表 on 连接条件

Select a.bname,a.price,b.btypename from books a left join category b on a.btypeid=b.btypeid;  
右连接:select 字段 from a表 right join b表 on 条件
Select a.bname,b.* from books a right join category b on a.btypeid=b.btypeid;  
右连接,可以多表连接  

2.4.11 聚合函数

函数:执行特定功能的代码块。

    mysql> select sum(price) from books; 或select sum(price) as 图书总价 from books;  
+------------+  
| sum(price) |  
+------------+  
|      10048 |  
+------------+  

avg()平均值:

mysql> select avg(price) from books where bId<=3;  
+------------+  
| avg(price) |  
+------------+  
|    39.3333 |  
+------------+  

max() 最大值:

mysql> select bName,max(price) from books; 这种方法是错误的  

我们来查一下最贵的图书是哪本?

select bname,price from books order by price asc limit 0,3;  

可见最贵书是Javascript与Jscript从入门到精通,而不是网站制作直通车

select bName,price from books where price=(select max(price) from books);  
+----------------------------------------+-------+  
| bName                          | price |  
+----------------------------------------+-------+  
| Javascript与Jscript从入门到精通 |  7500 |  
+----------------------------------------+-------+  

min()最小值:

mysql> select bName,price from books where price=(select min(price) from books);  
+-----------------------+-------+  
| bName                 | price |  
+-----------------------+-------+  
| 网站制作直通车 |    34 |  
+-----------------------+-------+  

count()统计记录数:

mysql> select count(*) from books where price>40;  
+----------+  
| count(*) |  
+----------+  
|       43 |  
+----------+  

Count()中还可以增加你需要的内容,比如增加distinct来配合使用

select count(distinct price) from books where price>40;  

算数运算:

/
mysql> update books set price=price+5 where price<40;  

给所有价格高于70元的书籍打8折

mysql> update books set price=price*0.8 where price>70;  

字符串函数:

mysql> select substr(bTypeName,1,7) from category where bTypeId=10;  
+-----------------------+  
| substr(bTypeName,1,7) |  
+-----------------------+  
| AutoCAD               |      本来是AutoCAD技术  
+-----------------------+  
select substr(bTypeName,8,2)from category where bTypeId=10;  
+-----------------------+  
| substr(bTypeName,8,2) |  
+-----------------------+  
| 技术                  |          只截取汉字  
+-----------------------+  
1 row in set (0.00 sec)  

concat(str1,str2,str3.....) 拼接。 把多个字段拼成一个字段输出

mysql> select concat(bName,publishing) from books;  
mysql> select concat(bName,"-----",publishing) from books;  

大小写转换

    mysql> select upper(bname) from books where bId=9;  
+---------------------------+  
| upper(bname)              |  
+---------------------------+  
| DREAMWEAVER 4?ɡμň?? |  
+---------------------------+  

这样转换中文会出现乱码

    mysql> select lower(bName) from books where bId=10;  
+-------------------------------+  
| lower(bName)                  |  
+-------------------------------+  
| 3d max 3.0 创作效果百例 |  
+-------------------------------+  
相关TAG标签
上一篇:oracle归档-linux-51CTO博客
下一篇:30.4. MySQL函数,存储过程,触发器,视图-颤沙的博客-51CTO博客
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站