MySQL登陆方式、数据类型、数据引擎及SQL查询语句,注意的问题.

1.  连接到服务器上面有三种方式:

   1.1  通过mysql自己的客户端:
          在开始  -----所有程序--mysql--mysql Command Line Client 让你输入密码 那么该密码是root用户的密码
   1.2  在dos命令行中使用mysql.exe客户端连接上
        mysql.exe -hlocalhost -uroot -padmin
       -h 连接的主机
       -u 数据库服务器的用户名
       -p 用户名对应的密码

        注意: 如果通过dos 来执行select 或者是 insert into  的语句 ,如果出现了乱码,可以设置编码为GBK来解决,在mysql环境中设置: SET NAMES 'GBK';


   1.3 通过Navicat客户端连接到数据库服务器上面

2. 常用的sql命令
    2.1 创建数据库
         create database  数据库的名字
     
      如:  create database  myjdbc
    2.2 删除数据库
          drop  database 数据库的名字

       如: drop database myjdbc

  2.3 查看数据库
      show databases
 
  3.4 使用数据库
      use database 数据库的名字
   如: use database myjdbc

 3. 表
 
  数据库中的数据类型:

char(size)   
定长字符,0 - 255字节    如: char(2)        aaaaa -> aa          a -> a
size: 不管放什么,都是两个字符,如果放入的字符数超出了指定的长度,那么就截取, 如果小于指定的字符数,那么就用空字符填充

varchar(size)     变长字符,0 - 255字节  如: varchar(2)    aaaaa -> aa        a -> a
size:表示的是最大的字节数,   如果放入的字符数超出了指定的长度,那么就截取, 如果小于指定的字符数, 那么就放这些字符

注:JAVA中的一个英文字母站一个字节,而一个汉字在GBK编码站两个字节,在UTF-8编码中占三个字节.

如:'a'  1节
"中"  在GBK的编码中占 2个字节
"中" 在UTF-8的编码中占3个字节

date    日期数据,MySQL用'yyyy-MM-dd'格式检索和显示DATE值      'yyyy-MM-dd'

DATETIME     日期数据,要比date数据更确切,包含时分秒。MySQL以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值

Int(size)     整型数据(size是显示的宽度)     和int类型存储一个数据库的空间是没有任何关系的

double[(s,p )]     数字型,可存放实型和整型 ,精度(p )和范围(s)

s:  指的是数据的总位数
p: 指的是小数点后面的位数

例如: double(2,2 )  最大值为:0.99  最小值为:-0.99
          double(3,2)   最大值为:9.99    最小值为 -9.99

blob     存放图形、声音和影像,二进制对象,0-4GB
通常不用把二进制文件存储在数据库中的, 而是存储在硬盘上面,而是数据中存入该文件的地址,   程序可以通过该地址找到这个文件读取..
 
text      存放大文本文件, 0-4GB

建表语法:
   create table table_name
   (
        column_name1 column_properties constraint_definition,
      column_name2 column_properties constraint_definition,
      #列名 类型 约束 (最后一行没有逗号)
    )
   


create  table student(
      name  char(8),
      sex  char(2),
      age int,
      birthday date
)

4.Select查询语句
    SELECT

 最简单的select语句

SELECT    {*, column [alias],...}
FROM        table;


如果为 * 和创建表时的顺序一致。
可以自己调整顺序,在select后边加上要查询的列名。

