Found 6705 Articles for Database

Sorting varchar field numerically in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

2K+ Views

‘LPAD(lower(column_name))’ is used to sort the varchar field numerically in MySQL. Let us see an example. Firstly, we will create a table. The CREATE command is used to create a table. mysql> create table SortingvarcharDemo -> ( -> List varchar(10) -> ); Query OK, 0 rows affected (0.82 sec) Records are inserted with the help of INSERT command. mysql> insert into SortingvarcharDemo values("99"); Query OK, 1 row affected (0.12 sec) mysql> insert into SortingvarcharDemo values("9"); Query OK, 1 row affected (0.17 sec) mysql> insert into SortingvarcharDemo ... Read More

How to check similarity between two strings in MySQL?

Arjun Thakur
Updated on 26-Jun-2020 12:27:33

2K+ Views

Similarity between two strings can be checked with the help of ‘strcmp()’ function. Here are the conditions.If both strings are equal, then it returns 0.If first string is less than the second string, it returns -1.If first string is greater than the second string, it returns 1.Here is an example.Case 1 − If both strings are equal.The following is the query.mysql > SELECT STRCMP("demo", "demo");The following is the output of the above query.+------------------------+ | STRCMP("demo", "demo") | +------------------------+ | 0 ... Read More

Is it possible to use UPDATE query with LIMIT in MySQL?

Chandu yadav
Updated on 26-Jun-2020 12:30:35

10K+ Views

Yes, it is possible to use UPDATE query with LIMIT in MySQL. Let us see how.For our example, we will first create a table. The CREATE command is used to create a table.mysql>CREATE table tblUpdateLimit -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)Records are inserted with the help of INSERT command.mysql>INSERT into tblUpdateLimit values(1, 'John'); Query OK, 1 row affected (0.54 sec) mysql>INSERT into tblUpdateLimit values(2, 'Carol'); Query OK, 1 row affected (0.12 sec) mysql>INSERT into tblUpdateLimit values(3, 'Smith'); Query OK, 1 row affected (0.10 sec) mysql>INSERT into ... Read More

How to change MySQL timezone?

George John
Updated on 30-Jul-2019 22:30:23

367 Views

To change the timezone in MySQL, we can use the SET command. The following is the syntax. SET time_zone=’someValue’; Let us now use the above syntax and apply it in the below query. mysql > SET time_zone = '+8:00'; Query OK, 0 rows affected (0.00 sec) Above, the time_zone is changed by a value equivalent to 8. To change the timezone globally, use the GLOBAL command. Here is the syntax. SET GLOBAL time_zone = 'someValue'; Here is an example. mysql> SET GLOBAL time_zone = '+8:00'; Query OK, 0 rows affected (0.00 sec) The above query will change the timezone globally.

How to add super privileges to MySQL database?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

8K+ Views

Firstly, check the MySQL database with DESC command. mysql>DESC mysql.db; The following is the output. +-----------------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+---------------+------+-----+---------+-------+ | Host | char(60) | NO | PRI | | ... Read More

MySQL pagination without double-querying?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

296 Views

To work with MySQL pagination, firstly let us see how to use CREATE command and use it to create a table. mysql>CREATE table RowCountDemo -> ( -> ID int, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.95 sec) Records are inserted with the help of INSERT command. mysql>INSERT into RowCountDemo values(1, 'Larry'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into RowCountDemo values(2, 'John'); Query OK, 1 row affected (0.13 sec) mysql>INSERT into RowCountDemo values(3, 'Bela'); Query OK, 1 row ... Read More

How to raise an error within MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

3K+ Views

MySQL has introduced signals similar to an exception in other languages. Let us first see the syntax of signal. SIGNAL SQLSTATE ' PredefinedValueforSignalError' SET MESSAGE_TEXT = 'AnyMessageInformation'; Above, we have set our own error message text as well. We will apply the above query to get an error message with the help of signals. mysql > SIGNAL SQLSTATE '42927' SET MESSAGE_TEXT = 'Error Generated'; The following is the output of the above query. ERROR 1644 (42927): Error Generated In the above output, ‘42927’ is a SQLSTATE and ‘Error Generated’ is an error message, which we added.

Fastest way to count number of rows in MySQL table?

Ankith Reddy
Updated on 26-Jun-2020 12:23:37

1K+ Views

Let us first see an example to create a table, add records and display them. The CREATE command is used to create a table.mysql> CREATE table RowCountDemo -> ( -> ID int, -> Name varchar(100) > ); Query OK, 0 rows affected (0.95 sec)Records are inserted with the INSERT command.mysql>INSERT into RowCountDemo values(1, 'Larry'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into RowCountDemo values(2, 'John'); Query OK, 1 row affected (0.13 sec) mysql>INSERT into RowCountDemo values(3, 'Bela'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into RowCountDemo values(4, 'Jack'); Query OK, 1 row affected (0.11 sec) ... Read More

Which one is better in MySQL - NULL or empty string?

Arjun Thakur
Updated on 26-Jun-2020 12:24:27

441 Views

The choice depends upon the database. In ORACLE database, an empty string is converted to NULL.In MySQL, the usage of an empty string is better as compared to NULL. It is easy to check for an empty string with some boundary conditions, while this cannot be done with NULL. To find NULL, we need to add an extra condition i.e. ‘IS NULL’We can check that the length of NULL is 0 while length of empty string is 1.To check the length of NULL.mysql>SELECT count(NULL);The following is the output of the above query.+-----------------+ | count(NULL) | +-----------------+ ... Read More

How to escape single quotes in MySQL?

Chandu yadav
Updated on 26-Jun-2020 12:24:56

559 Views

We can escape single quotes with the help of the SELECT statement. For instance, when single quotes are encountered in a name, eg. “Carol’s”.Let us see the syntax.SELECT ‘SomeValue’;Here is an example that display how to include text with single quotes.mysql> SELECT 'Carol\'s Taylor.'; The following is the output.+-------------------+ | Carol's Taylor | +-------------------+ | Carol's Taylor | +-------------------+ 1 row in set (0.00 sec)

Advertisements