search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

Difference between Signed and Unsigned in MySQL

Database
chevron_right
MySQL
chevron_right
Cookbooks
chevron_right
Data types Cookbook
schedule Jul 1, 2022
Last updated
local_offer MySQL
Tags
tocTable of Contents
expand_more

UNSIGNED: only stores positive numbers (or zero).

SIGNED: can store negative numbers.

Below is a table of the range of values each integer type can store:

Type

Storage (Bytes)

Min Value Signed

Min Value Unsigned

Max Value Signed

Max Value Unsigned

TINYINT

1

-128

0

127

255

SMALLINT

2

-32768

0

32767

65535

MEDIUMINT

3

-8388608

0

8388607

16777215

INT

4

-2147483648

0

2147483647

4294967295

BIGINT

8

- 2^63

0

2^63 - 1

2^64 - 1

Notice that with UNSIGNED, you're essentially giving yourself twice as much space for the integer since you explicitly specify you don't need negative numbers.

NOTE

When deciding whether to use SIGNED or UNSIGNED for a field, ask yourself wether this field will ever contain a negative value. If the answer is no, then you want an UNSIGNED data type. A common use case for UNSIGNED is for auto-increment id fields in a table.

Examples

To specify an UNSIGNED auto-increment id column:

CREATE TABLE info (
id INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(20),
age INT,
hobby VARCHAR(20),
PRIMARY KEY (id)
);
Query OK, 0 rows affected (0.03 sec)

To specify a SIGNED column profit to represent profit on investments:

CREATE TABLE stock (
stock_code INT,
stock_name VARCHAR(20),
profit INT SIGNED,
PRIMARY KEY (stock_code)
);
Query OK, 0 rows affected (0.01 sec)

As the profit column should be able to take negative values to represent losses on investments we have specified the column as SIGNED.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!