Found 4378 Articles for MySQL

Create a database in MySQL from Java?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

2K+ Views

The following is the code to create a database in MySQL from Java. We are creating the database with name, “Customer_Tracker_Database”import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class CreateDatabaseDemo {    public static void main(String[] args) {       Connection con=null;       Statement stmt=null;       String yourDatabaseName="Customer_Tracker_Database";       try {          con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?useSSL=false",          "root", "123456");          stmt = con.createStatement();          int status = stmt.executeUpdate("CREATE DATABASE "+yourDatabaseName);          if(status > 0) {             System.out.println("Database ... Read More

Get the returned record set order in MySQL IN clause?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

94 Views

For returned record set order, you need to use FIND_IN_SET() function. For an example, let us create a table.mysql> create table returnRecordSetOrderDemo    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.01 sec)Insert some records in the table using insert command.The query is as follows.mysql> insert into returnRecordSetOrderDemo values(100, 'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into returnRecordSetOrderDemo values(130, 'Carol'); Query OK, 1 row affected (0.13 sec) mysql> insert into returnRecordSetOrderDemo values(103, 'Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into returnRecordSetOrderDemo values(134, 'Sam'); Query OK, ... Read More

MySQL: Testing connection with query?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

756 Views

Use any predefined function with select query or you can print some words with the select query in order to test connection with query.The syntax is as follows.SELECT yourValue;The select query with predefined function is as follows.The syntax is as follows.SELECT anyPredefinedFunctionName();Now you can implement the above syntax in order to test connection with query.Case 1 -The query is as follows.mysql> select "This is MySQL" as Display;The following is the output.+---------------+ | Display | +---------------+ | This is MySQL | +---------------+ 1 row in set (0.00 sec)Case 2 -The query is as follows.mysql> select ... Read More

How to select from MySQL table A that does not exist in table B?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

1K+ Views

You can use IN operator to select from one table that does not exist in another. To understand the above syntax, let us create a table.The first table name is A and second table name is B. The query to create a table is as followsmysql> create table A    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.56 sec)Now you can insert some records in the table using insert command.The query is as followsmysql> insert into A values(10); Query OK, 1 row affected (0.23 sec) mysql> insert into A values(20); Query OK, 1 ... Read More

Updating a MySQL column that contains dot (.) in its name?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

869 Views

If the MySQL column contains dot (.) in its name, then you need to use backticks around the column name. To understand the above concept, let us create a table. The query to create a table is as followsmysql> create table UpdateDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> `User.FirstName.LastName` varchar(60)    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into UpdateDemo(`User.FirstName.LastName`) values('John Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('Adam Smith'); Query OK, ... Read More

How to get file extension of file as a result of MySQL query?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

3K+ Views

In order to get file extension of file as a result of SQL query, you can 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 getFileExtensionDemo    -> (    -> File_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> File_Name text    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into getFileExtensionDemo(File_Name) values('John.AllMySQLConcept.doc'); Query OK, 1 row affected (0.17 sec) mysql> ... Read More

How to set all values in a single column MySQL Query?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

10K+ Views

To set all values in a single column MySQL query, you can use UPDATE command.The syntax is as follows.update yourTableName set yourColumnName =yourValue;To understand the above syntax, let us create a table. The query to create a table is as follows.mysql> create table setAllValuesDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Amount int    -> ); Query OK, 0 rows affected (0.64 sec)Now you can insert some records in the table using insert command.The query is as follows.mysql> insert into setAllValuesDemo(Name, Amount) values('John', 2345); Query OK, 1 row affected ... Read More

How to find strings with a given prefix in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

3K+ Views

You can use LIKE operator to find strings with a given prefix.The syntax is as followsselect *from yourTableName where yourColumnName LIKE 'yourPrefixValue%';To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table findStringWithGivenPrefixDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserMessage text    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into findStringWithGivenPrefixDemo(UserMessage) values('Hi Good Morning !!!'); Query OK, 1 row affected (0.17 sec) mysql> insert into findStringWithGivenPrefixDemo(UserMessage) values('Hey ... Read More

Convert MySQL null to 0?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

17K+ Views

Use IFNULL or COALESCE() function in order to convert MySQL NULL to 0.The syntax is as followsSELECT IFNULL(yourColumnName, 0) AS anyAliasName FROM yourTableName; The second syntax is as follows: SELECT COALESCE(yourColumnName, 0) AS anyAliasName FROM yourTableName;Let us first create a table. The query to create a table is as followsmysql> create table convertNullToZeroDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Salary int    -> ); Query OK, 0 rows affected (1.28 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into convertNullToZeroDemo(Name, Salary) values('John', ... Read More

Get the second last row of a table in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

9K+ Views

You need to use ORDER BY clause to get the second last row of a table in MySQL.The syntax is as follows.select *from yourTableName order by yourColumnName DESC LIMIT 1, 1;To understand the above syntax, let us create a table. The query to create a table is as follows.mysql> create table secondLastDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(10)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command.The query is as follows.mysql> insert into secondLastDemo(StudentName) values('Larry'); Query OK, 1 row affected (0.15 ... Read More

Advertisements