Demo Day -10 (1)
Demo Day -10 (1)
to give permission-----grant
remove the permission------revoke
select
update
delete
insert
references
alter
index
all
grant select
on emp
to user_name
revoke select
on emo
from user_name
-----------------------------------------------
SQL NoSQL
---------------------- -------------
-structured query lag not only SQL
-structured data data is unstructured
-data is in the form of data is stored in rows and columns
document
-date accessing /processing faster
slow
-data is stored on 1 machine distributed data
-SQL serve there is vertical horizontal scaling
scaling.
-can case accessibility problem highly accessible
(data replication)
-database tables database collections
-column,field,attribute field,attribute
-row,record,tuple document
-pk created system gives pk
-constraints no constraints
-joins no joins
(nested documents)
-OLTP OLAP
online transaction processing online analytical
processing
display databases
-----------------
>show dbs
create database
---------------
>use empdb
will create new database. if existing will connect
current database
-----------------
>db
>db.dept.insert({dno:1,dname:'sales'})
db.dept.insert({dno:2,dname:'purchase',loc:'pune'})
db.dept.insert({dno:3,dname:'mkt',ph_no:999999})
db.dept.insertMany([{deptno:3,dname:'training'},
{dno:10,dname:'development',location:'Hydrabad'}])
display collections
-------------------
show collections
-----------------
db.dept.insertMany([{deptno:3,dname:'acc'},
{deptno:3,dname:'admin',location:'Hydrabad'}])
db.dept.findOne()
db.dept.find({deptno:3})
select * from dept where deptno=3;
db.dept.drop()
--------------------------
deleting database
>db.dropDatabase()
current database is deleted
-----------------------
create collection
>db.createCollection("collection_name")
>db.createCollection("emp")
db.emp.insertMany([{eno:1,ename:'Kiran',deptno:10},
{eno:2,ename:'Sanjay'},{eno:3,ename:'Smita',hobby:["reading","music"]}])
syntax of find
find({query},{projection})
db.emp.find({ename:'Kiran'},{deptno:1,_id:0})
1-inclusion
0-exclusion
db.emp.insertMany([{eno:4,ename:'Radha',deptno:10},
{eno:5,ename:'Sameer',deptno:10},{eno:6,ename:'Sarita',hobby:
["reading","music"],deptno:10}])
db.emp.find({deptno:10},{eno:1,ename:1,deptno:1,_id:0}).pretty()
operators in Mongo
-----------------
$lt ------ <
$gt ------- >
$eq ------- ==
$lte ------ <=
$gte ------- >=
$ne ------- !=
rrating >3
----------
$gt
db.movie.find({rating:{$gt:3}},{name:1,_id:0,rating:1}).pretty()
$nin not in
display all movies where price is not =200 or not 300 or not 350
db.movie.find({price:{$nin:[200,300,350]}})
$mod
display all movies with even rating
db.movie.find({rating:{$mod:[2,0]}})
sorting
-------
db.movie.find({},{name:1,rating:1,_id:0}).sort({rating:1}).pretty()
ascending order
db.movie.find({},{name:1,rating:1,_id:0}).sort({rating:-
1}).pretty()
descending order
limt
-----
db.movie.find({},{name:1,_id:0}).limit(5)
skip()
-----
db.movie.find({},{name:1,rating:1,_id:0}).skip(2).limit(3)
db.movie.find({actor:'Amitabh'}).pretty()
db.movie.find({'actor.1':'Amitabh'}).pretty()
db.movie.find({$or:[{rating:3},{price:{$gt:400}}]})
regular expressions
-------------------
^ begin
\d digit
$ end
.* multiple char
// pattern
movie ending with digit
------------------
db.movie.find({name:/[0-9]$/},{name:1,_id:0})
or
db.movie.find({name:/\d$/},{name:1,_id:0})
------------------------------------------------------
updating data
update({query},{action},{options})
updateOne({query},{action})
updateMany({query},{action})
operators
db.movie.update({name:'sholay'},{$set:{rating:5}},{multi:true})
db.movie.update({name:'sholay'},{$inc:{price:100}},{multi:true})
db.movie.update({name:'sholay'},{$set:{rating:9},$inc:{price:-50}},{multi:true})
price*1.10
db.movie.update({name:'sholay'},{$mul:{price:1.10}},{multi:true})
update rating to 6
$min
db.movie.update({},{$min:{rating:6}},{multi:true})
max()
db.movie.update({},{$max:{rating:5}},{multi:true})
--------------------------------------------------------
delete
db.dept.drop()
--------------------------
deleting database
>db.dropDatabase()
current database is deleted
deleting document
db.collection_name.remove({})
eg
db.emp.remove({deptno:10})
delete()
--------
will delete selected documents
db.movie.deleteOne({rating:5})
db.movie.deleteMany({rating:5})
db.movie.find({$or:[{rating:3},{price:{$gt:400}}]})