Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set maximum value to a column

Tags:

mysql

I have a table with a column (int type) called age. This column should hold maximun value 50. If it exceeds then it shouldn't update that row.

Means this column shold take values from 0 to 50.

If I try to update that to 51 then that shouldn't allow.

Could any one help....!

like image 736
Gnanendra Avatar asked May 02 '11 11:05

Gnanendra


People also ask

How do you SELECT the maximum of a column?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

Can we use Max on a column?

MAX can be used with numeric, character, and datetime columns, but not with bit columns.

How do you set a minimum and maximum value in SQL?

To ask SQL Server about the minimum and maximum values in a column, we use the following syntax: SELECT MIN(column_name) FROM table_name; SELECT MAX(column_name) FROM table_name; When we use this syntax, SQL Server returns a single value. Thus, we can consider the MIN() and MAX() functions Scalar-Valued Functions.

What function is used to find the maximum value of a column?

The MAX() function returns the largest value of the selected column.


1 Answers

Try this:

CREATE TRIGGER check_trigger
  BEFORE INSERT
  ON table
  FOR EACH ROW
BEGIN
  IF NEW.age<0 OR NEW.age>50 THEN
    CALL `Error: Wrong values for age`; -- this trick will throw an error
  END IF;
END
like image 194
Marco Avatar answered Oct 08 '22 22:10

Marco