Found 6702 Articles for Database

How to list databases vertically in MySQL command line?

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

158 Views

You can use backward slash followed by G i.e. \G instead of semicolon(;). The syntax is as follows to display database names vertically in MySQL command lineSHOW DATABASES \GTo display all database names vertically, you need to use \G. The query is as followsmysql> show databases\GThe following is the output*************************** 1. row *************************** Database: business *************************** 2. row *************************** Database: database1 *************************** 3. row *************************** Database: databasesample *************************** 4. row *************************** Database: education *************************** 5. row *************************** Database: hello *************************** 6. row *************************** Database: information_schema *************************** 7. row *************************** Database: javadatabase2 *************************** 8. row *************************** Database: javasampledatabase *************************** 9. row ... Read More

How to display records vertically in MySQL command line?

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

2K+ Views

You can use backward slash followed by G i.e. \G instead of semicolon(;). The syntax is as follows to show records vertically in MySQL command line.SELECT *FROM yourTableName\GTo understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table showRecordsVertically -> ( -> Id int, -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (2.10 sec)Insert some records in the table using insert command. The query is as followsmysql> insert into showRecordsVertically ... Read More

Sort by order of values in a MySQL select statement IN clause?

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

105 Views

You can use field() function with ORDER BY clause to sort by order of values. The syntax is as followsSELECT *FROM yourTableName WHERE yourColumnName IN(Value1, Value2, Value3, .......N); ORDER BY FIELD(yourColumnName ,Value1, Value2, Value3, .......N);To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table SelectInDemo    -> (    -> StudentId int,    -> StudentName varchar(100),    -> StudentAge int    -> ); Query OK, 0 rows affected (1.04 sec)Insert records in the table using insert command. The query is as followsmysql> insert into SelectInDemo values(1, 'Mike', 23); Query ... Read More

Generate table DDL via a query on MySQL and SQL Server?

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

12K+ Views

The DDL stands for Data Definition Language. To generate the table DDL via query, you can use show create command.The syntax is as followsSHOW CREATE TABLE yourTableName;The above syntax is MySQL specific. Suppose, we have a table with the name ‘DDLOfTableStudent’.First, create a table with the name ‘DDLOfTableStudent’. The query to create a table is as followsmysql> create table DDLOfTableStudent -> ( -> StudentId int, -> StudentFirstName varchar(100), -> StudentLastName varchar(100), -> StudentAddress varchar(200), -> StudentAge int, -> StudentMarks ... Read More

How to know if MySQL binary log is enabled through SQL command?

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

4K+ Views

to know if MySQL binary log is enabled through SQL command, you can use show variables command.The syntax is as followsshow variables like ‘yourPatternValue’;In place of ‘yourPatternValue’, you can use log_bin to check the binary log is enabled using SQL command show.The query is as followsmysql> show variables like 'log_bin';The following is the output that displays whether it is enabled or not+---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.03 sec)

MySQL stored-procedure: out parameter?

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

754 Views

Here is a stored procedure that takes one parameter for input (IN) and second parameter for output (OUT)mysql> delimiter // mysql> create procedure Sp_SQRT(IN Number1 INT, OUT Number2 FLOAT) -> Begin -> set Number2=sqrt(Number1); -> end; -> // Query OK, 0 rows affected (0.24 sec) mysql> delimiter ;Call the stored procedure and send the value to the user variable. The syntax is as followsCALL yourStoredProcedureName(anyIntegerValue, @anyVariableName);Check what value is stored in the variable @anyVariableName. The syntax is as followsSELECT @anyVariableName;Created the stored procedure with the name ‘Sp_SQRT’. The ... Read More

What is the difference between int and integer in MySQL?

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

2K+ Views

The int is the synonym of integer in MySQL 5.0. Here is the demo display both int and integer internally represents int(11).Creating a table with int datatypemysql> create table IntDemo    -> (    -> Id int    -> ); Query OK, 0 rows affected (1.04 sec)Here is description of the table. The query is as followsmysql> desc IntDemo;The following is the output+-------+---------+------+-----+---------+-------+ | Field | Type    | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | Id    | int(11) | YES  |     | NULL    |       | +-------+---------+------+-----+---------+-------+ 1 row in ... Read More

Split the left part of a string by a separator string in MySQL?

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

404 Views

You can use substring_index() function from MySQL to split the left part of a string. The syntax is as follows −SELECT yourColumnName1, .....N, SUBSTRING_INDEX(yourColumnName, ’yourSeperatorSymbol’, 1) as anyVariableName from yourTableName;The value 1 indicates that you can get left part of string. To check the above syntax, let us create a table. The query to create a table is as follows −mysql> create table LeftStringDemo -> ( -> Id int, -> Words varchar(100) -> ); Query OK, 0 rows affected (0.92 sec)Insert some records in the table using insert ... Read More

How to order DESC by a field, but list the NULL values first?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

61 Views

To order by a field and list the NULL values first, you need to use the following syntax. This will order in descending order −select yourColumnName from yourTableName group by yourColumnName is null desc, yourColumnName desc;To understand the above syntax, let us first create a table −mysql> create table OrderByNullFirstDemo    −> (    −> StudentId int    −> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table with the help of insert command. The query is as follows −mysql> insert into OrderByNullFirstDemo values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into OrderByNullFirstDemo ... Read More

Get timestamp date range with MySQL Select?

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

933 Views

To select timestamp data range, use the below syntax −SELECT *FROM yourTableName where yourDataTimeField >= anyDateRange and yourDataTimeField < anyDateRangeTo understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table DateRange −> ( −> DueTime timestamp −> ); Query OK, 0 rows affected (1.34 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into DateRange values('2016-11-13'); Query OK, 1 row affected (0.51 sec) mysql> insert into DateRange values('2016-10-14'); Query OK, 1 row ... Read More

Advertisements