Found 6705 Articles for Database

How to fix the incorrect datetime value while inserting in a MySQL table?

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

23K+ Views

To avoid the incorrect datetime value error, you can use the STR_TO_DATE() method.As we know the datetime format is YYYY-MM-DD and if you won’t insert in the same format, the error would get generated.Let us see what actually lead to this error. For this, let us create a new table. The query to create a table is as followsmysql> create table CorrectDatetimeDemo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > ArrivalTime datetime   - > ); Query OK, 0 rows affected (0.63 sec)The occurs when we try to include a ... Read More

How to add a “created at” column in a table to set the timestamp in MySQL?

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

4K+ Views

You need to use ALTER command to add a created at column to an already created table in MySQL.Let us first create a table. The query to create a table is as follows. Here is your table without the “created at” columnmysql> create table formDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Email varchar(128), - > PhoneNumber varchar(15), - > Country varchar(30), - > Platform varchar(40) - > ); Query OK, 0 ... Read More

Can MySQL concatenate strings with ||?

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

167 Views

Yes, you can concatenate strings with || in MySQL with the help of sql_mode. Set the sql_mode to PIPES_AS_CONCAT.The syntax is as followsset sql_mode=PIPES_AS_CONCAT;The following is the syntax to concat with the help of ||.SELECT ‘yourValue' || yourColumName AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table PipeConcatDemo    - > (    - > Name varchar(20)    - > ); Query OK, 0 rows affected (0.93 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into PipeConcatDemo values('Larry'); Query ... Read More

Check how many rows are in a MySQL database table?

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

1K+ Views

To know how many rows are in a ySQL database table, you need to use aggregate function COUNT(*).The syntax is as followsSELECT COUNT(*) FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table CountRowsDemo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > Name varchar(20)    - > ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CountRowsDemo(Name) values(NULL); Query OK, 1 row affected (0.15 sec) mysql> ... Read More

Is there an operator in MySQL to implement multiple NOT conditions like WHERE id != 5 AND id != 10 AND id != 15?

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

544 Views

Yes, for this MySQL comes with a NOT IN.The syntax is as followsSELECT *FROM yourTableName WHERE yourColumnName NOT IN(1, 2, 7);To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table User_informations    - > (    - > UserId int,    - > UserName varchar(20)    - > ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into User_informations values(12, 'Maxwell'); Query OK, 1 row affected (0.17 sec) mysql> insert into User_informations values(7, 'David'); Query OK, 1 ... Read More

Does SELECT TOP command exist in MySQL to select limited number of records?

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

239 Views

There is no concept of TOP in MySQL. The alternate way to write your query is using LIMIT i.e to select 2 records, you need to use TOP 2. Let us see the syntax for the same in MySQLSELECT *FROM yourTableName ORDER BY yourColumnName DESC LIMIT 2;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table Top2Demo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > Name varchar(20),    - > Age int    - > ); Query OK, 0 rows ... Read More

How to get server_id in MySQL?

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

5K+ Views

To get the server_id, use the system defined variable @@server_id. You cannot use only a single @ as user defined variable for server_id.The syntax is as followsSELECT@@ server_idAs an alternate, you can use SHOW VARIABLES command.The syntax is as followsSHOW VARIABLES LIKE ‘server_id’;Case 1The query is as followsmysql> SELECT @@server_id as SERVER_ID;The following is the output+-----------+ | SERVER_ID | +-----------+ |         1 | +-----------+ 1 row in set (0.00 sec)Case 2The query is as followsmysql> show variables like 'server_id';The following is the output+---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | ... Read More

Check if value exists in a comma separated list in MySQL?

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

12K+ Views

To check if value exists in a comma separated list, you can use FIND_IN_SET() function.The syntax is as followsSELECT *FROM yourTablename WHERE FIND_IN_SET(‘yourValue’, yourColumnName) > 0;Let us first create a table. The query to create a table is as followsmysql> create table existInCommaSeparatedList - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Name varchar(200) - > ); Query OK, 0 rows affected (0.68 sec)Now you can insert some records in the table using insert command.The query is as followsmysql> insert into existInCommaSeparatedList(Name) values('John, ... Read More

How to correctly implement END IF statement in a MySQL Stored Procedure?

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

255 Views

The following is the syntax of END IF statement in MySQLIF yourCondition THEN yourStatement ELSE yourStatement END IFHere is the demo of END IF statement while creating a stored proceduremysql> DELIMITER // mysql> CREATE PROCEDURE Sp_Test( IN value INT ) - > BEGIN - > IF value < 10 THEN - > select 'Your value is less than 10'; - > ELSE - > select 'Your value is greater than 10'; - > END IF; ... Read More

How to get the longest VarChar length in MySQL?

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

4K+ Views

To get the longest varchar length, you need to use CHAR_LENGTH().The syntax is as followsSELECT Max(CHAR_LENGTH(yourColumnName)) AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table CharLengthDemo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > Sentence varchar(255)    - > ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CharLengthDemo(Sentence) values('Java is an object oriented programming language' - > ); Query OK, 1 row ... Read More

Advertisements