search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

Difference between Signed and Unsigned in MySQL

schedule Aug 12, 2023
Last updated
local_offer
MySQL
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!