楼主: mchdba

MySQL知识点技能点总结贴--个人版!

[复制链接]
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
101#
 楼主| 发表于 2011-11-18 11:23 | 只看该作者


#存储嵌套的对象
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
#存储数组对象
db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
#根据query条件修改,如果不存在则插入,允许修改多条记录
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
#删除yy=5的记录
db.foo.remove({'yy':5})
#删除所有的记录
db.foo.remove()



-- 索引

#增加索引:1(ascending),-1(descending)
db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
#索引子对象
db.user_addr.ensureIndex({'Al.Em': 1})
#查看索引信息
db.deliver_status.getIndexes()
db.deliver_status.getIndexKeys()
#根据索引名删除索引
db.user_addr.dropIndex('Al.Em_1')

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
102#
 楼主| 发表于 2011-11-18 11:23 | 只看该作者
== 查询 详细见官方网站:http://www.mongodb.org/display/DOCS/Advanced+Queries

#查找所有
db.foo.find()
#查找一条记录
db.foo.findOne()
#根据条件检索10条记录
db.foo.find({'msg':'Hello 1'}).limit(10)
#sort排序
db.deliver_status.find({'From':'yushunzhi@sohu.com'}).sort({'Dt',-1})
db.deliver_status.find().sort({'Ct':-1}).limit(1)
#count操作
db.user_addr.count()
#distinct操作
db.foo.distinct('msg')
#>操作
db.foo.find({"timestamp": {"$gte" : 2}})
#子对象的查找
db.foo.find({'address.city':'beijing'})
– 分页查询
db.user.find().skip(10).limit(20)
– in查询
db.user.find({‘age’ : {$in : [25, 35, 45]}})
– 查询排序
db.user.find().sort({‘age’ : -1})

– group
db.user.group({
key : {‘name’ : true},
cond: {‘name’ : ‘foo’},
reduce: function(obj,prev) { prev.msum += obj.marks; },
initial: {msum : 0}
});

相当于SELECT name, sum(marks) FROM user GROUP BY name

– 动态查询
db.user.find(‘this.age < 20′, {name : 1})

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
103#
 楼主| 发表于 2011-11-18 11:24 | 只看该作者

-- 管理
#查看collection数据的大小
db.deliver_status.dataSize()
#查看colleciont状态
db.deliver_status.stats()
#查询所有索引的大小
db.deliver_status.totalIndexSize()


--创建集合
> db.createCollection("user");
--录入数据
> db.user.insert({uid:1,username:"Falcon.C", age:25});
> db.user.insert({uid:2,username:"aabc", age:24});
> db.user.insert({uid:2,username:"ttcv", age:24});
> db.user.insert({uid:1,username:"Falcon.C", age:25, year:1982});
> db.user.insert({uid:2,username:"aabc", age:24, year:1983});
> db.user.insert({uid:2,username:"ttcv1", age:23, year:1984});
> db.user.insert({uid:2,username:"ttcv2", age:25, year:1984});
--录入多属性数据
> db.user.insert({uid:2,username:"李三", age:25, year:1984, area:"beijing", attr:1});
> db.user.insert({uid:2,username:"李三", age:25, year:1984, area:"beijing", country:'CICO', attr:2});
> db.user.insert({uid:2,username:"李三", age:25, year:1984, area:"shanghai", product:'P1', attr:3});
> db.user.insert({uid:2,username:"李三", age:25, year:1984, area:"hangzhou", attr:3});
> db.user.insert({uid:2,username:"李三", area:"shengzhen", age:25, year:1984, unit:'RMB', attr:3});
> db.user.insert({uid:2,username:"李三", age:25, year:1984, attr:2});
> db.user.insert({uid:2,username:"李三", age:25, attr:1});
> db.user.insert({uid:2,username:"李三", attr:0});

--查询数据
> db.user.find();
> db.user.find({uid:1});
> db.user.find({"username":"日志"});
--多属性查询
> db.user.find({"username":"李三","age":26, year:1983});
> db.user.find({"username":"李三","age":25});
> db.user.find({"username":"李四"});
-- 查询x、b属性必须存在,attr字段=3的记录。
> db.c1.find({x:{'$exists':true},b:{'$exists':true}, attr:3});

--// get all posts about 'tennis' but without the comments field
> db.posts.find( { tags : 'tennis' }, { comments : 0 } );
> db.c1.find({b:2},{x:0});
> db.c1.find({b:2},{x:null});


