Data Types in SQL Server
|
Data Type |
Type |
Description |
|
Bit |
Integer |
The BIT data type is an integer, and its value can only be 0, 1, or null. This data type is used to store data with only two possible values, such as yes, no, true, or fa lse, on, or off. |
|
Int |
Integer |
Int data type can store integers from-231 (-2147483648) to 231 (2147483 647. This type can be used for almost all numeric data stored in the database. This data type occupies 4 bytes in the database. |
|
Smallint |
Integer |
The smallint data type can store integers from-215 (-32768) to 215 (32767. This type of data is useful for storing numeric data that is often limited to a specific range. This data type occupies 2 bytes of space in the database. |
|
Tinyint |
Integer |
The tinyint data type can store integers ranging from 0 to 255. It is useful when you only plan to store a limited number of values. This data type occupies 1 byte in the database |
|
Numeric |
Exact numeric type |
Numeric has the same data type as decimal. |
|
Decimal |
Exact numeric type |
The decimal data type can be used to store numeric data with fixed precision and range from-1038-1 to 1038-1. When using this data type, you must specify the range and precision. The range is the total number of digits that can be stored around the decimal point. Precision is the number of digits stored on the right of the decimal point. |
|
Money |
Currency type |
The money data type is used to indicate money and currency values. This data type can store data from-922 billion to 922 billion, accurate to one thousandth of the currency unit |
|
Smallmoney |
Currency type |
The smallmoney data type is used to indicate money and currency values. This data type can store data from-214748.3648 to 214748.3647, accurate to one thousandth of the currency unit |
|
Float |
Approximate numeric type |
Float data is an approximate value type for floating point numbers. Floating Point Numbers are similar because not all numbers in the range can be accurately expressed. Floating Point number can be any number from-1.79e + 308 to 1.79e + 308 |
|
Real |
Approximate numeric type |
The real data type is similar to a floating point number and is an approximate value type. It indicates the floating point number between-3.40e + 38 and 3.40e + 38. |
|
Datetime |
Datetime type |
The datetime data type is used to represent the date and time. This data type stores all the date and time data from January 1, 1/300 to 3.33 seconds or milliseconds. |
|
Smalldatetime |
Datetime type |
The smalldatetime data type is used to indicate the date and time from January 1, January 1-20, 1900 to January 1, June 6, accurate to one minute. |
|
Cursor |
Special Data Type |
The cursor data type is a special data type that contains a reference to the cursor. This type of data is used in the stored procedure and cannot be used when creating a table. |
|
Timestamp |
Special Data Type |
The timestamp data type is a special data type used to create a unique digital within the database range. A table can have only one timestamp column. The value of the timestamp column changes each time a row is inserted or modified. Although its name contains "time", the timestamp column is not a date that people can recognize. In a database, the timestamp value is unique. |
|
Uniqueidentifier |
Special Data Type |
The uniqueidentifier data type is used to store a globally unique identifier (guid. GUID is indeed globally unique. This number has almost no chance of being rebuilt in another system. You can use the newid function or convert a string to a unique identifier to initialize a column with a unique identifier. |
|
Char |
Character Type |
The Char data type is used to store non-uniform encoded data with a fixed length. When defining a column of this type, you must specify the column length. This data type is useful when you always know the length of the data to be stored. For example, when you store data in zip code and 4 character format, you know that it always takes 10 characters. The column width of this data type is up to 8000 characters |
|
Varchar |
Character Type |
The varchar data type, same as the char type, is used to store non-uniform encoding character data. Unlike char, the data type is variable. When defining a column as the data type, you must specify the maximum length of the column. The biggest difference between it and char data types is that the storage length is not the column length, but the data length. |
|
Text |
Character Type |
The text data type is used to store a large amount of non-uniform encoding character data. This data type can contain a maximum of 2 billion to characters |
|
Nchar |
Unified encoding type |
The nchar data type is used to store fixed-length, unified encoding data. Unified encoding uses a dual-byte structure to store each character, rather than a single byte (in common text ). It allows a large number of extended characters. This data type can store 4000 characters, doubling the byte space used |
|
Nvarchar |
Unified encoding type |
The nvarchar data type is used as a variable-length uniform encoding type data. This data type can store 4000 characters, and the byte space is doubled. |
|
Ntext |
Unified encoding type |
The ntext data type is used to store a large amount of uniform encoding data. This data type can store 230-1 or nearly 1 billion characters, and the byte space is doubled. |
|
Binary |
Binary data type |
The binary data type is used to store binary data that can be up to 8000 bytes in length. When the content of the input table is close to the same length, you should use this data type. |
|
Varbinary |
Binary data type |
The varbinary data type is used to store up to 8000 bytes of long binary data. This type of data should be used when the content size of the input table is variable. |
|
Image |
Binary data type |
The image data type is used to store variable-length binary data up to 2 billion-1-1 or approximately bytes. |
1. Data Definition Language (DDL) · Create a table
Create Table biaoming
(
Pub_id char (4) not null,
Pub_name varchar (40) null,
Country varchar (30) null default ('usa ')
...
)
· Modify a table
Modify Table Name: exec sp_rename 'original table name', 'destination table name'
Add field: alter table table name add field name char (50) null
Modify Field: alter table table name alter column field name char (50) null
Delete field: alter table Table Name drop column field name
· Delete a table
Drop Table Name
· Create a view
Create view name as Select column name from table name where Condition
· Modify View
Alter view name as Select column name from table name where Condition
· Delete a view
Drop view name
2. Data Control Language (DML)-query statement (select)
Select select_list from table_source
Where search_condition
Group by group_by_expression
Having search_condition
Order by order_expression ASC | DESC
Select distinct pub_id from table_name -- delete duplicate rows where price between $10 and $30 -- range operator... and... where country in ('Germany ', 'France ') -- list operator inwhere price is null -- null value identifier is nullwhere pub_name like '% Hing' -- percent % matching character of any type length where pub_name like '[A-F]' -- square brackets [] specifies a character, string, or range where pub_name like '[^ A-F] %' -- [^] The matched object is any character other than the specified character where pub_name like 'gg _ '-- match characters starting with GG and with a length of 5
Note:A group by clause cannot use a set function.
All non-set items in the select list must be listed in the group by clause.
· Insert)
Insert into Table Name (column name, column name,...) values ('','',...) // insert record
Insert into Table Name (column name,...) select list from table name where condition // copy data
· Update)
Update table name set column name = 'Update value ',...
· Delete)
Delete from table name where Condition
3. Data Control Language (DCL)-grant statement-grant permissions
Grant permission 1, permission 2... on table name to Username
Example:
// Grant select and insert permissions to user1
Grant select, insert on employee_tab to user1;
· Revoke statement -- delete permission
Revoke permission 1, permission 2 on table name from username