Found 6702 Articles for Database

Check if table exists in MySQL and display the warning if it exists?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

313 Views

To check if table exists, use the following syntax −CREATE TABLE IF NOT EXISTS yourTableName (    yourColumnName1 dataType,    .    .    .    .    N );Here, we will try to create a table that already exists and then it will produce a warning message “Table already exists”. Let us first create a table. This table already exists −mysql> CREATE TABLE IF NOT EXISTS DemoTable    (    Id int    ); Query OK, 0 rows affected, 1 warning (0.06 sec)The warning message is as follows −mysql> show warnings;Output+-------+------+-------------------------------------+ | Level | Code | Message ... Read More

MongoDB Query to search for records only in a specific hour?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

453 Views

For this, use the $hour operator. Let us first create a collection with documents with one of the field as date −> db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-01-31 09:45:50")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8a86d78f205348bc62a") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-02-21 01:10:01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8b86d78f205348bc62b") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-04-01 04:10:11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8e26d78f205348bc62c") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-05-11 08:53:01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8f26d78f205348bc62d") }Following is the query to display all documents from a collection with the help of find() method −> db.mongoDbSearchForHoursDemo.find().pretty();This will ... Read More

How to apply NOW() to timestamps field in MySQL Workbench?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

729 Views

Let us first create a table −create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ShippingDate timestamp );Insert some records in the table using insert command. Here, we have included the current date with NOW() −INSERT INTO DemoTable(ShippingDate) VALUES(now());Display all records from the table using select statement −SELECT *FROM DemoTable;OutputFollowing is the screenshot of query in MySQL workbench to set NOW() to timestamp field “ShippingDate”. The query also displays the output below −

MySQL automatic conversion on lowercase? Is this possible?

George John
Updated on 30-Jul-2019 22:30:26

291 Views

Yes, it is possible with triggers. You can create trigger for automatic conversion on lower case. Let us first create a table −mysql> create table DemoTable    (    StudentSubject text    ); Query OK, 0 rows affected (0.61 sec)Let us create a trigger for automatic conversion on lower case −mysql> CREATE TRIGGER lowerCaseOnInsertDemo BEFORE INSERT ON DemoTable FOR EACH ROW    SET NEW.StudentSubject = LOWER(NEW.StudentSubject); Query OK, 0 rows affected (0.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MOngoDb'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('JaVA'); Query OK, ... Read More

Update multiple rows in a single MongoDB query?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

320 Views

Use the concept of initializeUnorderedBulkOp(). Let us first create a collection with documents −>db.upDateMultipleRowsDemo.insertOne({"CustomerName":"John", "CustomerPurchaseAmount":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb06d78f205348bc626") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Chris", "CustomerPurchaseAmount":700}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb26d78f205348bc627") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"David", "CustomerPurchaseAmount":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb36d78f205348bc628") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Larry", "CustomerPurchaseAmount":1900}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb46d78f205348bc629") }Following is the query to display all documents from a collection with the help of find() method −> db.upDateMultipleRowsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6ceb06d78f205348bc626"),    "CustomerName" : "John",    "CustomerPurchaseAmount" : 500 } {   ... Read More

How do I search for names starting with A in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

113 Views

Use LIKE for this, as shown below −select *from yourTableName where yourColumnName LIKE 'A%';Let us first create a table −mysql> create table DemoTable    (    StudentName varchar(100)    ); Query OK, 0 rows affected (0.66 sec)Now you can insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Adam Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Aaron Taylor'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.27 sec)Display ... Read More

How to implement MongoDB $or operator

Smita Kapse
Updated on 30-Jul-2019 22:30:26

118 Views

Evaluate one or more expressions using the $or operator in MongoDB. Following is the syntax −db.yourCollectionName.find({ $or: [{ "yourFieldName": yourValue1 }, { "yourFieldName": yourValue2} ] } ).pretty();Let us first create a collection with documents −> db.orOperatorDemo.insertOne({"StudentNames":["John", "Carol", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6b80a6d78f205348bc61b") } > db.orOperatorDemo.insertOne({"StudentNames":["Robert", "Chris", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6b8266d78f205348bc61c") } > db.orOperatorDemo.insertOne({"StudentNames":["John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6b8346d78f205348bc61d") }Following is the query to display all documents from a collection with the help of find() method −> db.orOperatorDemo.find().pretty();This will produce the following output −{    "_id" ... Read More

How to display two decimals in MySQL/PHP?

Arjun Thakur
Updated on 31-Dec-2019 08:52:20

276 Views

To display two decimals, use number_format(). Let us first write the PHP code for the same. We have first declared two variables are initialized −$number1=10.3423; $number2=10;Now, display the two decimals using the number_format() function −$result1=number_format ($number1, 2); $result2=number_format ($number2, 2);ExampleFollowing is the example −OutputFollowing is the snapshot of PHP code −10.34 10.00

How do I split a numerical query result in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

103 Views

To split a numerical query result, you can use the CONCAT() function in MySQL. Let us first create a table −mysql> create table DemoTable    (    StudentId int    ); Query OK, 0 rows affected (0.68 sec)Now you can insert some records in the table using insert command −mysql> insert into DemoTable values(2222); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(5555); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(4567); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(8905); Query OK, 1 row affected (0.15 sec)Display all records from ... Read More

Add MD5 hash value to MongoDB collection?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

To add MD5 hash value, use hex_md5(). Let us first create a collection with documents −>db.addMd5HashValueDemo.insertOne({"UserName":"Adam", "UserPassword":"Adam123456"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6a4c66d78f205348bc619") } >db.addMd5HashValueDemo.insertOne({"UserName":"Chris", "UserPassword":"Chris_121#"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6a4e46d78f205348bc61a") }Following is the query to display all documents from a collection with the help of find() method −> db.addMd5HashValueDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#" }Following is the query to add md5 hash value to mongo collection −> db.addMd5HashValueDemo.find().forEach( function(documentPass){    documentPass.Value ... Read More

Advertisements