Found 6705 Articles for Database

Use MySQL concat() and lower() effectively

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

1K+ Views

The contact() method is used to concatenate. However, lower() is used to change the case to lowercase. For our example, let us create a table.The query to create a table is as followsmysql> create table concatAndLowerDemo    -> (    -> FirstValue varchar(10),    -> SecondValue varchar(10),    -> ThirdValue varchar(10),    -> FourthValue varchar(10)    -> ); Query OK, 0 rows affected (0.55 sec)Now you can insert some records in the table using insert command.The query is as followsmysql> insert into concatAndLowerDemo values('John', '12345', 'Java', 'MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into concatAndLowerDemo values('Hi', '12345', ... Read More

How to avoid null result of “SELECT max(rank) FROM test” for an empty table?

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

3K+ Views

You can use COALESCE() along with aggregate function MAX() for this.The syntax is as followsSELECT COALESCE(MAX(`yourColumnName`), 0) FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table avoidNullDemo    -> (    -> `rank` int    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into avoidNullDemo values(10); Query OK, 1 row affected (0.20 sec) mysql> insert into avoidNullDemo values(NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into avoidNullDemo values(20); Query OK, 1 ... Read More

What is the return type of a “count” query against MySQL using Java JDBC?

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

480 Views

The return type of count is long. The Java statement is as followsrs.next(); long result= rs.getLong("anyAliasName");First, create a table with some records in our sample database test3. The query to create a table is as followsmysql> create table CountDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CountDemo(Name) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into CountDemo(Name) values('Carol'); Query OK, 1 row affected (0.16 sec) ... Read More

How to Drop MySQL Table using Java?

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

274 Views

Let us first create a table in a database. The query to create a table is as followsmysql> create table customerDetails    -> (    -> CustomerId int,    -> CustomerName varchar(30)    -> ); Query OK, 0 rows affected (0.56 sec)Now display all tables from the database in order to check the customerDetails table is present or not.The query is as followsmysql> show tables;The following is the output+------------------------------+ | Tables_in_test3              | +------------------------------+ | bestdateformatdemo           | | customerdetails              | | deletedemo     ... Read More

Resolve error 1045 (28000) access denied for user 'root'@'localhost' (using password: YES)?

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

5K+ Views

To fix this error, you need to specify the -p option for password.The syntax is as followsmysql -uyourUserName -pLet us implement it.First, we need to open CMD using Windows+R shortcut keys. The snapshot is as followsType CMD and press OK button. You will get a command prompt.The snapshot is as followsNow reach the MySQL bin directory.The snapshot is as followsNow use the syntax discussed in the beginning.The command is as follows

MySQL temporary variable assignment?

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

3K+ Views

You can use SET command for temporary variable assignment.The syntax is as followsSET @anyVariableName=(SELECT yourColumnName FROM yourTableName WHERE yourCondition);To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table tempVariableAssignment    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Age int    -> ); Query OK, 0 rows affected (0.59 sec)Now insert some records in the table using insert commandmysql> insert into tempVariableAssignment(Name, Age) values('John', 25); Query OK, 1 row affected (0.14 sec) mysql> insert into tempVariableAssignment(Name, Age) values('Carol', 26); Query ... Read More

How to update the current delimiter in MySQL?

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

336 Views

At first, let us determine the current delimiter in MySQL using the following syntax\sThe above syntax will let you know about the current delimiter. Let us implement the above syntax.The query is as followsmysql> \sThe following is the outputC:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe Ver 8.0.12 for Win64 on x86_64 (MySQL Community Server - GPL) Connection id: 19 Current database: sample Current user: root@localhost SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256 Using delimiter: ; Server version: 8.0.12 MySQL Community Server - GPL Protocol version: 10 Connection: localhost via TCP/IP Insert id: 11 Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: ... Read More

Priority of AND and OR operator in MySQL select query?

Chandu yadav
Updated on 06-Aug-2021 21:41:51

1K+ Views

The AND has the highest priority than the OR operator in MySQL select query.Let us check how MySQL gives the highest priority to AND operator.The query is as followsmysql> select 0 AND 0 OR 1 as Result;The following is the output+--------+ | Result | +--------+ | 1     | +--------+ 1 row in set (0.00 sec)If you are considering the OR operator has the highest priority then MySQL will wrap up the above query like this.The query is as followsselect 0 AND (0 OR 1) as ResultFirst, solve the 0 OR 1, this will give result 1. After that ... Read More

How to count number of distinct values per field/ key in MongoDB?

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

545 Views

You can use the distinct command for this. 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.distinctCountValuesDemo.insertOne({"StudentFirstName":"John", "StudentFavouriteSubject":["C", "C++", "Java", "MySQL", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a39f193b406bd3df60e07") } > db.distinctCountValuesDemo.insertOne({"StudentFirstName":"Larry", "StudentFavouriteSubject":["MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a3a1193b406bd3df60e08") }Display all documents from a collection with the help of find() method. The query is as follows −> db.distinctCountValuesDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8a39f193b406bd3df60e07"),    "StudentFirstName" : "John",    "StudentFavouriteSubject" : [ ... Read More

Find duplicate records in MongoDB?

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

2K+ Views

You can use the aggregate framework to find duplicate records in MongoDB. 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.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330293b406bd3df60e01") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330493b406bd3df60e02") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a330c93b406bd3df60e03") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331093b406bd3df60e04") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a331593b406bd3df60e05") } > db.findDuplicateRecordsDemo.insertOne({"StudentFirstName":"Mike"}); {   ... Read More

Advertisements