Found 6705 Articles for Database

Difference between two timestamps in seconds in MySQL?

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

1K+ Views

Let us now see the following methods to calculate the time difference between two timestamps in seconds. Method The following is the query to calculate the difference between two timestamps. mysql> SELECT TIMESTAMPDIFF(SECOND, '2018-10-17 11:51:55', '2018-10-17 11:51:58'); The following is the output in seconds. +---------------------------------------------------------------------+ | TIMESTAMPDIFF(SECOND, '2018-10-17 11:51:55', '2018-10-17 11:51:58') | +---------------------------------------------------------------------+ | ... Read More

Remove new line characters from rows in MySQL?

George John
Updated on 26-Jun-2020 12:48:38

5K+ Views

The Trim() function is used to remove new line characters from data rows in MySQL. Let us see an example. First, we will create a table. The CREATE command is used to create a table.mysql> create table tblDemotrail - > ( - > id int, - > name varchar(100) - > ); Query OK, 0 rows affected (0.57 sec)Let us now insert some records.mysql> insert into tblDemotrail values(1, 'John '); Query OK, 1 row affected (0.15 sec) mysql> insert into tblDemotrail values(2, ' Carol'); Query OK, 1 row affected (0.32 sec) mysql> insert into tblDemotrail values(3, ' Sam ... Read More

How to store usernames and passwords safely in MySQL database?

Ankith Reddy
Updated on 26-Jun-2020 12:50:08

13K+ Views

To store username and passwords safely in MySQL database, we can use MD5().Let us see an example. First, we will create a table. The CREATE command is used to create a table.mysql> create table UserNameAndPasswordDemo - > ( - > U_Id int(10) unsigned NOT NULL AUTO_INCREMENT, - > UserId varchar(255) DEFAULT NULL, - > UserPassword varchar(255) DEFAULT NULL, - > primary key(U_Id), - > UNIQUE KEY `UserId` (`UserId`) - > ); Query OK, 0 rows affected (0.61 sec)Inserting records and safely storing passwords with the help of MD5().mysql> INSERT INTO UserNameAndPasswordDemo(UserId, UserPassword) VALUES ('John@gg.com', MD5('john123')); Query OK, 1 row affected ... Read More

Simulating MySQL's ORDER BY FIELD() in PostgreSQL?

Arjun Thakur
Updated on 26-Jun-2020 12:53:09

376 Views

The following is the process to simulate MySQL’s ORDER BY FIELD() in PostgreSQL.We have used an Online Compiler to run PostgreSQL.Let us now see what we did above to get the output.Firstly, we created a table.create table PostgreOrderIdDemo (    countryName varchar(20) );Inserted records with the help of INSERT command.insert into PostgreOrderIdDemo values('IND'); insert into PostgreOrderIdDemo values('US'); insert into PostgreOrderIdDemo values('UK');The following is the syntax in PostgreSQL.SELECT * FROM yourTableName ORDER BY CASE WHEN columnName='IND' THEN 1 WHEN columnName='US' THEN 2 WHEN columnName='UK' THEN 3 ELSE 0 END, columnName;ExampleLet us now use the above syntax to get the output.SELECT * ... Read More

How to use a function for default value in MySQL?

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

1K+ Views

We cannot use a function for default value in MySQL, but we can use triggers. Let us see an example. First, we will create a table. The CREATE command is used to create a table. mysql> CREATE table TbLFunctionTrigger - > ( - > id int, - > username varchar(100) - > ); Query OK, 0 rows affected (0.55 sec) The following is the syntax to create a trigger and include a default value. CREATE TRIGGER anyName BEFORE INSERT ON yourTableName FOR EACH ROW ... Read More

Generating a unique random 10 character string using MySQL?

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

5K+ Views

In order to generate a 10 character string, we can use inbuilt functions ‘rand()’ and ‘char()’. The following is the query to generate random 10 character string. mysql> SELECT concat( - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97), - > char(round(rand()*25)+97) - > )AS Random10CharacterString; ... Read More

IF ELSE statement in a MySQL Statement?

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

748 Views

In an If-Else statement, the condition is evaluated to be true or false depending on the value. Let us see an example. Firstly, we will create a table. The CREATE command is used to create a table. mysql> create table IfelseDemo - > ( - > id int, - > FirstName varchar(100) - > ); Query OK, 0 rows affected (0.46 sec) Records are inserted with the help of INSERT command. mysql> insert into IfelseDemo values(1, 'John'); Query OK, 1 row affected (0.13 sec) ... Read More

How to use MySQL JOIN without ON condition?

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

4K+ Views

We can use ‘cross join’ without on condition. Cross join gives the result in cartesian product form. For instance, if in one table there are 3 records and another table has 2 records, then the first record will match with all the second table records. Then, the same process will be repeated for second record and so on. Example of cross join Creating the first table mysql> CREATE table ForeignTableDemo - > ( - > Id int, - > Name varchar(100), - > FK int ... Read More

MySQL ON vs USING?

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

6K+ Views

In general, we use ON in MySQL. In Joins, we use ON in a set of columns. USING is useful when both the tables share a column of the exact same name on which they join. Example of On. Creating our first table. mysql> CREATE table ForeignTableDemo -> ( -> Id int, -> Name varchar(100), - > FK int - > ); Query OK, 0 rows affected (0.47 sec) Creating our second table. mysql> CREATE table PrimaryTableDemo - > ... Read More

Usage of backtick in SQL statements?

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

505 Views

The backtick can be used in MySQL. To create a table, we can put table_name in backticks. Example of Backtick in MySQL. The CREATE command is used to create a table. Here, we have added the table name using the backtick symbol. mysql> create table `backtickSymbol` -> ( -> uniId int -> ); Query OK, 0 rows affected (1.65 sec) Records are inserted with the help of INSERT command. mysql> insert into `backtickSymbol` values(1); Query OK, 1 row affected (0.20 sec) mysql> insert into `backtickSymbol` values(2); Query ... Read More

Advertisements