Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Limit min and max value in database

Tags:

sql

sql-server

CREATE TABLE TBL_CD(
CDnr                int identity(1,1),
CDTitel             nvarchar(80) NOT NULL,
CDduur              int,
CDprijs             smallmoney,

So I am creating this table, is there any way I can limit the value of CDprijs to be between 0 and 100?

like image 790
Strah Behry Avatar asked Apr 08 '15 10:04

Strah Behry


People also ask

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.

How do I limit a value in SQL?

The SQL LIMIT clause restricts how many rows are returned from a query. The syntax for the LIMIT clause is: SELECT * FROM table LIMIT X;. X represents how many records you want to retrieve. For example, you can use the LIMIT clause to retrieve the top five players on a leaderboard.

Can we use max and min together in SQL?

You can use both the MIN and MAX functions in one SELECT . If you use only these functions without any columns, you don't need a GROUP BY clause.

What is MAX () and MIN ()?

Python's built-in min() and max() functions come in handy when you need to find the smallest and largest values in an iterable or in a series of regular arguments.


1 Answers

Add a check constraint:

CREATE TABLE TBL_CD(
CDnr                int identity(1,1),
CDTitel             nvarchar(80) NOT NULL,
CDduur              int,
CDprijs             smallmoney,
check (CDprijs between 0 and 100),
like image 53
jarlh Avatar answered Sep 19 '22 17:09

jarlh