Found 6702 Articles for Database

Set auto increment initial value for MySQL table using ALTER command

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

556 Views

To set auto increment initial value for MySQL table, use ALTER command. The first step would bealter table yourTableName modify yourColumnName int NOT NULL AUTO_INCREMENT PRIMARY KEY, add index(yourColumnName);The second step is as followsalter table yourTableName AUTO_INCREMENT=yourStartingValue;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table setAutoIncrementDemo    -> (    -> UserId int,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.75 sec)Now implement the above two steps to set auto increment initial value for MySQL table.Step 1 -The query is as followsmysql> alter ... Read More

How to add +1 to existing MySQL values?

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

580 Views

Let us see an example and create a table first.mysql> create table Add1ToExistingValue    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into Add1ToExistingValue values(10); Query OK,  1 row affected (0.12 sec) mysql> insert into Add1ToExistingValue values(13); Query OK,  1 row affected (0.15 sec) mysql> insert into Add1ToExistingValue values(15); Query OK,  1 row affected (0.13 sec) mysql> insert into Add1ToExistingValue values(16); Query OK,  1 row affected (0.14 sec) mysql> insert into Add1ToExistingValue values(20); Query OK,  1 row affected (0.16 sec) mysql> insert into Add1ToExistingValue values(40); Query OK,  1 row affected (0.15 sec) mysql> insert into Add1ToExistingValue values(50); Query OK,  1 row affected (0.11 sec) mysql> insert into Add1ToExistingValue values(55); Query OK,  1 row affected (0.17 sec) mysql> insert into Add1ToExistingValue values(56); Query OK,  1 row affected (0.17 sec)Display all records from the table using select statement.The query is as followsmysql> select *from Add1ToExistingValue;The following is the output+-------+ | Value | +-------+ | 10   ... Read More

How to query for records where field is null or not set in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

677 Views

Let us work around two cases −Case 1 − The syntax is as follows when the field is present and set to null.db.yourCollectionName.count({yourFieldName: null});Case 1 − The syntax is as follows when the field is not present and not set.db.yourCollectionName.count({yourFieldName: {$exists: false}});To understand both the above syntaxes, let us create a collection with the document. The query to create a collection with a document is as follows −> db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Larry", "EmployeeAge":null, "EmployeeSalary":18500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a995c6cea1f28b7aa07fe") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Bob", "EmployeeAge":21, "EmployeeSalary":23500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a99836cea1f28b7aa07ff") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Carol", "EmployeeSalary":45500}); { ... Read More

Resolve the error Column count doesn’t match value count in MySQL?

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

638 Views

This type of error occurs when number of columns does not match whenever you are inserting records in the destination table. For a demo example, let us create a tablemysql> create table errorDemo    -> (    -> User_Id int NOT NULL AUTO_INCREMENT,    -> User_Name varchar(20),    -> PRIMARY KEY(User_Id)    -> ); Query OK, 0 rows affected (0.47 sec)The error is as followsmysql> insert into errorDemo values('John'); ERROR 1136 (21S01): Column count doesn't match value count at row 1To avoid this type of error, you need to use the following syntaxinsert into yourTableName(yourColumnName1, yourColumnName2, ...N)values(yourValue1, yourValue2, ....N);Insert some ... Read More

Find items that do not have a certain field in MongoDB?

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

119 Views

To find items that do not have a certain field, use the $exists operator. The syntax is as follows −> db.yourCollectionName.find({"yourItemName":{$exists:false}}).pretty();To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findDocumentDoNotHaveCertainFields.insertOne({"UserId":101, "UserName":"John", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a95fb6cea1f28b7aa07fb") } > db.findDocumentDoNotHaveCertainFields.insertOne({"UserName":"David", "UserAge":22, "UserFavouriteSubject":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a96116cea1f28b7aa07fc") } > db.findDocumentDoNotHaveCertainFields.insertOne({"UserName":"Bob", "UserAge":24, "UserFavouriteSubject":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a96306cea1f28b7aa07fd") }Display all documents from a collection with the help of find() method. The ... Read More

How to revert rows to default column value in MySQL?

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

149 Views

To revert rows to default column value, let us first create a demo tablemysql> create table defaultDemo    -> (    -> Id int    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into defaultDemo values(10); Query OK, 1 row affected (0.25 sec) mysql> insert into defaultDemo values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into defaultDemo values(30); Query OK, 1 row affected (0.14 sec) mysql> insert into defaultDemo values(40); Query OK, 1 row affected (0.11 sec) mysql> insert into defaultDemo values(80); Query OK, ... Read More

Performing regex Queries with PyMongo?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

889 Views

PyMongo is a Python distribution containing tools for working with MongoDB. To perform regex queries with PyMongo, the syntax is as follows −db.yourCollectionName.find({'yourCollectionName':{'$regex':'^yourWords'}}).pretty();The above syntax will give all those documents that start from a specific word.To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.performRegex.insertOne({"ClientName":"Larry", "ClientFolderInformation":[    "Folder 1", "Folder 2", "Folder 3", "Folder 4", "Folder 5"], "MainFolderLocation":"/MainFolder/Details/ClientFolder" }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a8b186cea1f28b7aa07f2") } > db.performRegex.insertOne({"ClientName":"Larry", "ClientFolderInformation":[    "ClientFolder 1", "ClientFolder 2", "ClientFolder 3", "ClientFolder 4", "ClientFolder 5"],   ... Read More

How to convert positive value to negative while inserting in MySQL?

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

1K+ Views

Let us first create a tablemysql> create table recordsDemo    -> (    -> UserId int,    -> Value int    -> ); Query OK, 0 rows affected (0.52 sec)Now insert some records in the table using insert command.The query is as followsmysql> insert into recordsDemo values(1, 10); Query OK, 1 row affected (0.17 sec) mysql> insert into recordsDemo values(3, 598); Query OK, 1 row affected (0.18 sec) mysql> insert into recordsDemo values(5, 786); Query OK, 1 row affected (0.25 sec) mysql> insert into recordsDemo values(7, 189); Query OK, 1 row affected (0.16 sec) mysql> insert into recordsDemo values(9, 345); ... Read More

How to loop thrugh a stored procedure in MySQL?

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

178 Views

Let us see how to loop through a stored procedure in MySQLmysql> DELIMITER // mysql> CREATE PROCEDURE do_WhileDemo(LastValue INT)    -> BEGIN       -> SET @loop = 0;       -> REPEAT          -> SET @loop= @loop+ 1;          -> select @loop;          -> UNTIL @loop >LastValue       -> END REPEAT;    -> END // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;Now call the stored procedure with the help of CALL command.The query is as followsmysql> call do_WhileDemo(10);The following is the output+-------+ | ... Read More

Selecting Random Result from MySQL?

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

100 Views

You need to use rand() function to select random result from MySQL.The syntax is as followsselect *from yourTableName order by rand() limit 1;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table selectRandomRecord    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into selectRandomRecord(StudentName) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRandomRecord(StudentName) values('Carol'); Query OK, ... Read More

Advertisements