mysql 数据操作命令汇总 hibernate操作

转载其他人的文章,谢谢合作:

http://www.jb51.net/article/59623.htm

http://www.yiibai.com/mysql/mysql_like_clause.html

http://blog.163.com/budong_weimin_zh/blog/static/12919852420115130484204/


http://www.cnblogs.com/greatverve/archive/2011/11/10/mysql-summary.html

http://www.cnblogs.com/52fhy/p/5831589.html

http://www.cnblogs.com/52fhy/p/5269318.html  账户安全

 

 

http://blog.csdn.net/daryl715/article/details/1916060   hibernate ORM   级联关系映射操作案例 好的

 http://www.cnblogs.com/greatverve/archive/2011/11/10/mysql-summary.html

==========================================================

 

distinct 去掉重复的数据行,只有一航。

select distinct  realname as 姓名 ,  password as 密码 from user;


返回表中从上到下的 N行数据:

select top 2  realname as 姓名 ,  password as 密码 from user;  false;

select   realname as 姓名 ,  password as 密码 from user limit 1;

select   realname as 姓名 ,  password as 密码 from user limit 0 , 2;

select  realname as 姓名  from user where username=2;


select  realname as 姓名  from user where username<3;

select  realname as 姓名  from user where username<3  and  表达式;

select  realname as 姓名  from user where username in(1,2); 和上面等价,也是查询

select  realname as 姓名  from user where username  not  in(1,2,2016);


select  *  from user where username  between 1 and 2016;

select  *  from user where username  between 2 and 2016; 范围以内的数据

select  *  from user where username  not  like '马%';
                         where  列明 is not null ;

结果集合数据按照从低到高的正序 或者从高到低的倒叙排序。
select  *  from user where username ,列明,order  by 列明 asc,列明  desc ;

select  *  from user  order  by username  asc  ;

insert  into user values( 3 , '沈晶晶' , 1245 ,  1  );


对每列中的数据进行分组查询:
select  *, count(realname) as 人数  from user group by role ;


join  多种的连接查询: 默认是  内连接(可能存在信息丢失)

select   from  table  join-type join  table2  on  jion-conditions  where search-conditions
order  by  order-expression

例如:
select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息  join  订单信息
on    商品信息.商品编号 = 订单信息.商品编号
where 商品信息.商品价格 <350

select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息 inner join  订单信息
on    商品信息.商品编号 = 订单信息.商品编号
order by 商品信息.商品价格

select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息 inner join  订单信息
on    商品信息.商品编号 = 订单信息.商品编号
and 订单信息.商品价格  between  250  and 350
order by 商品信息.商品价格

以上3条可能存在数据行重复现象。

自然连接:
select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息  join  订单信息
on    商品信息.商品编号 = 订单信息.商品编号
结果集中: 删除连接表中重复列。

 

左外连接:
以主表中的数据为标准  去匹配从表中的数据行,如果没有匹配,从表中的数据行被填上

NULL, 且返回数据。
select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息(主表)  left outer join  订单信息(从表)
on    商品信息.商品编号 = 订单信息.商品编号

 


右外连接:
以主表中的数据为标准  去匹配从表中的数据行,如果没有匹配,从表中的数据行被填上

NULL, 且返回数据。
select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息(从表)  right outer join  订单信息(主表)
on    商品信息.商品编号 = 订单信息.商品编号


全外连接:
以主表中的数据为标准  去匹配从表中的数据行,如果没有匹配,从表中的数据行被填上

NULL, 且返回数据。 相互的返回整个数据
select  商品信息.商品名称  ,商品信息.商品价格,  订单信息,交易数量 
from 商品信息(主表) full outer join  订单信息
on    商品信息.商品编号 = 订单信息.商品编号(从表)

 


自连接:对同一个表进行连接查询。需要定义别名,其他操作一样。

select  A.商品名称  ,B.商品价格,A.商品价格,  B.库存数量 
from 商品信息 as A  inner join  商品信息 as B
on    A.商品编号 = B.商品编号
where A.商品价格  between  200 and 300
order by A.商品价格

 

联合查询:把 查询结果集 再次连接到一起。

select  制造商名称  as  ‘制造商地址’ ,  制造商电话  as  '传真电话'
from   制造商信息
union
select  供货商地址, 供货商电话
from   供货商信息

 

使用In 进行子查询:

select  * from 注册会员
where 会员编号  in
                      ( select    会员编号  from   订单信息  where  交易数量 >3 )


作为外围查询的触发条件的exists查询

select  * from 注册会员
where exists
                  ( select    会员编号  from   订单信息  where  交易数量 >3 )


比较运算符:

select  会员编号, 会员名称,会员地址   from 注册会员
where 会员编号  <= any  或者 all
                  ( select    会员编号  from   订单信息  where  交易数量 >3 )

 


返回单值的子查询:子查询的结果只返回一个值。

select  会员编号, 会员名称,会员地址   from 注册会员
where 会员编号  < 
                  ( select    会员编号  from   订单信息  where  商品价格=3356 )

 


嵌套的子查询:
select  会员名称  from 会员信息
where 会员编号  in(
                          select 会员编号 from 订单信息 where 订单编号 in
                                                                                         (select 订单编号

from   订单明细 id in (3,4,5,6)) )

 ---------------------------------------------------

create table a(

name char(20) not null,

id char(20) not null  primary key);

 

create table b(

b_name char(20) not null,

b_id char(20) not null ,

constraint foreign key(b_id) references a(id) on delete cascade);

insert into a values("111","1");

insert into a values("222","2");


在b表中插入下面数据 insert into b values("1","1"); 显示插入成功。

在b表中插入下面数据 insert into b values("1","3"); 显示插入失败。是因为这里设置了外键而a表中的id没(id=="3")的数据。

执行delete from a where id="1"; b表中的数据也会被删除掉。这里就应用到了级联删除。

 


CREATE TABLE `user` (
 `id` int(4) NOT NULL,
 `sex` enum('f','m') DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `userinfo` (
 `sn` int(4) NOT NULL AUTO_INCREMENT,
 `userid` int(4) NOT NULL,
 `info` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`sn`),
 KEY `userid` (`userid`),
 CONSTRAINT `userinfo_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

 

 ---------------------------------------

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值