货品表的DDL
CREATE TABLE `product` (
  `id` bigint(11) NOT NULL auto_increment,     货品的id
  `productName` varchar(50) default NULL,    货品的名字
  `dir_id` bigint(11) default NULL,                    货品分类的编码
  `salePrice` double(10,2) default NULL,         零售价     100
  `supplier` varchar(50) default NULL,            供应商
  `brand` varchar(50) default NULL,               品牌
  `cutoff` double(2,2) default NULL,               折扣        主要是给零售商的批发价=== 零售价*折扣
  `costPrice` double(10,2) default NULL,        成本价
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


 4.1  查询出货品表中的所有的货品信息
  select  *    from  product
 4.2  查询出货品表中所有 供应商, id, 零售价,货品名称
    select supplier,id,salePrice,productName  from product

 4.3 查询货品的id,货品名字,零售价,折扣,批发价
  select id,productName,salePrice,cutoff, salePrice*cutoff     from product

 
 别名:

(1)、改变列的标题头
(2)、用于表示计算结果的含义
(3)、作为列的别名
(4)、如果别名中使用特殊字符,或者是强制大小写敏感,或有空格时,都需加双引号

4.4  使用别名    查询货品的id,货品名字,零售价,折扣,批发价

 select id as 编号 ,productName as  货品名字,salePrice as 零售价,cutoff  as  折扣 , salePrice*cutoff  as 批发价     from product

或者   as 可以省略
  select id  编号 ,productName   货品名字,salePrice  零售价,cutoff    折扣 , salePrice*cutoff   批发价     from product

  如果别名中有特殊的符号,需要用引号"" 或者  ''  引起来
  select id  "编   号" ,productName   "货品名字",salePrice " 零   售价",cutoff   "折  扣" , salePrice*cutoff   "批发价"     from product

4.5  使用别名    查询货品的id,货品名字,零售价,成本 并且各进50个的总成本
     select id  "编   号" ,productName   "货品名字",salePrice " 零   售价",costPrice  "单个成本", costPrice*50 "50各成本"    from product
4.6  使用别名    查询货品的id,货品名字,零售价,成本 并且各进50个,每个的运费为1元的总成本

  select id  "编   号" ,productName   "货品名字",salePrice " 零   售价",costPrice  "单个成本", (costPrice+1)*50  "50各成本"    from product

5.使用WHERE子句限定返回的记录
      SELECT    [DISTINCT] {*, column [alias], ...}     FROM     table
[WHERE    condition(s)];


WHERE子句在 FROM 子句后.

 5.1  查询货品零售价为119的所有货品信息
       select *   from product  where salePrice=119


 5.2  查询货品的分类编号为2  货品信息的id,货品名字,分类编号,品牌
       select id,productName,dir_id,brand from product where dir_id=2

5.3. 查询出货品名字为  罗技M905  货品信息的id,货品名字,分类编号,品牌

     select id,productname,dir_id,brand from product where productName='罗技M905'

     select id,productname,dir_id,brand from product where   binary  productName='罗技m905'

    如果需要大小敏感的话, 有两种方法:

第一种方法:

要让mysql查询区分大小写,可以:

select * from some_table where binary str='abc'

select * from some_table where binary str='ABC'

第二方法:

在建表时时候加以标识

create table some_table(

     str char(20) binary 

)


5.4 查询分类编号不等于2的货品信息
   select * from product  where dir_id <> 2


5.5查询货品名称,零售价小于等于200的货品
   select productName,salePrice from product where salePrice<=200

 5.6查询id,货品名称,批发价大于350的货品 

   select id, productName, salePrice*cutoff as 批发价  from product  where   salePrice*cutoff >350
  
   select id, productName, salePrice*cutoff as 批发价  from product  where   批发价 >350    是错误的..

  总结:  where语句先执行,   select后执行..  先过滤出满足条件的数据, 然后再查询出来

使用BETWEEN 运算符
使用BETWEEN运算符显示某一值域范围的记录,这个操作符最常见的使用在数字类型数据的范围上,但对于字符类型数据和日期类型数据同样可用   ,包含边界值

  5.7. 选择id,货品名称,批发价在300-400之间的货品
    select id,productName,salePrice*cutoff from product where salePrice*cutoff  BETWEEN   300    and   400

使用IN 运算符:
使用IN运算符获得匹配列表值的记录,在IN操作符后跟着一个值的列表,可以应用日期,字符串数据类型

  5.8. 选择id,货品名称,分类编号为2,3,4的所有货品
   select id,productName,dir_id from product where dir_id in(2,3)

使用LIKE 运算符

使用LIKE运算符执行通配查询
查询条件可包含文字字符或数字
(%) 可表示零或多个字符
( _ ) 可表示一个任意字符

  5.8. 选择id,货品名称,品牌  , 那么货品名字中有    罗技M  的货品
      select id,productName,brand from product  where productName like  '%罗技M%'

  5.8. 选择id,货品名称,品牌  , 那么货品名字中有    罗技M__    的货品
      select id,productName,brand from product  where productName like  '罗技M%'

注意:
A:罗技%
B:罗技_%      

  这两个的区别为:   B 不能够查询出来 罗技   这两个字   


6 .  逻辑运算符

  6.1    选择id,货品名称,分类编号,零售价大于等于200并且货品名称匹配'%罗技M1_'
     select id,productName,dir_id,salePrice from product where  salePrice>=200  and   productName like "%罗技M1__"

 6.2  选择id,货品名称,批发价在300-400之间的货品
     select id,productName,salePrice*cutoff from product where salePrice*cutoff>=300 and salePrice*cutoff<=400
 
6.3  选择id,货品名称,分类编号的货品零售价大于等于250或者是成本大于等于200
     select id,productName,dir_id,salePrice,costPrice from product where salePrice>=250  or costPrice>=200
6.4  选择id,货品名称,分类编号为2,3,4的所有货品
      select id,productName,dir_id from product where dir_id=2 or dir_id=3 or dir_id=4


优先级的事例:   

SELECT id,productName FROM product WHERE NOT productName LIKE '%M%' OR dir_id = 2 AND salePrice > 100

SELECT id,productName FROM product WHERE NOT (productName LIKE '%M%' OR dir_id = 2) AND salePrice > 100

SELECT id,productName FROM product WHERE NOT (productName LIKE '%M%' OR dir_id = 2 AND salePrice > 100)

优先级顺序:NOT>AND>OR


7. 排序:

   7.1  选择id,货品名称,分类编号,零售价并且按零售价排序升序
   select id,productName,dir_id,salePrice from product  order by salePrice asc     默认 升序
 
   select id,productName,dir_id,salePrice from product  order by salePrice  desc



   7.2  选择id,货品名称,分类编号,零售价先按分类编号排序,再按零售价排序
 
           select id,productName,dir_id,salePrice from product order by dir_id  ,salePrice   默认升序
 
       select id,productName,dir_id,salePrice from product order by dir_id desc ,salePrice desc   升序


7.3   查询M系列并按照批发价排序(加上别名)
        select id,productName,salePrice*cutoff as pfprice from product    where   productName like '%M%'   order by pfprice   

           where先执行, 后执行select 再执行 order by


7.4 查询分类为2并按照批发价降序排序(加上别名)

   select id,productName,dir_id,salePrice*cutoff as pfprice from product    where   dir_id=2   order by pfprice   desc


8. 多表查询:
   8.1.    查询所有的货品信息+货品分类信息

    select * from product,productdir


   8.2.    查询所有的货品信息 并且 对应的每个货品的货品分类信息

    select * from product,productdir where product.dir_id=productdir.id


 使用表连接从多个表中查询数据
SELECT    table1.column, table2.column
FROM        table1, table2
WHERE    table1.column1 = table2.column2;

在 WHERE 子句中写入连接条件

当多个表中有重名列时,必须在列的名字前加上表名作为前缀

    select   product.id,productdir.id from product,productdir where product.dir_id=productdir.id

     如果不是使用表名的话, 可以给表另外起一个别名,通过别名来区分

    select   p.id,pd.id from product as p,productdir as pd  where p.dir_id=pd.id


 主键:  如果一个列上面设置了主键,那么该列中的值不能够重复

 外键:  如果设置一个列为外键的话, 那么该列中的值是从参照表中来的..


对于mysql来说有两种存储引擎 :
 1. InnoDB(第三方公司)       支持数据一致性的
      a.  支持事务
      b.  支持外键
 
2. MyISAM (mysql公司自己开发)    支持不数据一致性的
   a. 不支持事务
   b. 不外键

注意:在设置外键的时候可能出错,那么可能是存储引擎没有设置为InnoDB.

  要是用外键必须使用InnoDB这种存储引擎:
alter table product ENGINE = 'InnoDB'
alter table productdir ENGINE = 'InnoDB'


8.3  查询零售价大于200的无线鼠标

  select * from product as p ,productdir as pd where p.dir_id=pd.id  and p.salePrice>200 and pd.dirName='无线鼠标'


1,查询货品名称,零售价,批发价,货品分类,货品库存,库存成本总价

 select   p.productName,p.salePrice,p.cutoff*p.salePrice as  pfprice, pd.dirName,ps.storeNum, ps.storeNum*p.costPrice as totalPrice   from   product  p ,productdir pd ,productstock ps  where p.dir_id=pd.id  and  p.id= ps.product_id
  

2,查询货品名称,零售价,货品分类,库存不足的无线鼠标

  select   p.productName,p.salePrice,pd.dirName  from   product  p ,productdir pd ,productstock ps  where p.dir_id=pd.id  and  p.id= ps.product_id and ps.storeNum<ps.warningNum and dirName='无线鼠标'

3,按照库存数量排序查询货品名称,成本价,数量,库存成本总价

   select  p.productName,p.costPrice, ps.storeNum,  ps.storeNum*p.costPrice  as totalPrice     from product p, productstock   ps where p.id=ps.product_id  order by  ps.storeNum

4,如果库存货品都销售完成,按照利润从高到低查询货品名称,零售价,货品分类      站零售商的角度
 
  select     p.productName,p.salePrice,pd.dirName,    (p.salePrice -  p.cutoff*p.salePrice)*ps.storeNum as lirun     from   product  p ,productdir pd ,productstock ps  where p.dir_id=pd.id  and  p.id= ps.product_id   order by lirun  desc

猜你喜欢

转载自dendy.iteye.com/blog/1674665