Found 4378 Articles for MySQL

How can we grant a user to access all stored procedures in MySQL?

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

1K+ Views

Let us first display all users and host from the table MySQL.user −mysql> select user, host from Mysql.user;This will produce the following output −+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | %         | | Charlie          | %         | | Robert           | %         | | User2 | % ... Read More

How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?

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

517 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(FirstName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1(FirstName) values('Chris'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John ... Read More

Can we skip a column name while inserting values in MySQL?

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

635 Views

Yes, we can do that. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(100),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentAge) values(23); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(StudentName) values('John'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output. NULL will get inserted for the skipped column values −+-------------+------------+ | StudentName | StudentAge ... Read More

How to find a value between range in MySQL?

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

573 Views

For this, use BETWEEN operator in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Start int,    -> End int    -> ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Start, End) values(100, 200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Start, End) values(400, 500); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Start, End) values(210, 350); Query OK, 1 row affected (0.11 sec)Display all ... Read More

Add a new column and index to an existing table with ALTER in a single MySQL query?

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

348 Views

To add a new column to an existing table, use ADD. With that, to add a new index, use the ADD INDEX(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(100),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.69 sec)Let us check the description of the table −mysql> desc DemoTable;This will produce the following output −+-------+--------------+------+-----+---------+----------------+ | Field | Type         | Null | Key | Default | Extra          | +-------+--------------+------+-----+---------+----------------+ | Id ... Read More

Insertion in a MySQL table with only a single column set as auto_increment?

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

87 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command. Here, we have inserted a value, but since it is AUTO_INCREMENT, therefore, the default value would be visible −mysql> insert into DemoTable(StudentId) values(0); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output. The default AUTO_INCREMENT value 1 as the first value is visible ... Read More

Execute operations (plus, minus, multiply, divide) while updating a MySQL table?

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

300 Views

Following is the syntax executing the plus (+) operator −update yourTableName set yourColumnName3=(yourColumnName1+yourColumnName2)The above syntax is only for plus operator. You need to change symbol like -, *, / for other operations. Let us first create a table −mysql> create table DemoTable    -> (    -> Number1 int,    -> Number2 int,    -> AddResult int,    -> MinusResult int,    -> MultiplyResult int,    -> DivideResult int    -> ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number1, Number2) values(40, 20); Query OK, 1 row affected (0.16 ... Read More

MySQL query to get the current date from the list of dates

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

95 Views

For the current day, you can use CURDATE() method. Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate date    -> ); Query OK, 0 rows affected (1.35 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-06-14' ); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('2019-06-15'); Query OK, 1 row affected (0.64 sec) mysql> insert into DemoTable values('2019-06-16'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2018-01-16'); Query OK, 1 row affected (0.29 sec) mysql> insert into ... Read More

Counting the number of non-null or nonzero columns in a MySQL table?

Naveen Singh
Updated on 30-Jun-2020 09:45:00

501 Views

Use if() method for this. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Number1 int,    -> Number2 int    -> ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number1, Number2) values(10, 20); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable(Number1, Number2) values(0, 32); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable(Number1, Number2) values(40, 0); Query OK, 1 row affected (0.21 sec) mysql> insert ... Read More

Set a certain value first with MySQL ORDER BY?

Naveen Singh
Updated on 30-Jun-2020 09:47:00

1K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Number int    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(14); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.14 sec) ... Read More

Advertisements