About Lesson
DATA TYPES IN SQL
- Numeric Data Types
- Integer: A whole number without a decimal point.
- Decimal: A number with a decimal point.
- Floating-point: A number with a decimal point and a fractional part.
- Character Data Types
- Char: A fixed-length character string.
- Varchar: A variable-length character string.
- Nchar: A fixed-length Unicode character string.
- Nvarchar: A variable-length Unicode character string.
- Date and Time Data Types
- Date: A date without a time.
- Time: A time without a date.
- DateTime: A date and time.
- Smalldatetime: A date and time with a precision of 3.33 milliseconds.
- Datetime2: A date and time with a precision of 7.00 milliseconds.
- Other Data Types
- Binary: A binary string.
- Varbinary: A variable-length binary string.
- Image: A large binary string.
- UniqueIdentifier: A unique identifier.
- Cursor: A pointer to a set of rows.
- Rowversion: A value that changes whenever a row is updated.
- XML: An XML document.
The data type of a column defines what value the column can hold. For example, an integer column can only hold whole numbers, while a varchar column can hold any string of characters.
When you create a table, you must specify the data type for each column. For example, the following statement creates a table with two columns:
CREATE TABLE customers (
id INT,
name VARCHAR(255)
);
The id column is an integer column, and the name column is a varchar column with a maximum length of 255 characters.
You can also change the data type of a column after the table has been created. For example, the following statement changes the data type of the name column to nvarchar:
ALTER TABLE customers
ALTER COLUMN name NVARCHAR(255);