AmitDiwan has Published 10744 Articles

Will MySQL work if we won’t include the size of VARCHAR while creating a new table?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:29:40

114 Views

No, the query won’t work. Let’s create the same scenario and check the error −mysql> create table DemoTable1821      (      Id int,      FirstName varchar,      LastName varchar      ); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual ... Read More

MySQL query to subtract date records with week day and display the weekday with records

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:28:24

227 Views

For this, you can use DATE_FORMAT(). Let us first create a table −mysql> create table DemoTable1820      (      AdmissionDate varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1820 values('20/10/2019'); Query OK, 1 row ... Read More

Count the same value of each row in a MySQL column?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:25:07

371 Views

To count the same value of each row, use COUNT(*) along with GROUP BY clause. Let us first create a table −mysql> create table DemoTable1818      (      Id int,      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the ... Read More

Passing Multiple ids to single parameter in MySQL?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:23:54

1K+ Views

To pass multiple ids to single parameter, use FIND_IN_SET(). Let us first create a table −mysql> create table  DemoTable1817      (      EmployeeName varchar(20),      CountryName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert ... Read More

MySQL randomly select 2 values from column values?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:21:23

380 Views

To randomly select, use ORDER BY RAND(). To select only 2 values, use LIMIT 2 in MySQL. Let us first create a table −mysql> create table DemoTable1815      (      Question text      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using ... Read More

Implement and set DOUBLE length in MySQL

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:20:19

498 Views

To implement DOUBLE in MySQL, the syntax is as follows −create table yourTableName      (      yourColumnName double(5, 2) unsigned );Let us first create a table −mysql> create table DemoTable1814      (      Amount double(5, 2) unsigned      ); Query OK, 0 rows affected (0.00 ... Read More

How to GRANT SELECT ON all tables in all databases on a server with MySQL?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:19:03

1K+ Views

For this, you can use GRANT SELECT statement as in the below syntax −GRANT SELECT ON *.* TO 'yourUserName'@'yourHostName';First list all the user names along with host −mysql> select user, host from mysql.user;This will produce the following output −+------------------+-----------+ | user             |     ... Read More

How to select and display a list of values in one column that are available in two different MySQL columns?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:15:52

2K+ Views

For this, use UNION ALL. Let us first create a table −mysql> create table DemoTable1813      (      Name1 varchar(20),      Name2 varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1813 values('John', 'Chris'); ... Read More

How to extract column name and type from MySQL?

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:14:15

479 Views

To extract column name and type, use INFORMATION_SCHEMA.COLUMNS −select concat(column_name, '=', data_type) as anyAliasName from information_schema.columns where table_schema= yourDatabaseName and table_name= yourTableName;Let us first create a table −mysql> create table DemoTable1812      (      Id int,      FirstName varchar(20),      Age int,      isMarried boolean, ... Read More

Adding unique constraint to ALTER TABLE in MySQL

AmitDiwan

AmitDiwan

Updated on 24-Dec-2019 06:13:14

401 Views

Let us first create a table −mysql> create table DemoTable1811      (      FirstName varchar(20),      LastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Here is the query to add indexmysql> alter table DemoTable1811 ADD UNIQUE unique_index_first_last_name(FirstName, LastName); Query OK, 0 rows affected (0.00 ... Read More

Advertisements