Found 6705 Articles for Database

Adding new column after a specific column and defining a default in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

You need to follow some steps to add a new column after a specific column and defining default value. In order to achieve this, you need to use ALTER command. Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentAge int,    StudentCountryName varchar(100)    ); Query OK, 0 rows affected (0.21 sec)Let us check the description of table −mysql> desc DemoTable;This will produce the following output −+--------------------+--------------+------+-----+---------+----------------+ | Field              | Type         | Null | ... Read More

How to use MySQL CASE statement while using UPDATE Query?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

864 Views

For using MySQL CASE statement while using UPDATE Query, you can use CASE statement. Let us first create a table −mysql> create table DemoTable    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserScore int    ); Query OK, 0 rows affected (0.29 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserScore) values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(UserScore) values(110); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(UserScore) values(120); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(UserScore) values(200); Query OK, 1 ... Read More

MySQL query to select a row which contains same number in a column with set of numbers separated by comma?

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

148 Views

You need to use FIND_IN_SET() for this. Let us first create a table −mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(20), CustomerAllProductPrice text ); Query OK, 0 rows affected (0.30 sec)Insert some records in the table using insert command. Here, we are inserting numbers separated by comma −mysql> insert into DemoTable(CustomerName, CustomerAllProductPrice) values('Chris', '245, 345, 678, 90, 45, 56, 78'); Query OK, 1 row affected (0.03 sec) mysql> insert into DemoTable(CustomerName, CustomerAllProductPrice) values('Chris', '98, 99, 90, 56, 77'); ... Read More

Performing mathematical operations in MySQL IF then ELSE is possible?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

289 Views

For performing mathematical operations and working with conditions, you can consider CASE statement. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FruitName varchar(100),    FruitPrice int    ); Query OK, 0 rows affected (0.26 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FruitName, FruitPrice) values('Orange', 250); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FruitName, FruitPrice) values('Banana', 100); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(FruitName, FruitPrice) values('Apple', 150); Query OK, 1 row affected (0.05 sec) ... Read More

MySQL system variable table_type doesn't work?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

99 Views

The variable table_type doesn’t work since this variable is deprecated as of MySQL 5.5.3. Use default_storage_engine instead. Following is the syntax −SET default_storage_engine = yourTableEngine;The table engine name may be InnoDB or MyISAM. Here, we will set engine type to MyISAM −mysql> SET default_storage_engine=MyISAM; Query OK,  0 rows affected (0.00 sec)Let us create a table.mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK,  0 rows affected (0.40 sec)Now check the engine type of above table −mysql> SHOW TABLE STATUS WHERE Name = 'DemoTable';This will produce the following output −+--------------+--------+---------+------------+------+----------------+-------------+------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+ | Name | Engine | ... Read More

Which MySQL Data Type can be used to store Negative Number?

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

2K+ Views

You can use TINYINT data type in MySQL to store negative number. Following is the syntax −CREATE TABLE yourTableName ( yourColumnName TINYINT . . . . N );Let us first create a table with a column set as type TINYINT −mysql> create table DemoTable ( Number tinyint ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert ... Read More

Updating boolean value in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

614 Views

To update boolean value, you can use SET. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, isMarried boolean    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(isMarried) values(false); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(isMarried) values(true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(isMarried) values(true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(isMarried) values(false); Query OK, 1 row affected (0.13 sec)Display all records ... Read More

Count items in a MySQL table with type ENUM involved?

Venu Madhavi
Updated on 22-Jan-2025 17:38:39

1K+ Views

Counting ENUM items in MySQL In MySQL, ENUM is a data type where it stores the predefined and fixed values making them ideal for categories such as sizes, grades, and statuses. Knowing the frequency of each value inside ENUM columns will allow you to make data-driven decisions. Below, we'll learn how to use MySQL's GROUP BY and COUNT() functions to get the count of ENUM values. GROUP BY(): It is used for aggregating data, as it follows you to perform calculations, such as totals, and averages. ... Read More

MySQL query to decrease the value of a specific record to zero?

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

159 Views

Use SET to decrease the value and WHERE to set the condition for a specific record to be 0. Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Number int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command &minusmysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Number) values(1); Query OK, 1 row affected ... Read More

How to concatenate all values of a single column in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

You can use group_concat() along with concat() to concatenate all values of a single column. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Robert'); ... Read More

Advertisements