Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:37:41+00:00 2026-06-14T08:37:41+00:00

How can we add a constraint which enforces a column to have only positive

  • 0

How can we add a constraint which enforces a column to have only positive values.

Tried the following mysql statement but it doesn’t work

create table test ( test_column integer CONSTRAINT blah > 0);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:37:43+00:00Added an answer on June 14, 2026 at 8:37 am

    You would use the keyword unsigned to signify that the integer doesn’t allow a “sign” (i.e. – it can only be positive):

    CREATE TABLE test (
        test_column int(11) unsigned
    );
    

    You can read more about the numeric data types (signed & unsigned) here.

    As far as an actual constraint to prevent the insertion-of negative values, MySQL has a CHECK clause that can be used in the CREATE TABLE statement, however, according to the documentation:

    The CHECK clause is parsed but ignored by all storage engines.

    For reference, here is how you would use it (and though it will execute absolutely fine, it just does nothing – as the manual states):

    CREATE TABLE test (
        test_column int(11) unsigned CHECK (test_column > 0)
    );
    

    UPDATE (rejecting negative values completely)
    I’ve noticed from a few of your comments that you want queries with negative values to be completely rejected and not set to 0 (as a normal transaction into an unsigned column would do). There is no constraint that can do this in-general (that I know of, at least), however, if you turn strict-mode on (with STRICT_TRANS_TABLES) any query that inserts a negative value into an unsigned column will fail with an error (along with any-other data-insertion errors, such as an invalid enum value).

    You can test it by running the following command prior to your insert-commands:

    SET @@SESSION.sql_mode = 'STRICT_TRANS_TABLES';
    

    And if it works for you, you can either update your MySQL config with sql-mode="STRICT_TRANS_TABLES" or use SET @@GLOBAL.sql_mode = 'STRICT_TRANS_TABLES'; (I’m not sure if the SET command will affect the global mysql config though, so it may be better to update the actual config-file).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I add a check constraint which makes sure that all values are unique,
I have the following query which is adding contraint. but in order to add,
I can add a Conditional statement to a breakpoint, for instance arg0.startsWith(something) but i'd
I have a table with which I want to add a check constraint after
Im trying to add a constraint to a reservation table, the reservation can either
I can add a normal rightBarButton to my navigation without a problem but now
I wrote library which can add and update objects in salesforce. I use beatbox
I have a mapping like this: HasMany(x => x.Orders).KeyColumn(CustomerID); Which is causing a constraint
I have a non-nullable database column which has a default value set. When inserting
As per https://stackoverflow.com/questions/3264227/relations-with-multiple-keys-in-doctrine-1-2 , I have two tables which (as I can't get it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.