
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Prevent having a zero value in a MySQL field?
Use trigger BEFORE INSERT on the table to prevent having a zero value in a MySQL field. Let us first create a table −
mysql> create table DemoTable(Value int); Query OK, 0 rows affected (0.85 sec)
Let us create a trigger to prevent having a zero value in a MySQL field −
mysql> DELIMITER // mysql> create trigger preventing_to_insert_zero_value before insert on DemoTable for each row begin if(new.Value = 0) then SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You can not provide 0 value'; END if; end // Query OK, 0 rows affected (0.34 sec) mysql> DELIMITER ;
Let us insert 0 value in the table and check the value is going to be inserted or not. It won’t allow −
mysql> insert into DemoTable values(0); ERROR 1644 (45000): You cannot provide 0 value
So, in the above table, you cannot insert 0 value because of trigger. But, you can insert some other value.
Let us now insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(-1000); Query OK, 1 row affected (0.41 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | -1000 | +-------+ 2 rows in set (0.00 sec)
- Related Articles
- How to derive value of a field from another field in MySQL?
- How to implement HAVING LENGTH(field) in MySQL?
- How to swap a specific field value in MySQL?
- How to decrement a value in MySQL keeping it above zero?
- Getting the maximum value from a varchar field in MySQL
- How can we provide a date with only year (zero months & zero days) value in MySQL?
- Searching for an integer value in a varchar field in MySQL?
- How to retrieve a value with MySQL count() having maximum upvote value?
- How to select data in MySQL where a field has a minimum value?
- Simple way to toggle a value of an int field in MySQL
- Duration ZERO field in Java
- MySQL query to decrease the value of a specific record to zero?
- Show row with zero value after addition in MySQL?
- Count rows having three or more rows with a certain value in a MySQL table
- MySQL query to fetch only a single field on the basis of boolean value in another field

Advertisements