Found 4378 Articles for MySQL

What is the usage of zerofill in a MySQL field?

karthikeya Boyini
Updated on 30-Jun-2020 14:17:39

664 Views

Zerofill pads the displayed value of the field with zeros up to the display width specified in the column definition. For example, if column is set int(8), therefore the width is 8. If the number is let’s say 4376, then zero will be padded on the left for total width i.e. 8 −00004376Let us first create a table −mysql> create table DemoTable -> ( -> Number int(8) zerofill -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

MySQL query to return a substring after delimiter?

Sharon Christine
Updated on 30-Jun-2020 14:18:32

759 Views

Use SUBSTRING() to return values after delimiter. Let us first create a table −mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John is good in MySQL, Sam is good in MongoDB, Mike is good in Java'); 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 −+-------------------------------------------------------------------+ | Title ... Read More

If I truncate a table, should I also add indexes?

Sharon Christine
Updated on 30-Jun-2020 14:19:51

665 Views

If you truncate a table, you do not need to add indexes because table is recreated after truncating a table and indexes get added automatically.Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.65 sec)Following is the query to create an index −mysql> create index Index_firstName_LastName on DemoTable(FirstName, LastName); Query OK, 0 rows affected (1.04 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> ... Read More

Find and display duplicate values only once from a column in MySQL

Sharon Christine
Updated on 30-Jun-2020 14:21:28

620 Views

Let us first create a table −mysql> create table DemoTable -> ( -> value int -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.24 sec) mysql> insert into ... Read More

Change the column name from StudentName to FirstName in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 14:27:02

114 Views

Use CHANGE with ALTER statement. Let us first create a table −mysql> create table DemoTable -> ( -> StudentName varchar(100), -> Age int -> ); Query OK, 0 rows affected (0.84 sec)Now check the description of table −mysql> desc DemoTable;OutputThis will produce the following output −+----------------+--------------+------+-----+---------+-------+ | Field          | Type         | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | StudentName    | varchar(100) | YES | | NULL | | | Age         ... Read More

Count only null values in two different columns and display in one MySQL select statement?

Sharon Christine
Updated on 30-Jun-2020 13:57:51

371 Views

Use IS NULL to test for NULL value. Let us first create a table −mysql> create table DemoTable    -> (    -> Number1 int,    -> Number2 int    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(NULL, NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3, NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL, 90); Query OK, 1 row affected ... Read More

Display records from two columns based on comparison in MySQL?

Sharon Christine
Updated on 30-Jun-2020 13:59:02

416 Views

Let us first create a table −mysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 200); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(200, 100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(300, 400); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(400, 300); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(500, 600); Query OK, 1 ... Read More

Sort a column in descending order after placing argument in MySQL IN()?

karthikeya Boyini
Updated on 30-Jun-2020 14:01:35

56 Views

For this, use FIELD() function with DESC. 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.16 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(50); Query OK, 1 row ... Read More

Can we use ORDER BY NULL in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 14:02:38

337 Views

Yes, we can do thatNote − Before MySQL 5.7, ORDER BY NULL was useful, but with MySQL 8.0, specifying ORDER BY NULL, for example, at the end to suppress implicit sorting is no longer necessary.Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(10) -> ); Query OK, 0 rows affected (1.01 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David'); Query OK, 1 ... Read More

MySQL XOR operator with IN clause?

Sharon Christine
Updated on 30-Jun-2020 14:03:58

596 Views

MySQL XOR returns TRUE if one or the other operand (or expression) but not both is TRUE. The IN clause is used to specify a condition with any other MySQL query.Let us first create a tablemysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(5, 5); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(6, 6); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(7, 7); Query OK, ... Read More

Advertisements