Found 4378 Articles for MySQL

Reset the primary key to 1 after deleting all the data in MySQL?

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

7K+ Views

To reset the primary key to 1 after deleting the data, use the following syntaxalter table yourTableName AUTO_INCREMENT=1; truncate table yourTableName;After doing the above two steps, you will get the primary key beginning from 1.To understand the above concept, let us create a table. The query to create a table is as followsmysql> create table resettingPrimaryKeyDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into resettingPrimaryKeyDemo values(); Query OK, 1 row ... Read More

How to find capital letters with Regex in MySQL?

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

377 Views

You can use REGEXP BINARY for thisselect *from yourTableName where yourColumnName REGEXP BINARY '[A-Z]{2}';Let us first create a tablemysql> create table FindCapitalLettrsDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentFirstName varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('JOHN'); Query OK, 1 row affected (0.24 sec) mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... Read More

How to see spaces in data when selecting with MySQL command line client?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

152 Views

Use quote() function for this. The syntax is as follows −select yourColumnName, quote(yourColumnName) from yourTableName;To understand the concept, let us create a table. The query to create a table is as follows −mysql> create table seeSpacesDemo    -> (    -> spaceValue varchar(10)    -> ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into seeSpacesDemo values(""); Query OK, 1 row affected (0.70 sec) mysql> insert into seeSpacesDemo values(" "); Query OK, 1 row affected (0.45 sec) mysql> insert into seeSpacesDemo values(" "); Query OK, 1 ... Read More

How to add a random number between 30 and 300 to an existing field in MySQL?

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

81 Views

Let us first create a demo tablemysql> create table RandomNumberDemo    -> (    -> MyNumber int    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into RandomNumberDemo values(17); Query OK, 1 row affected (0.20 sec) mysql> insert into RandomNumberDemo values(18); Query OK, 1 row affected (0.12 sec) mysql> insert into RandomNumberDemo values(29); Query OK, 1 row affected (0.49 sec)Display all records from the table using select statement. The query is as follows −mysql> select *from RandomNumberDemo;The following is the output+----------+ | MyNumber | ... Read More

SELECT a FLOAT with given precision in MySQL

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

1K+ Views

You can use ROUND() function.The syntax is as followsSELECT ROUND(yourColumnName, yourPrecisionIntegerValue) from yourTableName;To understand the concept, let us create a table. The query to create a table is as followsmysql> create table givenPrecisionDemo -> ( -> Amount float -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into givenPrecisionDemo(Amount) values(45.678); Query OK, 1 row affected (0.12 sec) mysql> insert into givenPrecisionDemo(Amount) values(123.456); Query OK, 1 row affected (0.15 sec) mysql> insert into givenPrecisionDemo(Amount) values(245.890); Query OK, ... Read More

MySQL command-line tool: How to find out number of rows affected by a DELETE?

Samual Sam
Updated on 30-Jul-2019 22:30:25

466 Views

You can use row_count() at the end for this. Let us first create a table −mysql> create table rowAfftectedByDeleteDemo    -> (    -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerName varchar(20)    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Carol'); Query OK, 1 row affected (0.10 sec) mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Sam'); Query ... Read More

Can we store CSS color values in MySQL?

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

728 Views

Yes, we can. In order to store CSS color value, you can use CHAR(6) without # symbol for hexadecimal. Let us see an example and create a tablemysql> create table storeCSSColorDemo -> ( -> CSSValue char(6) -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command. The records here are individual color values in hexadecimal, for which we have used char(6)mysql> insert into storeCSSColorDemo values('FF0000'); Query OK, 1 row affected (0.13 sec) mysql> insert into storeCSSColorDemo values('FFA500'); Query OK, 1 row affected (0.86 sec) ... Read More

How to write a procedure to insert data in the table in phpMyAdmin?

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

373 Views

Let us first create a new table and understand the concept in continuationmysql> create table StoredProcedureInsertDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20),    -> UserAge int    -> ); Query OK, 0 rows affected (0.63 sec)Here is the query to create a stored procedure to insert data in to the tablemysql> DELIMITER // mysql> create procedure procedure_InsertIntoTable(IN FirstName VARCHAR(100), IN Age INT)    -> BEGIN    -> insert into StoredProcedureInsertDemo(UserName, UserAge) values (FirstName, Age);    -> END    -> // Query OK, 0 rows affected (0.34 sec) mysql> DELIMITER ;Call ... Read More

How to retrieve a random row or multiple random rows in MySQL?

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

150 Views

You can use RAND() method for this. To retrieve a random row, use the following syntaxSELECT *FROM yourTableName ORDER BY RAND() LIMIT yourIntegerNumber;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table gettingRandomRow    -> (    -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerName varchar(100)    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into gettingRandomRow(CustomerName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into gettingRandomRow(CustomerName) values('Robert'); ... Read More

MySQL query to GROUP BY multiple columns

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

645 Views

You can use IF() to GROUP BY multiple columns. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table MultipleGroupByDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerId int,    -> ProductName varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into MultipleGroupByDemo(CustomerId, ProductName) values(1000, 'Product-1'); Query OK, 1 row affected (0.20 sec) mysql> insert into MultipleGroupByDemo(CustomerId, ProductName) values(1001, 'Product-2'); Query OK, 1 row affected (0.18 ... Read More

Advertisements