- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6702 Articles for Database
10K+ Views
Update date of datetime field with the help of arithmetic operator minus(-).The syntax is as followsupdate yourTableName set yourDateTimeColumnName=yourDateTimeColumnName - interval yourValue day where date(yourDateTimeColumnName)=’yourDateValue’;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table updateDateDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ArrivalDate datetime -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into updateDateDemo(ArrivalDate) values('2011-01-13'); Query OK, 1 row affected (0.19 sec) mysql> insert into updateDateDemo(ArrivalDate) values('2013-04-21'); ... Read More
289 Views
To return query based on the date in MongoDB, let us take an example.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.returnQueryFromDate.insertOne({"PassengerName":"John", "PassengerAge":23, "PassengerArrivalTime":new ISODate("2018-03-10 14:45:56")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57be9064dcd4a68b70e4") } > db.returnQueryFromDate.insertOne({"PassengerName":"Larry", "PassengerAge":21, "PassengerArrivalTime":new ISODate("2018-05-19 11:10:23")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e5") } > db.returnQueryFromDate.insertOne({"PassengerName":"Mike", "PassengerAge":24, "PassengerArrivalTime":new ISODate("2018-08-25 16:40:12")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e6") } >db.returnQueryFromDate.insertOne({"PassengerName":"Carol", "PassengerAge":26, "PassengerArrivalTime":new ISODate("2019-01-29 09:45:10")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e7") }Display all documents from a ... Read More
11K+ Views
To achieve this for multiple tables, use the UNION ALL.The syntax is as followsselect sum(variableName.aliasName) from ( select count(*) as yourAliasName from yourTableName1 UNION ALL select count(*) as yourAliasName from yourTableName2 ) yourVariableName;Let us implement the above syntax. Here, I am using the sample database which has more tables.The two tables we are using areuserdemowheredemoHere is the query to display all records of both the tables. The query is as follows to display records from table ‘userdemo’.mysql> select *from userdemo;The following is the output+--------+----------+------------------+ | UserId | UserName | RegisteredCourse | +--------+----------+------------------+ | 1 ... Read More
430 Views
You can delete everything in a MongoDB database using dropDatabase() function. The syntax is as follows −use yourDatabaseName; db.dropDatabase();The above syntax will delete everything in a MongoDB database.To delete everything in a MongoDB database, let us first display all the databases from MongoDB. The query is as follows −> show dbsThe following is the output −use yourDatabaseName; admin 0.000GB config 0.000GB flighInformation 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GBNow we will delete everything from the database ‘flightInformation’.First, you need to switch the database to ‘flightInformation’. The query is as follows −> use flighInformation; switched to db flighInformationNow here ... Read More
151 Views
You can call pretty() function on cursor object to prettyprint in MongoDB shell. The syntax is as follows −db.yourCollectionName.find().pretty();To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −>db.prettyDemo.insertOne({"ClientName":"Larry", "ClientAge":27, "ClientFavoriteCountry":["US", "UK"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2") } >db.prettyDemo.insertOne({"ClientName":"Mike", "ClientAge":57, "ClientFavoriteCountry":["AUS", "UK"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3") }Display all documents from a collection with the help of find() method. The query is as follows −> db.prettyDemo.find();The following is the output −{ "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"), "ClientName" : "Larry", "ClientAge" ... Read More
7K+ Views
To list all collections in Mongo shell, you can use the function getCollectionNames().The syntax is as follows −db.getCollectionNames();You can use another command which is collections. The syntax is as follows −show collections;To list all collections in Mongo, use the above two functions. The query is as follows −> db.getCollectionNames();The following is the output −[ "ConvertStringToDateDemo", "IdUpdateDemo", "ProductsInformation", "addFieldDemo", "addNewFieldToEveryDocument", "arrayInnerElementsDemo", "arrayLengthGreaterThanOne", "arrayOfArraysDemo", "caseInsensitiveDemo", "changeDataType", "changeType", "charactersAllowedDemo", "charactersDemo", "checkFieldContainsStringDemo", "checkSequenceDemo", "combinationOfArrayDemo", "conditionalSumDemo", "convertStringToNumberDemo", "copyThisCollectionToSampleDatabaseDemo", "countDemo", "createSequenceDemo", "distinctCountValuesDemo", "distinctRecordDemo", ... Read More
1K+ Views
In order to delete everything after a space, you need to use SUBSTRING_INDEX().The syntax is as followsselect substring_index(yourColumnName, ' ', 1) as anyAliasName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table deleteAfterSpaceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into deleteAfterSpaceDemo(StudentName) values('John Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into deleteAfterSpaceDemo(StudentName) ... Read More
240 Views
To drop a MongoDB database from the command line, use the following syntax:mongo yourDatabaseName --eval "db.dropDatabase()"To understand the above syntax, let us display all the database from MongoDB. The query is as follows −> show dbs;The following is the output −StudentTracker 0.000GB admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB test 0.003GBDrop the database with the name ‘StudentTracker’. The query is as follows to drop a database from command line −C:\Program Files\MongoDB\Server\4.0\bin>mongo StudentTracker --eval "db.dropDatabase()"The following is the output −MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/StudentTracker?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("afc34e93-b4c0-46f0-85bd-b80ed17b8c11") } MongoDB server version: 4.0.5 { "dropped" ... Read More
141 Views
You need to use UPDATE statement for this.The syntax is as followsupdate yourTableName set yourColumnName1=yourValue1, yourColumnName2=yourValue2, ....N where yourCondition;Let us create a table for our examplemysql> create table addWhereClauseDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(30), -> StudentPassword varchar(40) -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into addWhereClauseDemo(StudentName, StudentPassword) values('John', 'John123456'); Query OK, 1 row affected (0.14 sec) mysql> insert into addWhereClauseDemo(StudentName, StudentPassword) values('Carol', '99999'); Query OK, 1 row affected (0.24 sec) mysql> insert ... Read More
2K+ Views
You can compare only day and month with date field in MySQL with the help of DATE_FORMAT().The syntax is as followsselect *from yourTableName WHERE DATE_FORMAT(yourColumnName, '%m-%d') = DATE_FORMAT('yourValue', '%m-%d') and yourCondition;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table compareDayAndMonthDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> compareDayAndTime date -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into compareDayAndMonthDemo(compareDayAndTime) values('2014-01-31'); Query OK, 1 row affected (0.20 ... Read More