-- 二层查询,多层次查询
> t.find({})
{ "_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1, "z" : [ 1, 2, 3 ] } }
> t.find({}, {'x.y':1})
{ "_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1 } }


-- 分页
db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10



--倒序
> db.user.find({}).sort({uid:-1});
--升序
> db.user.find({}).sort({uid:1});
-- 双排序 失败
> db.user.find({}).sort({uid:-1},{username:-1});

--更新数据
> db.user.update({uid:1},{$set:{age:26}});
--在原来的基础上减1
> db.user.update({uid:1},{$inc:{age:-1}})
更新的条件还有$unset、$push 、$pushAll 、$pop 、$pull 、$pullAll等等
-- 添加一个新的节点age
> db.c1.update({a:1},{$set:{age:25}});
-- 移除一个节点
> db.c1.update({a:1},{$unset:{age:25}});


====================================================================================
> t = db.mycoll;
> t.insert({x:3});
> t.insert( {x : 2.9} );
> t.insert( {x : new Date()} );
> t.insert( {x : true } )
> t.find().sort({x:1})

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
104#
 楼主| 发表于 2011-11-18 11:24 | 只看该作者
=====================================================================================
-- Database Profiler 数据库探查器
> db.setProfilingLevel(2);
> db.getProfilingLevel();
--查看所有记录
> db.system.profile.find();
--查看特定的记录
> db.system.profile.find( { info: /test.foo/ } )
--查看超过一定时间的记录
> db.system.profile.find( { millis : { $gt : 5 } } )
--查看最新的信息
> db.system.profile.find().sort({$natural:-1})

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
105#
 楼主| 发表于 2011-11-18 11:25 | 只看该作者


=====================================================================================
先锁表、快照、解锁表

> use admin
switched to db admin
> db.runCommand({fsync:1,lock:1})
{
        "info" : "now locked against writes, use db.$cmd.sys.unlock.findOne() to unlock",
        "ok" : 1
}
> db.currentOp()
{
        "inprog" : [ ],
        "fsyncLock" : 1,
        "info" : "use db.$cmd.sys.unlock.findOne() to terminate the fsync write/snapshot lock"
}
>// do some work here: for example, snapshot datafiles.
>// runProgram("/path/to/my-filesystem-snapshotting-script.sh")


> db.$cmd.sys.unlock.findOne();
> //解锁失败 ?
{ "ok" : 1, "info" : "unlock requested" }
> db.currentOp();
{ "inprog" : [ ] }
>

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
106#
 楼主| 发表于 2011-11-18 11:25 | 只看该作者


====================================================================================
-- DBRef in Different 数据关联
> x = { name : 'Biology' }
{ "name" : "Biology" }
> db.courses.save(x)
> x
{ "name" : "Biology", "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }
> stu = { name : 'Joe', classes : [ new DBRef('courses', x._id) ] }
// or we could write:
// stu = { name : 'Joe', classes : [ {$ref:'courses',$id:x._id} ] }
> db.students.save(stu)
> stu
{
        "name" : "Joe",
        "classes" : [
                {
                        "$ref" : "courses",
                        "$id" : ObjectId("4b0552b0f0da7d1eb6f126a1")
                }
        ],
        "_id" : ObjectId("4b0552e4f0da7d1eb6f126a2")
}
> stu.classes[0]
{ "$ref" : "courses", "$id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }
> stu.classes[0].fetch()
{ "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1"), "name" : "Biology" }
>

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
107#
 楼主| 发表于 2011-11-18 11:25 | 只看该作者


-- 验证集合collection的命令
> db.c1.validate();
> db.$cmd.findOne({validate:"c1"})

=====================================================================================
-- Updating Data in MongoDB 集合嵌入 collection embedding

> var other = { s : "other thing", n : 1};
> db.otherthings.save(other);
> db.otherthings.find();
> var mongo = db.things.findOne();
> print(tojson(mongo));
> mongo.otherthings = new DBRef( 'otherthings' , other._id );
> db.things.save(mongo); --开始嵌入
> db.things.findOne().otherthings.fetch();
-- update 嵌入集合
> other.n = 2;
> db.otherthings.save(other);
> db.otherthings.find();
> db.things.findOne().otherthings.fetch();

使用道具 举报

回复
论坛徽章:
27
优秀写手
日期:2013-12-18 09:29:09ITPUB季度 技术新星
日期:2012-08-15 14:50:13嫦娥
日期:2013-03-04 09:47:05数据库板块每日发贴之星
日期:2011-09-08 01:01:01数据库板块每日发贴之星
日期:2011-09-11 01:01:01数据库板块每日发贴之星
日期:2011-09-10 01:01:02数据库板块每日发贴之星
日期:2011-09-09 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:26:59ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:422013年新春福章
日期:2013-03-04 09:50:49
108#
 楼主| 发表于 2011-11-18 14:28 | 只看该作者
--  数据从sqlserver迁移到mysql需要注意的地方:

1,分页存储过程,sqlserver里面是双top,mysql里面是limit。
2,存储视图,存储过程以及存储函数都不太一样,需要更新。
3,数据库调度任务event不一样,需要更新。
4,泠热分离
5,纵切 横切
6,备份

使用道具 举报

回复
论坛徽章:
26
ITPUB伯乐
日期:2012-05-22 15:05:35喜羊羊
日期:2015-05-14 12:15:30美羊羊
日期:2015-07-02 22:22:59秀才
日期:2015-08-24 09:51:48天枰座
日期:2015-11-05 16:53:35双鱼座
日期:2015-12-05 19:22:56巨蟹座
日期:2015-12-20 18:46:10摩羯座
日期:2016-05-19 09:54:17ITPUB15周年纪念
日期:2016-10-13 13:15:34秀才
日期:2017-02-22 15:16:26
109#
发表于 2012-1-6 13:25 | 只看该作者
mchdba 发表于 2011-11-18 14:28
--  数据从sqlserver迁移到mysql需要注意的地方:

1,分页存储过程,sqlserver里面是双top,mysql里面是 ...

呵呵 现在正在尝试从sqlserver迁移到mysql.

使用道具 举报

回复
论坛徽章:
0
110#
发表于 2012-2-7 14:49 | 只看该作者
学习学习

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表