美文网首页
NoSql与Sql的比较学习

NoSql与Sql的比较学习

作者: 夏薇雨茉 | 来源:发表于2018-07-16 17:34 被阅读12次

    一、通过后台连接数据库

    sql:mysql -uroot -pqwe123

    mongodb:在mongodb/bin目录下,输入mongo

    二、查看所有数据库

    mysql:show databases;

    mongodb:show dbs

    三、创建数据库

    mysql:create 库名;

    mongodb:use 库名;新建+切换

    查看当前所在数据库:

    mongodb:db

    四、表/集合的创建、查看、删除

    1、创建

    mysql:CREATE TABLE user(id int(5) primary key,name varchar(10),age int(3));

    mongodb:db.createCollection('user')

    2、查看

    mysql:show tables;

    mongodb:show collections

    3、删除

    mysql:DROP TABLE table_name ;

    mongodb:db.collection.drop()

    五、数据/文档的增、删、改、查

    1、增加数据

    mysql:INSERT INTO user( id, name,name ) VALUES

                          ( 1, 'gcf',26 );

    mongodb:db.user.insert({'name':'gcf','age':'26'})

    2、查询

    1)查询全部

    mysql:select * from user;

    mongodb:db.user.find().pretty();----格式化

    2)带条件查询

    mysql:select * from user where +条件;

    eg:

    select * from user where name = 'gcf';

    select * from user where name = 'gcf' and age = 26;

    select * from user where name = 'gcf' or age = 32;

    mongodb:

    eg:

    db.user.find({'name':'gcf'});

    db.user.find({'name':'gcf','age':'26'});

    db.user.find({$or:[{'name':'gcf'},{'age':'32'}]}).pretty();

    3、更新数据

    mysql:update user set age = age+4 where id = 1;

    mongodb:db.user.update({'age':'32'},{$set:{'age':'28'}});  ---只会更新一条相同数据

    db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true}); ---更新多条

    4、删除数据

    mysql:删除表中的所有数据: delete from students;

    delete from user where name = 'gcf';

    mongodb:db.user.remove();---全部删除

    db.user.remove({'name':'gcf'});---删除姓名为gcf的文档

    相关文章

      网友评论

          本文标题:NoSql与Sql的比较学习

          本文链接:https://www.haomeiwen.com/subject/pbmbpftx.html