Integers The Bit Type In the computer industry, a bit is a value that can be either 0 or 1. To support the bit as a data type, MySQL provides a data type named BIT. Here is an example of applying it to a column: CREATE TABLE MetroStations
(
ParkingAvailable BIT
);
Tiny Integers If the values of a column will be small natural numbers that range from 0 to 255 (1 byte), set its data type as TINYINT or tinyint. Here are examples: CREATE TABLE GradesScales ( MinPercent TINYINT, MaxPercent TINYINT ); If you want to indicate that the values of the column must always be positive, set the data type as TINYINT UNSIGNED. Here are examples: <!DOCTYPE html> <html> <head> <title>Students Grades - Scales</title> </head> <body> <h2>Students Grades - Scales</h2> <?php $link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise"); mysqli_connect_errno(); $link->query("CREATE TABLE GradesScales1 ( LetterGrade CHAR(2), MinPercent TINYINT UNSIGNED, MaxPercent TINYINT UNSIGNED, Descriptor VARCHAR(25) );"); $link->close(); ?> </body> </html> Small Integers If a column needs small integers in the range of -32,768 to 32,767 (2 bytes), set its data type as SMALLINT or smallint. To indicate that the values of the column must always be positive, add UNSIGNED to the data type. Medium Integers If you want a column to carry signed numbers between -8388608 and 8388607 or unsigned numbers between 0 and 16777215 (3 bytes), set its data type as MEDIUMINT. To indicate that the values of the column must always be positive, add UNSIGNED to the data type. Integers If the values of a column would range from -2,147,483,648 to 2,147,483,647, (4 bytes) set its data type to INT or INTEGER (or int or integer. To indicate that the values of the column must always be positive, add UNSIGNED. Long Integers If you want a column that can hold large numbers (8 bytes) between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807, set its type as BIGINT or bigint The binary type is used for a column that would hold natural numbers in series of bits. Each bit is treated, checked or used individually. When creating a column for such values, apply the BIT data type that uses parentheses. In the parentheses, type the number of bits necessary to store the value. The number is between 1 and 64. Decimal Numbers Floating-Point Decimal Numbers A floating-point decimal number is a number with a fractional part. MySQL supports various types of decimal numbers. Some of the decimal types use an approximate precision for their values. These data types are float, real, and double. Here is an example that uses the float type: CREATE TABLE GradesScales ( MinRange float, MaxRange float ); If you want the numbers to always be positive, add the UNSIGNED keyword after the parentheses of the data type. Here are examples: <!DOCTYPE html> <html> <head> <title>Students Grades - Scales</title> </head> <body> <h2>Students Grades - Scales</h2> <?php $link = mysqli_connect("localhost", "username", "P@s$w0rd1", "DBExercise"); mysqli_connect_errno(); $link->query("CREATE TABLE GradesScales24 ( LetterGrade CHAR(2), MinRange float UNSIGNED, MaxRange float UNSIGNED, MinPercent TINYINT UNSIGNED, MaxPercent TINYINT UNSIGNED, Descriptor VARCHAR(25) );"); $link->close(); ?> </body> </html> Fixed-Point Decimal Numbers To let you create a column for values that use a fixed precision, MySQL provides data types as NUMERIC (or numeric) or DECIMAL (or decimal). Here is an example: CREATE TABLE Distances ( FromCity varchar(40), ToCity varchar(40), Distance decimal ); This also means that you can use the decimal types where you would use an integer. Normally, the NUMERIC (or numeric) or DECIMAL (or decimal) type is suitable for a column that will store monetary values. If you want to specify the amount of precision for the values, add a set of parentheses to the type. In the parentheses, add the precision and the scale as natural numbers separated by a comma. The column can have positive or negative values between -922,337,203,685,477.5808 and +922,337,203,685,477.5807. If you want the numbers to always be positive, add the UNSIGNED keyword after the parentheses of the data type. MySQL supports coordinates of a geometric figure. It does this through a data type named GEOMETRY or geometry. Geographical Location-Based Columns MySQL supports geographical locations using a data type named GEOGRAPHY. |
|
|
|