Found 4381 Articles for MySQL

How to resolve the error that occurs while using a reserved word as a table or column name in MySQL?

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

1K+ Views

This error occurs when you try to use a reserved word as a table or column name. It can occur due to −Case 1: Whenever you use reserved word as a table name −mysql> create table insert −> ( −> Id int −> );The error is as follows −ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert ( Id int )' at line 1The above error occurred because the word ‘insert’ is a keyword in MySQL.Case 2 − Whenever you ... Read More

MySQL query to extract last word from a field?

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

3K+ Views

To extract last word from a field, use in-built SUBSTRING_INDEX() function. The syntax is as follows −SELECT SUBSTRING_INDEX(yourColumnName, ’ ‘, -1) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query to create a table −mysql> create table FirstWordDemo −> ( −> AllWords longtext −> ); Query OK, 0 rows affected (0.83 sec)Now insert some words in the table using insert command. The query is as follows −mysql> insert into FirstWordDemo values('This is the first MySQL Query'); Query OK, 1 row affected (0.11 ... Read More

Which MySQL type is most suitable for “price” column?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

24K+ Views

The best type for price column should be DECIMAL. The type DECIMAL stores the value precisely.For Example - DECIMAL(10, 2) can be used to store price value. It means the total digit will be 10 and two digits will be after decimal point.To understand the type DECIMAL, let us create a table.mysql> create table PriceDemo    −> (    −> ProductPrice DECIMAL(10, 2)    −> ); Query OK, 0 rows affected (0.60 sec)Now insert some records in the table in the form of price. The query to insert records is as follows −mysql> insert into PriceDemo values(12345.67); Query OK, 1 row ... Read More

Strip last two characters of a column in MySQL?

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

2K+ Views

You can strip last two characters with the help of SUBSTRING() and CHAR_LENGTH() methods. The syntax is as follows −select yourColumnName, SUBSTRING(yourColumnName, 1, CHAR_LENGTH(yourColumnName) - 2) AS anyVariableName from yourTableName;To understand the above syntax, let us create a table −mysql> create table LastTwoCharacters −> ( −> Words varchar(200) −> ); Query OK, 0 rows affected (0.71 sec)Now you can insert some records in the table with the help of select statement. The query to insert records is as follows −mysql> insert into LastTwoCharacters values('Hellooo'); Query OK, 1 row affected (0.23 sec) ... Read More

How to rearrange MySQL columns?

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

641 Views

To rearrange the MySQL columns, check the column arrangement with the help of show create command. The syntax is as follows −show create table yourTableName;The syntax to rearrange the MySQL columns is as follows −alter table yourTableName change column yourColumnName yourColumnName dataType firstFor the same purpose, you can use the after keyword. The syntax is as follows −alter table yourTableName change column yourColumnName yourColumnName dataType after yourSpecificColumnName;Let us first check the column arrangement for the already created table “AddColumn” −mysql> show create table AddColumn; The following is the output −+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table     | Create Table       ... Read More

How do I alter table column datatype on more than 1 column at a time in MySql?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

106 Views

To add more than 1 column with ALTER table command, you can use MODIFY column command. The syntax is as follows −alter table yourTableName modify column yourColumnName1 dataType, modify column yourColumnName2 dataType, . . . modify column yourColumnNameN dataTypeTo understand the above syntax, let us create a table. The following is the query −mysql> create table AddColumn    −> (    −> StudentID int,    −> StudentName varchar(200)    −> ); Query OK, 0 rows affected (0.49 sec)Above we have two columns in the table “AddColumn”. In this we will see how to modify more than one column datatype −mysql> ... Read More

How do I alter a MySQL table column defaults?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:24

228 Views

To alter a MySQL table column defaults, you can use the CHANGE command. The syntax is as follows −alter table yourTableName change yourCoumnName youColumnName datatype not null default Value;To understand the above syntax, let us create a table. The following is the query −mysql> create table DefaultDemo −> ( −> ArrivalTime timestamp −> ); Query OK, 0 rows affected (0.65 sec)Here is the query that describes the table with default column −mysql> desc DefaultDemo;The following is the output −+-------------+-----------+------+-----+---------+-------+ | Field | Type ... Read More

How to create a Cumulative Sum Column in MySQL?

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

3K+ Views

To create a cumulative sum column in MySQL, you need to create a variable and set to value to 0. Cumulative sum increments the next value step by step with current value.Firstly, you need to create a variable with the help of SET. The syntax is as follows −set @anyVariableName:= 0;The syntax to create a cumulative sum column in MySQL is as follows −select yourColumnName1, yourColumnName2, ........N, (@anyVariableName := @anyVariableName + yourColumnName2) as anyVariableName from yourTableName order by yourColumnName1;To understand the above concept, let us create a table. The following is the query to create a table −mysql> create table ... Read More

How to get the creation date of a MySQL table?

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

10K+ Views

To get the creation date of a MySQL table, use create_time from information_schema.tables. The syntax is as follows −SELECT create_time FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'yourDatabaseName' AND table_name = 'yourTableName';My table name is 'skiplasttenrecords' and database is ‘test’.Implement the above syntax for your database and table name. The query is as follows −mysql> SELECT create_time FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'test' −> AND table_name = 'skiplasttenrecords';The following is the output displaying the creation date −+---------------------+ | CREATE_TIME | +---------------------+ | 2018-11-29 15:47:14 | +---------------------+ 1 row in set (0.00 sec)

Calculate Age from given Date of Birth in MySQL?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

1K+ Views

To calculate age in MySQL from Date of Birth, you can use the following syntax −SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(yourColumnName) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(yourColumnName, 5)) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query to create a table.mysql> create table AgeCalculatesDemo −> ( −> YourDateOfBirth datetime −> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table with the help of insert command. These records are the DOBs through which we will calculate the age. The following is the ... Read More

Advertisements