i am creating a table. and i am using some update queries on it.
this table has placeId, noOfGirls, noOfBoys. due to some update queries the values of these two fields noOfGirls and noOfBoys becomes negative.
Is there a way so i can convert these negative values to 0. means any constrains or something else so i can protect to be value negative.
Thank you in advance.
Easiest way is to use an unsigned integer type:
CREATE TABLE foo(bar INTEGER UNSIGNED)
The MySQL "numeric types" documentation has more information on what this means.
Declare the column as unsigned int type.
The unsigned range is 0 to 4294967295, so the value never become negative.
You can alter the schema of your existing table
alter table table_name modify column noOfGirls int(10) unsigned;
You can read it more here
Declare it unsigned zerofill works, negative values will be converted to 0. you will get the following warning though
Warning: #1264 Out of range value for column 'a1' at row 1
other ways: you may create a trigger
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Related
As far as I know, we can change the type of column in MySQL, like
ALTER TABLE t1 MODIFY b INT NOT NULL;
I'm using TDengine, and I have lots of data stored. Now I want to change the type of one column from tinyint to tinyint unsigned. I saw some examples in its document for adding/deleting columns.
Does anyone know how to modify the type information of a column?
to modify the data type of the value column in the test table from tinyint to tinyint unsigned, you can use the following ALTER statement:
ALTER TABLE test MODIFY value tinyint unsigned;
Let me know if it is helps.
I have some table in my database with column with type BIGINT:
The problem is, when I set (by update or insert) value lower that 0 (for example -2) then in DB it is set 0.
Do someone know why?
(I have not any procedures to change this value before insert/update).
Db is MySql
You can read the difference between unsigned bigint and signed bigint.
MySQL Bigint
Unsigned bigint values are : 0 to 18446744073709551615
Whereas signed bigint values are : -9223372036854775808 to 9223372036854775807
So you must use the signed bigint
It looks like what your database is actually using is a BIGINT UNSIGNED, which has a range of 0 to 18446744073709551615. And according to the MySQL Reference Manual:
When an out-of-range value is assigned to an integer column, MySQL stores the value representing the corresponding endpoint of the column data type range.
This means that attempting to store a negative number in any UNSIGNED column will wrap around to 0.
To remedy this, it's best to explicitly set the data type to be BIGINT SIGNED.
I've seen a similar question on stackexchange, but it's answer did not give me the correct results.
For demonstration purposes, I have a simple table PURCHASES with columns PURCHASE_NUM, PURCHASE_DATE, CUSTOMER_ID. I want to enforce a not null constraint on the CUSTOMER_ID table. I tried the following:
ALTER TABLE PURCHASES MODIFY CUSTOMER_ID char NOT NULL;
That syntax is fine, but then I insert with the following: INSERT INTO PURCHASES VALUES (333, NULL, NULL); and the tuple is added without issue. Why is the constraint not being enforced? Would having NULL values already in that column before adding the constraint affect things?
Thanks
edit DESCRIBE PURCHASES; says the following for the column of interest:
Field, Type, Null, Key, Default, Extra
CUSTOMER_ID, char(5), YES, , NULL,
Your ALTER command didn't work, the Null column still says YES. Your ALTER command syntax looks just fine, it should have worked. Check your typing and try again.
Is your customer ID really just a char?
Maybe you have to change to
ALTER TABLE PURCHASES MODIFY CUSTOMER_ID char NOT NULL;
According to the manual, data entry into a NOT NULL column that has no explicit DEFAULT clause will set the column to NULL. Thus, you should ALTER your column to contain a DEFAULT. From 4.0 documentation:
Implicit defaults are defined as follows:
For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence.
For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the type. For the first TIMESTAMP column in a table, the default value is the current date and time. See Section 10.3, “Date and Time Types”.
For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
How can I limit my database column's integral input to a specific number of digits ?
CREATE TABLE tab (id INT <1 digit> not null, value INT <10 digits> not null);
Thank you
Add a check constraint (SQL Server) or trigger (MySQL, doesn't support CHECK constraints)
SQL Server example:
CREATE TABLE tab (
id tinynot null CHECK (id BETWEEN 0 AND 9),
value INT not null CHECK (id BETWEEN 1000000000 AND 9999999999)
);
If you only want one digit though, then use tinyint
If you aren't storing numbers (eg "123456789 bananas") but, say, phone numbers then use a varchar type. See https://stackoverflow.com/a/8284674/27535
Edit, you'd need a trigger in MySQL
The short version is using TINYINT UNSIGNED NOT NULL will be a more suitable data type, but it can't limit the values stored.
The longer version is that you may wish to read up on MySQL integer data types. You'll see that TINYINT is sufficient for your purpose as that is a 1-byte column that stores values from -128 to +127, or 0 to +255.
Secondly if you define it as TINYINT(1) then you are defining it as being a TINYINT with a display width of 1 digit. This will not prevent values larger than 10 being stored though. For more reading on this behaviour check numeric type attributes.
When I try to insert the below into my MySQL
INSERT INTO `rooms` (`id`, `ip`) VALUES ('131213', '-259857341');
I fails with the follow error:
Warning: #1264 Out of range value for column 'ip' at row 1
I am looking around but haven't found how to fix or work it out...
My field is unsigned int which should work just fine for that entry.
What is the problem and how do I solve ?
I am using unsigned int because I wanted to store ips using inet_ntoa/aton.
EDIT:
I am using unsigned INT as recommend in MySQL website:
To store values generated by
INET_ATON(), use an INT UNSIGNED
column rather than INT, which is
signed. If you use a signed column,
values corresponding to IP addresses
for which the first octet is greater
than 127 cannot be stored correctly.
See Section 10.6, “Out-of-Range and
Overflow Handling”.
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html
A negative number is out of range for an UNSIGNED INT. Read the documentation for a full list of allowed values for a given data type.
Unsigned integer means non-negative value at least.
Not sure if this is what you need but you can try to convert signed integer to 4 bytes unsigned integer as your ipconverter does (http://www.silisoftware.com/tools/ipconverter.php):
INSERT INTO `rooms` (`id`, `ip`) VALUES ('131213', '-259857341' & 0xffffffff);
Firstly, by definition, you cannot insert a negative number into a unsigned int field. Change the field to int instead (or if possible use non-negative numbers).
Secondly i think that you should remove the single-quotes around the inserted number to that the the value is treated as an int and not a string.
Change your INT field into BIGINT and it will work for me smoothly.