修改数据表

添加字段:

alter  table  表名 add  字段名  列类型 [not null|null][primary key][unique][auto_increment][default value]

alter  table  表名 add  字段定义  after ar_id;

删除字段:

alter table 表名 drop 字段名

修改字段:

alter table 表名 modify 字段名 字段新类型

完整修改字段:

alter table 表名  change 旧字段名称  新字段定义

修改表名称

alter table 表名 rename 新名字

删除表

drop table [if (not) exists] 表名;

表中行的操作

insert [into] 数据表名称 [(字段列表)] values|value (表达式|null|default,...),(表达式|null|default,...)

insert [into] 数据表名称 set 字段名称=值,...

insert与insert...set的区别是后者可以带有子查询

update 表名 set 字段名称=值,... [where 条件]

如果省略WHERE条件将更新全部记录

delete from 数据表名称 [where 条件]

如果省略where条件,将删除全部记录

select 字段列表 from 数据表 [[as] 别名] [where 条件]

别名的用法:

Select * from 数据表 [[as] 别名]

字段名称 [[as]别名]

Select product_offer_instance_object_id as ID, product_offer_instance_object_name name,coumn33 ‘金额’From table

select btypeid as '图书类别ID',btypename as '图书类型' from category;

Mysql-SQL语句进阶(一)

select语句返回零条或多条记录;属于记录读操作

insert、update、delete只返回此次操作影响的记录数;属于写操作

数据类型

  • MySQL 中定义数据字段的类型对你的数据库的优化非常重要的 。
  • MySQL 支持多种类型,大致分为三类:数值、日期/时间和字符串(字符)类型。

数据类型

Mysql-SQL语句进阶(一)


日期和时间类型

Mysql-SQL语句进阶(一)


字符串类型

Mysql-SQL语句进阶(一)

整型

  • tinyint,占1字节,有符号:-128~127,无符号位:0~255
  • smallint,占2字节,有符号:-32768~32767,无符号位:0~65535
  • mediumint,占3字节,有符号:-8388608~8388607,无符号位:0~16777215
  • int,占4字节,有符号:-2147483648~2147483647,无符号位:0~4284967295
  • bigint,占8字节
  • bool 等价于 tinyint(1) 布尔型

浮点型

  • float([m[,d]]) 占4字节,1.17E-38~3.4E+38
  • double([m[,d]]) 占8字节
  • decimal([m[,d]]) 以字符串形式表示的浮点数

字符型

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

  • varchar[(m)]:可变长度的字符,占用m+1字节,大于255个字符:占用m+2

  • tinytext,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个成员

常用 SELECT 命令

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

--  打印当前的日期和时间

select  now();


--  打印当前的日期

select  curdate();


--  打印当前的时间

select  curtime();


--  打印当前数据库

select database();


--  打印 MySQL 版本

select version();


--  打印当前用户

select  user();


--  查看系统信息

show variables \G

show global variables \G

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  engines;    查看支持哪些存储引擎


--  查看系统运行情况

show status;

show global status like 'Thread%';


导入数据库

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

mysql -e 'create database book' -u -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          |

| catego

+----------------+


导出数据库

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

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版本导出报错,可以设置my.cnf 加上secure-file-priv="/ "

SQL 查询语句进阶

查看表内容

mysql> select * from category;

mysql> select * from books;

mysql> select * from books\G

查看字段类型:

desc 表名

mysql> desc books;

逻辑运算符:

and   

or     

not   


例:选择出书籍价格为(30,40,50,60)的记录,只显示书籍名称,出版社,价格

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 |

+--------------------------------------+--------------------------+-------+

算术运算符:

=     等于

<>   不等于  !=

>     大于

<     小于

>=   大于等于

<=   小于等于

in    运算符

IN 运算符用于 WHERE 表达式中,以列表项的形式支持多个选择,语法如下:

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

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

Not in 与in相反

当 IN 前面加上 NOT 运算符时,表示与 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);

排序

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

  • 降序:oredr by  “排序的字段” desc

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;


 

分类:

技术点:

相关文章: