Found 4378 Articles for MySQL

Is it impossible to add a column in MySQL specifically before another column?

Sharon Christine
Updated on 30-Jun-2020 14:05:36

334 Views

No, you can easily add a column before another column using ALTER.Note − To add a column at a specific position within a table row, use FIRST or AFTER col_name Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20),    -> CountryName varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Let us check all the column names from the table −mysql> show columns from DemoTable;OutputThis will produce the following output −+-------------+--------------+------+-----+---------+-------+ | Field       | Type         | Null | Key ... Read More

Adding integers from a variable to a MySQL column?

Sharon Christine
Updated on 30-Jun-2020 14:06:34

684 Views

To set a variable, use MySQL SET. For adding integers from a variable, use UPDATE and SET as in the below syntax −set @anyVariableName:=yourValue; update yourTableName set yourColumnName=yourColumnName+ @yourVariableName;Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.15 sec) mysql> ... Read More

Why BINARY keyword used with MySQL REGEXP operator?

karthikeya Boyini
Updated on 30-Jun-2020 13:48:06

451 Views

Use the BINARY keyword to force REGEXP to match the string as a binary string. We will see the difference here.Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command. We have names here with different cases −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('JOHN'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('john'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

MySQL query to display a substring before a special character in a string

karthikeya Boyini
Updated on 30-Jun-2020 13:49:13

278 Views

Use the LOCATE() and SUBSTRING() method for this in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Title text    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Introduction To Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Introduction - To MySQL'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------------------+ | Title           ... Read More

Convert hex string to number in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 13:51:10

1K+ Views

Use the CONV() method to convert hex string to number −select CONV(yourColumnName, 16, 10) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> HexString varchar(100) -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('A'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('F'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('B'); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable values('ABC'); Query OK, 1 row affected (0.11 sec)Display ... Read More

Extract the middle part of column values in MySQL surrounded with hyphens and display in a new column?

Sharon Christine
Updated on 30-Jun-2020 13:52:20

175 Views

Use the SUBSTR() method to extract the middle part of column values surrounded with hyphens, for example, “11-84848-11”.Let us first create a table −mysql> create table DemoTable -> ( -> Number varchar(100), -> Number1 varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number) values('11-84848-11'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Number) values('22-99999-22'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------+---------+ | Number ... Read More

How to set a string with hyphen and numbers in MySQL varchar?

Sharon Christine
Updated on 30-Jun-2020 13:53:58

3K+ Views

To set a string with hyphen and numbers, you need to use single quotes. For example, 'Customer-1234-899', 'Customer-9383-901', etc.Let us first create a table −mysql> create table DemoTable -> ( -> CustomerId varchar(100) -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Customer-1234-899'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Customer-8373-900'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Customer-9383-901'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTableOutputThis ... Read More

Change multiple columns in a single MySQL query?

Sharon Christine
Updated on 30-Jun-2020 13:54:52

462 Views

For this, use UPDATE and REPLACE() in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentCountryName varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentCountryName) values('John', 'US'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql select *from DemoTable;OutputThis will produce the following output −+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ |       ... Read More

MySQL query to get the next number in sequence for AUTO_INCREMENT field?

karthikeya Boyini
Updated on 30-Jun-2020 13:55:48

917 Views

Let us first create a table −mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+----+ | Id | +----+ | 1 ... Read More

MySQL query to remove special characters from column values?

karthikeya Boyini
Updated on 30-Jun-2020 13:56:49

1K+ Views

Let us first create a table −mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU#123'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('STU#567'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('STU#98494'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-----------+ | StudentId | +-----------+ | STU#123 | | STU#567 ... Read More

Advertisements