2013年暑假做的一些存档
Mysql和Mssql好多不同。
用workbench又是各种报错,烦死了。。。
1. Mysql当中貌似没有INTO子句
create table weibo.weicontent (select * from weibo.weicontent limit 10);
2. Limit选出前多少行
SELECT * FROM weibo.weicontent limit 24 ;
3. 查看所占表空间
use information_schema;
select table_schema,table_name,table_type,engine,version,row_format,table_rows,avg_row_length ,
concat(round((data_length/1024/1024),3),'MB') as data_length_MB,
concat(round((index_length/1024/1024),3),'MB') as index_length_MB,
concat(round((max_data_length/1024/1024),3),'MB') as max_data_length
from tables where table_name = 'ontask'
不过这个得到的表行数好像是一个估计值。
4. delete,update, insert常常需要设置的
set SQL_SAFE_UPDATES = 0;
5. 三种Insert方式 insert ignore replace
普通,如果uid设为主键或者不可重复,则报错
insert into weibo.tempontask(uid) values ('1009285500');
重复则不插入
insert ignore into weibo.tempontask(uid) values ('1009285500');
重复则覆盖
replace into weibo.tempontask(uid) values ('1007629244');
6. Mysql自带的内存表 In memory 表
create table weibo.heap_ontask ( uid varchar(11) , primary key(uid) )
engine = memory;
设置内存表,查看空间
ALTER table weibo.heap_ontask MAX_ROWS=120000;
ALTER table weibo.heap_ontask Row_format=fixed;
show variables like "%max_heap%";
set max_heap_table_size=33554432; /*32M*/
7. 插入存储过程 procedure
delimiter // /*是必须的*/
DROP PROCEDURE IF EXISTS weibo.getOntask;
delimiter //
CREATE PROCEDURE weibo.Mark_Ontask(INOUT count_str VARCHAR(1000))
BEGIN
DECLARE temp VARCHAR(12);
DECLARE count_int INT;
SET count_int =CAST(count_str as SIGNED);
SET count_str='';
WHILE count_int>0 DO
set temp=(select uid from weibo.tempontask where flag is null limit 1);
set count_str=CONCAT(count_str,temp,':');
update weibo.tempontask set flag='T' where uid=temp;
SET count_int=count_int-1;
select temp;
END WHILE;
(SELECT count_str)
UNION
(SELECT count_int) ;
END;
//
delimiter
创建后,workbench里面相应数据库下面有个Routines可以查看和编辑它
使用它
SET @res='2';
call weibo.Mark_Ontask(@res);
8. 备份数据库或者表
mysqldump -uroot -pzcmysql -hlocalhost 数据库名 [数据表名] > xxx.sql