Found 6705 Articles for Database

How do I lag columns in MySQL?

Samual Sam
Updated on 26-Jun-2020 09:55:30

227 Views

To lag a column in MySQL, first, let us create a table. The query to create a table is as follows −mysql> create table LagDemo    -> (    -> UserId int,    -> UserValue int    -> ); Query OK, 0 rows affected (1.74 sec)ExampleInsert some records in the table using insert command. The query is as follows −mysql> insert into LagDemo values(12, 158); Query OK, 1 row affected (0.61 sec) mysql> insert into LagDemo values(18, 756); Query OK, 1 row affected (0.21 sec) mysql> insert into LagDemo values(15, 346); Query OK, 1 row affected (0.25 sec) mysql> insert ... Read More

How can I set a MySQL database to use MyISAM by default?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

623 Views

To set the default storage engine, use the following syntax −set @@default_storage_engine = ’yourEngineType’;Now implement the above syntax to set the default engine to MyISAM. The query is as follows −mysql> set @@default_storage_engine = 'MyISAM'; Query OK, 0 rows affected (0.05 sec)Now you can check the default engine type with the help of SELECT statement. The query is as follows −mysql> select @@default_storage_engine;The following is the output displaying the engine as MyISAM −+--------------------------+ | @@default_storage_engine | +--------------------------+ | MyISAM | +--------------------------+ 1 ... Read More

How to work with one database connection object in the entire Java-MySQL application?

Samual Sam
Updated on 30-Jul-2019 22:30:25

808 Views

Use the singleton design pattern. Here is the Java code that returns a single object −ConnectDatabase.javaimport java.sql.Connection; import java.sql.DriverManager; public class ConnectDatabase {    static Connection conn = null;    public static Connection getConnection() {       if (conn != null) return conn;       String database = "test";       String Username = "root";       String password = "123456";       return getConnection(database, Username, password);    }    private static Connection getConnection(String databaseName, String UserName, String password) {       try {          Class.forName("com.mysql.jdbc.Driver");          conn ... Read More

How to write a MySQL “LIMIT” in SQL Server?

Samual Sam
Updated on 30-Jul-2019 22:30:25

346 Views

You need to use TOP(1) in SQL Server. The syntax is as follows −SELECT TOP(1) *FROM yourTableName WHERE yourCondition;To understand the above syntax, let us create a table. The query to create a table is as follows −create table TopDemoInSQLServer (    Id int,    Name varchar(10) );The snapshot of creation of table is as follows −Insert some records in the table using insert command. The query is as follows −insert into TopDemoInSQLServer values(10, 'John'); insert into TopDemoInSQLServer values(14, 'Carol'); insert into TopDemoInSQLServer values(1, 'Sam'); insert into TopDemoInSQLServer values(11, 'Bob'); insert into TopDemoInSQLServer values(18, 'David'); insert into TopDemoInSQLServer values(20, 'Sam');The ... Read More

Extract tuples with specified common values in another column in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

454 Views

To extract tuples with specified common values, use the following syntax −SELECT DISTINCT aliasName.yourColumnName1, aliasName.yourColumnName2, aliasName1.yourColumnName 1, aliasName1.yourColumnName2 FROM yourTableName aliasName INNER JOIN yourTableName aliasName1 ON aliasName.yourColumnName1 = aliasName1.yourColumnName1 WHERE aliasName.yourColumnName2 = 'value1' AND aliasName1.yourColumnName2 = 'value2';To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table extractTuples    -> (    -> Id int,    -> Name varchar(20),    -> Comments text    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into ... Read More

Create an aggregate checksum of a column in MySQL

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

611 Views

You can use CRC32 checksum for this. The syntax is as follows −SELECT SUM(CRC32(yourColumnName)) AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table CRC32Demo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserId varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CRC32Demo(UserId) values('USER-1'); Query OK, 1 row affected (0.38 sec) mysql> insert into CRC32Demo(UserId) values('USER-123'); Query OK, 1 row ... Read More

Select and filter the records on month basis in a MySQL table?

Samual Sam
Updated on 06-Mar-2020 07:03:12

670 Views

You can use aggregate function SUM() with GROUP BY clause to achieve this.Let us create a table. The query to create a table is as follows −mysql> create table SelectPerMonthDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Price int,    -> PurchaseDate datetime    -> ); Query OK, 0 rows affected (2.34 sec)ExampleInsert some records in the table using insert command with one of them would be the date of purchase. The query is as follows −mysql> insert into SelectPerMonthDemo(Price, PurchaseDate) values(600, date_add(now(), interval -1 month)); Query OK, 1 row affected (0.42 sec) ... Read More

Display the warning message when a FLOAT value is inserted into DECIMAL in MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

165 Views

You can create a temporary table with data type DECIMAL to get a warning when a float value is inserted into an int column. Display the same warning using SHOW WARNINGS.Let us create a table to understand. The query is as follows to create a table.mysql> create temporary table WarningDemo    -> (    -> Value DECIMAL    -> ); Query OK, 0 rows affected (0.13 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into WarningDemo values(9.80); Query OK, 1 row affected, 1 warning (0.03 sec)Here we are getting a warning. Let ... Read More

Select query using MySQL IN() and avoid sorting in it

Samual Sam
Updated on 30-Jul-2019 22:30:25

297 Views

Using IN() sorts the result for the specific field. To avoid this, use ORDER BY and FIND_IN_SET() for the field.To understand the find_in_set(), let us create a table. The query to create a table is as follows −mysql> create table ProductStock    -> (    -> ProductId int,    -> ProductName varchar(20),    -> ProductQuantity int,    -> ProductPrice float    -> ); Query OK, 0 rows affected (0.79 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into ProductStock values(1, 'Product-101', 10, 500.56); Query OK, 1 row affected (0.20 ... Read More

MySQL “order by” inside of “group by”? Is it possible?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

811 Views

Instead of using ORDER BY inside GROUP BY, you can use the MAX() aggregate function.The syntax is as follows −SELECT yourNameColumnName, MAX(yourRankColumnName) FROM yourTableName GROUP BY yourNameColumnName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table MaxDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20),    -> UserRank int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into MaxDemo(UserName, UserRank) values('Larry', 2); Query ... Read More

Advertisements