2-12-mysql-sql语句进阶-精品文档整理.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2-12-mysql-sql语句进阶-精品文档整理.docx》由会员分享,可在线阅读,更多相关《2-12-mysql-sql语句进阶-精品文档整理.docx(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、回顾前面的基础命令语句修改数据表添加字段:alter table 表名 add 字段名 列类型 not null|nullprimary keyuniqueauto_incrementdefault valuealter table 表名 add 字段定义 after ar_id;删除字段:alter table 表名 drop 字段名修改字段:alter table 表名 modify 字段名 字段新类型完整修改字段:alter table 表名 change 旧字段名称 新字段定义修改表名称alter table 表名 rename 新名字删除表drop table if (not) ex
2、ists 表名;表中行的操作insertinsert into 数据表名称 (字段列表) values|value (表达式|null|default,.),(表达式|null|default,.)insert into 数据表名称 set 字段名称=值,.insert与insert.set的区别是后者可以带有子查询。update - 单表update 表名 set 字段名称=值,. where 条件如果省略WHERE条件将更新全部记录。删除记录 - 单表delete from 数据表名称 where 条件如果省略where条件,将删除全部记录selectselect 字段列表 from 数据
3、表 as 别名 where 条件别名的用法: Select * from 数据表 as 别名字段名称 as别名Select product_offer_instance_object_id as ID, product_offer_instance_object_name name,coumn33 金额From tableselect btypeid as 图书类别ID,btypename as 图书类型 from category; select语句返回零条或多条记录;属于记录读操作insert、update、delete只返回此次操作影响的记录数;属于写操作数据类型MySQL中定义数据字段
4、的类型对你数据库的优化是非常重要的。MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。数值类型日期和时间类型字符串类型整型tinyint,占1字节,有符号:-128127,无符号位:0255smallint,占2字节,有符号:-3276832767,无符号位:065535mediumint,占3字节,有符号:-83886088388607,无符号位:016777215int,占4字节,有符号:-21474836482147483647,无符号位:04284967295bigint,占8字节bool 等价于tinyint(1) 布尔型浮点型float(m,d) 占4
5、字节,1.17E-383.4E+38 double(m,d) 占8字节decimal(m,d) 以字符串形式表示的浮点数字符型char(m):固定长度的字符,占用m字节varchar(m):可变长度的字符,占用m+1字节,大于255个字符:占用m+2tinytext,255个字符(2的8次方)text,65535个字符(2的16次方)mediumtext,16777215字符(2的24次方)longtext,(2的32次方)enum(value,value,.)占1/2个字节 最多可以有65535个成员set(value,value,.)占1/2/3/4/8个字节,最多可以有64个成员常用SE
6、LECT命令使用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_engin
7、e%; 默认的存储引擎like模糊搜索还可用户where字句,例如select * from students where stname like %l%1%2%3%;除了like 还有not likeshow engines;查看支持哪些存储引擎-查看系统运行状态信息show status;show global status like Thread%;多使用help导出,导入数据库导入数据库导入数据库前必须创建一个空数据库mysql -e create database book -uroot -p123456或者登陆 mysql create database book;导入(方法一)m
8、ysql -uroot -p123456 book 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 |+-+导出数据库导出数据库:mysqldump -u 用户名 -p 数据库名 导出的文件名mysqldump -u syst
9、em -p123456 bookbook2.sql扩展知识Mysqldump uroot p123456 B 库名文件.sql-B : 导出整个库包含建库语句-A:导出全部数据库如何把一个select的结果导出到文本select * into outfile /tmp/123.txt from books; 此处有个文件访问权限问题,mysql用户是可以访问/tmp路径的,所以这里放到tmp下select * from books into outfile /tmp/456.txt;其实就是备份数据库扩展: 5.7版本导出报错,可以设置f 加上secure-file-priv=/ SQL查询语
10、句进阶在我们刚导入的book数据库进行测试查看表的内容:mysql select * from category;mysql select * from books;mysql select * from booksG查看字段类型:desc 表名mysql desc books;逻辑运算符:and or notand 且or 或not 非选择出书籍价格为(30,40,50,60)的记录,只显示书籍名称,出版社,价格mysql select bName,publishing,price from books where price=30 or price=40 or price=50 or pr
11、ice=60;+-+-+-+| bName | publishing | price |+-+-+-+| Illustrator 10完全手册| 科学出版社| 50 | FreeHand 10基础教程| 北京希望电子出版| 50 | 网站设计全程教程| 科学出版社| 50 | ASP数据库系统开发实例导航| 人民邮电出版社| 60 | Delphi 5程序设计与控件参考| 电子工业出版社| 60 | ASP数据库系统开发实例导航| 人民邮电出版社| 60 |+-+-+-+算术运算符:=等于不等于 !=大于=大于等于 select bName,price from books where pri
12、ce60;找出价格为60的mysql select bName,price from books where price=60;找出价格不等于60的mysql select bName,price from books where price60;找出价格是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);排序:升序:order by
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 12 mysql sql 语句 进阶 精品 文档 整理
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内