Found 6702 Articles for Database

MySQL query to select one specific row and another random row?

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

142 Views

To select one specific row and another random row, you can use ORDER BY and RAND(). Let us first create a sample table:mysql> create table oneSpecificRowAndOtherRandom    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.72 sec)Following is the query to insert some records in the table using insert command:mysql> insert into oneSpecificRowAndOtherRandom(Name) values('Larry'); Query OK, 1 row affected (0.56 sec) mysql> insert into oneSpecificRowAndOtherRandom(Name) values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into oneSpecificRowAndOtherRandom(Name) values('Mike'); Query OK, 1 row affected ... Read More

In a MySQL schema, what is the meaning of “AUTO_INCREMENT=3”

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

227 Views

In MySQL, AUTO_INCREMENT=3 tells that the inserted record will start from 3 not the default 1. Let us first create a sample table and set auto increment to 3:mysql> create table Auto_incrementDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> )AUTO_INCREMENT=3; Query OK, 0 rows affected (0.52 sec)Following is the query to insert some records in the table using insert command:mysql> INSERT INTO Auto_incrementDemo(Name) values('John'); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO Auto_incrementDemo(Name) values('Larry'); Query OK, 1 row affected (0.15 sec) mysql> INSERT INTO Auto_incrementDemo(Name) ... Read More

Count value for multiple columns in MySQL?

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

508 Views

To count value for multiple columns, use the CASE statement. Let us first create a table::mysql> create table countValueMultipleColumnsDemo    -> (    -> Value1 int,    -> Value2 int,    -> Value3 int    -> ); Query OK, 0 rows affected (0.62 sec)Following is the query to insert some records in the table using insert command:mysql> insert into countValueMultipleColumnsDemo values(10, 15, 10); Query OK, 1 row affected (0.15 sec) mysql> insert into countValueMultipleColumnsDemo values(20, 30, 10); Query OK, 1 row affected (0.14 sec) mysql> insert into countValueMultipleColumnsDemo values(40, 10, 60); Query OK, 1 row affected (0.18 sec)Following ... Read More

How to change a primary key in MySQL to auto_increment?

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

4K+ Views

To change a primary key to auto_increment, you can use MODIFY command. Let us first create a table.mysql> create table changePrimaryKeyInAutoIncrement    -> (    -> StudentId int not null primary key,    -> StudentName varchar(100),    -> StudentAge int,    -> StudentAddress varchar(100)    -> ); Query OK, 0 rows affected (0.63 sec)Let us now check the description of table using desc command:mysql> desc changePrimaryKeyInAutoIncrement;This will produce the following output+----------------+--------------+------+-----+---------+-------+ | Field          | Type         | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | StudentId      | int(11)   ... Read More

Total number of fields in all tables in database?

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

545 Views

To get total number of fields in all tables in database, you can use information_schema.columns along with aggregate function count(*).We are using ‘sample’ database which consists of a lot of tables with fields. Following is the query to get total number of fields in all tables in database:mysql> SELECT COUNT(*) AS TOTAL_NUMBER_OF_FIELDS    -> FROM INFORMATION_SCHEMA.COLUMNS    -> WHERE TABLE_SCHEMA = 'sample';This will produce the following output+------------------------+ | TOTAL_NUMBER_OF_FIELDS | +------------------------+ | 796                    | +------------------------+ 1 row in set (0.04 sec)Now, let us check another database ‘test’. Following is the query ... Read More

How to convert bool to int in MySQL?

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

1K+ Views

To convert bool to int in MySQL, you can use CAST(). Let us first create a table:mysql> create table convertBoolToIntDemo -> ( -> isYoung bool -> ); Query OK, 0 rows affected (0.69 sec)Following is the query to insert some records in the table using insert command:mysql> insert into convertBoolToIntDemo values(true); Query OK, 1 row affected (0.18 sec) mysql> insert into convertBoolToIntDemo values(false); Query OK, 1 row affected (0.09 sec) mysql> insert into convertBoolToIntDemo values(true); Query OK, 1 row affected (0.15 sec) mysql> insert into convertBoolToIntDemo values(false); Query ... Read More

Sort by character length in MySQL

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

3K+ Views

To sort by character length in MySQL use the ORDER BY LENGTH(). Let us first create a table:mysql> create table orderingAADemo    -> (    -> Value varchar(100)    -> ); Query OK, 0 rows affected (1.30 sec)Following is the query to insert some records in the table using insert command:mysql> insert into orderingAADemo values('A'); Query OK, 1 row affected (0.12 sec) mysql> insert into orderingAADemo values('B'); Query OK, 1 row affected (0.13 sec) mysql> insert into orderingAADemo values('AA'); Query OK, 1 row affected (0.20 sec) mysql> insert into orderingAADemo values('C'); Query OK, 1 row affected (0.12 ... Read More

MySQL query to get the count of rows in which two or more specified values appear?

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

58 Views

To get the count of rows in which two or more specified values appear, let us first create a sample table:mysql> create table specifiedValuesDemo -> ( -> Value int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.60 sec)Following is the query to insert some records in the table using insert command:mysql> insert into specifiedValuesDemo values(10, 15, 20); Query OK, 1 row affected (0.17 sec) mysql> insert into specifiedValuesDemo values(40, 10, 20); Query OK, 1 row affected (0.16 sec) ... Read More

Write a MySQL query equivalent to “SHOW TABLES” in sorted order?

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

81 Views

Use INFORMATION_SCHEMA.TABLES to display tables in sorted order. The below syntax will give sorted list of tables in ascending order:select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA= 'yourDatabaseName' order by TABLE_NAME;Following is the query to implement the equivalent to SHOW TABLES:mysql> select TABLE_NAME from INFORMATION_SCHEMA.TABLES    -> where TABLE_SCHEMA= 'sample' order by TABLE_NAME;This will produce the following output+------------------------------------+ | TABLE_NAME                         | +------------------------------------+ | a                                  | | accumulateddemo               ... Read More

How to suppress MySQL stored procedure output?

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

846 Views

To suppress MySQL stored procedure output, you can use variable. Let us first create a table.mysql> create table person_information    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.50 sec)Following is the query to insert some records in the table using insert command:mysql> insert into person_information values(100, 'John'); Query OK, 1 row affected (0.17 sec) mysql> insert into person_information values(101, 'Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into person_information values(102, 'Robert'); Query OK, 1 row affected (0.16 sec)Following is the query to display records from ... Read More

Advertisements