日期:2014-05-16  浏览次数:20808 次

mysql 使用总结
1: mysql :
   创建新的用户后,给用户赋予新的权利:
  1. grant all privileges on *.* to 'penny'@'%' identified by '1' with grant option;

2. 将数据库中数据导出放在指定目录中(在bin下执行,黑窗口要进入到bin下)



mysqldump -u penny -p ri5 -h PRCSGI03001  ref_reduction>D:/data/ref_reduction.txt
备注: 导出所有ri5 下表以及表的数据
mysqldump -u penny -p ri5 -h PRCSGI03001  PRCSGI03001> D:/database/data/tablecreationdump.txt

导出tablecreationdump 表的记录


3:  将已有的一个字段不为空,但是现在要求将它改为空。
  
alter table return_auth mofidy order_no varchar(10) null;

4. 根据日期选出其他的字段:
select a.return_item_id , a.update_dt from return_item a where year(a.update_dt)=2010 and month(a.update_dt>=08) adn 05<day(a.update_dt)<=09;

5.  一条有用的语句;
   update aa set iid = iid+1;

6: 有用SQL
    insert into ref_restock_item select div_no ,item_no from restock_eligible_wrk where restock_flag = 'Y';

7 .-->
  储存过程:
   create   procedure pro_return_auth()
begin 
    declare last_id int  ;
set last_id = 0;
select max(return_auth_id) into last_id from return_auth;
if last_id = 1234567892284 then
set last_id = 10000000000;
  insert into return_auth(id) values(last_id);
end if;
end  ;


delimiter //
create procedure pro_return_auth(in paramId integer)
begin 
declare last_id int;
if paramId =  2234 then
set last_id = 100;
  insert into test(id) values(last_id);
     alter table test AUTO_INCREMENT=100;
end if;
end;
//

5.创建表
create table ab(id string(10)primary key auto_increment,
    name varchar(10) );
drop table aa
insert into aa values(null,'aa',1);
update aa set iid=iid+1;

insert into return_return_temp   
select div_no,item_no from
               return_auth
               where return_auth_id < = 10000 ;
6.创建触发器:

CREATE TRIGGER test_trigger
    BEFORE
    insert ON return_instant_rebate
    FOR EACH ROW
    BEGIN
    IF (select max(return_instant_rebate_id) from return_instant_rebate)>=14 THEN

    delete from return_instant_rebate where return_instant_rebate_id=6;

    END;


7. ***增加语句(从别的表里选出来的)

insert into return_return_temp   
select div_no,item_no from
               return_auth
               where return_auth_id < = 10000 ;


insert into 表a select col_name1,col_name2...(字段名) from
表b where 条件将表b里东西插到表adelete from 表b where 条件

8.在mysql中使用触发器
MySQL 触发器简单实例
~~语法~~
CREATE TRIGGER <触发器名称>  --触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中
其他对象的命名方式基本相象.
{ BEFORE | AFTER }  --触发器有执行的时间设置:可以设置为事件发生前或后。
{ INSERT | UPDATE | DELETE }  --同样也能设定触发的事件:它们可以在执行insert、update或delete的过程中触发。
ON <表名称>  --触发器是属于某一个表的:当在这个表上执行插入、 更新或删除操作的时候就导致触发器的激活.
我们不能给同一张表的同一个事件安排两个触发器。
FOR EACH ROW  --触发器的执行间隔:FOR EACH ROW子句通知触发器 每隔一行执行一次动作,而不是对整个表执行一次。
<触发器SQL语句>  --触发器包含所要触发的SQL语句:这里的语句可以是任何合法的语句, 包括复合语句,
但是这里的语句受的限制和函数的一样。

--你必须拥有相当大的权限才能创建触发器(CREATE TRIGGER),如果你已经是Root用户,那么就足够了。这跟SQL的标准有所不同。


~~