T-SQL笔记4:表

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: T-SQL笔记4:表 本章摘要 1:CREATE 2:SQL Server 2005 Data Types 3:Adding aColumn to an Existing Table 4:Changing a Column Definition 5:Creating aComputed ...

T-SQL笔记4:表

本章摘要

1:CREATE

2:SQL Server 2005 Data Types

3:Adding aColumn to an Existing Table

4:Changing a Column Definition

5:Creating aComputed Column

6:Dropping aTable Column

1:CREATE

The simplified syntax is as follows:

CREATE TABLE 
[ database_name . [ schema_name ] . | schema_name . ] table_name 
( column_name <data_type> [ NULL | NOT NULL ] [ ,...n ]  )

2:SQL Server 2005 Data Types

bigint: Whole number from –2^63 (-9,223,372,036,854,775,808) through 2^63-1(9,223,372,036,854,775,807).
binary: Fixed-length binary data with amaximum of 8000 bytes.

bit: Whole number either 0 or 1.
char: Fixed-length character data with maximum length of 8000 characters.
datetime: Date and time from January 1, 1753, through December 31, 9999. (1753 was the year following the adoption of the Gregorian calendar, which producedadifference in days to the previous calendar of 12 days. Beginning with the year 1753 sidesteps all sorts of calculation problems.)

Decimal or numeric: (no difference between the two) range from –10^38 +1 through 10^38-1.Decimal uses precision and scale. Precision determines maximum totalnumber of decimal digits both left and right of the decimal point. Scaledetermines maximum decimal digits to the right of the decimal point.
float: Floating precision number from - 1.79E + 38 to -2.23E - 38, 0 and 2.23E –38 to 1.79E + 38.
image: Variable-length binary data from 0 through 2^31 –1. This data type will be removed in afuture version of SQL Server. Instead of using this data type, use varbinary(max)instead.
int: Whole number from –2^31 (-2,147,483,648) through 2^31-1 (2,147,483,647).
money: Monetary value between –2^63 ( -922,377,203,685,477.5808) through 2^63-1 (+922,337,203,685,477.5807).
nchar: Fixed-length Unicode character data with amaximum length of 4000 characters.
ntext: Variable-length Unicode character data with amaximum length of 1,073,741,823 characters. This data type will be removed in afuture version of SQL Server. Instead of using this data type, use nvarchar(max)instead.
nvarchar: Variable-length Unicode character data with maximum length of 4000 characters. SQL Server 2005 has also added a“max” option which allows you to store up to 2^31-1bytes. This new option allows you to use the regular data types instead of SQL Server 2000’s text, ntext, and image.
real: Floating precision number from -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38.
smalldatetime: Date and time from January 1, 1900, through June 6, 2079.
smallint: Whole number from –32,768 through 32,767.
smallmoney: Monetary value between –214,748.3648 through +214,748.3647.
sql_variant: A data type which can store all data types except text, ntext, timestamp, varchar(max), nvarchar(max), varbinary(max), xml, image, user-defined types, and another sql_variant.

table: The tabledata type can’t be used in CREATE TABLEas acolumn type. Instead it is used for table variables or for storage of rows for atable-valued function.
text: Variable-length data with maximum length of 2,147,483,647 characters. This data type will be removed in afuture version of SQL Server. Instead of using this data type, use varchar(max)instead.
timestamp:
Database-wide unique number that is updated when arow is modified.

tinyint: Whole number from 0 through 255.
uniqueidentifier: Stores a16-byte GUID (globally-unique identifier).
varbinary: Variable-length data with amaximum of 8000 bytes. SQL Server 2005 has also added amax value, which allows you to store up to 2^31-1bytes. This new option allows you to use the regular data types instead of SQL Server 2000’s text, ntext, and image.
varchar: Variable-length character data with amaximum length of 8,000 characters.SQL Server 2005 has also added amax value, which allows you to store up to 2^31-1bytes. This new option allows you to use the regular data types
instead of SQL Server 2000’s text, ntext, and image.
xml: New to SQL Server 2005, this data type stores native xml data.

 

tip: If your character data type columns use the same or a similar number of characters consis-
tently, use fixed length data types (char, nchar).

 

3:Adding aColumn to an Existing Table

Sugest we alread have a table, such as :

CREATE TABLE Person.EducationType
(EducationTypeID int NOT NULL,
EducationTypeNM varchar(40) NOT NULL)
GO

 

And, After atable is created, you can modify it using the ALTER TABLEcommand.

ALTER TABLE table_name
ADD { column_name data_type } NULL

 

For example:

ALTER TABLE HumanResources.Employee 
ADD Latest_EducationTypeID int NULL

 

When adding columns to atable that already has data in it, you will be required to add the column with NULLvalues allowed. You can’t specify that the column be NOT NULL, because you must first add the column to the table before you can put avalue in that column for existing rows.

 

4:Changing a Column Definition

The syntax for doing this is as follows:

ALTER TABLE table_name 
ALTER COLUMN column_name 
[type_name] [NULL | NOT NULL] [COLLATE collation_name] 
 

example:

This example demonstrates how to change an existing table column’s nullability and data type.The Gender column in the HumanResources.Employeetable is originally NOT NULL and the original data type of the LoginIDcolumn is nvarchar(256):
-- Make it Nullable
ALTER TABLE HumanResources.Employee
ALTER COLUMN Gender nchar(1) NULL
-- Expanded nvarchar(256) to nvarchar(300)  
ALTER TABLE HumanResources.Employee
ALTER COLUMN LoginID nvarchar(300) NOT NULL

 

5:Creating aComputed Column

The syntax for adding acomputed column either by CREATEor ALTER TABLEis as follows:

column_name AS computed_column_expression [ PERSISTED ]


The column_name is the name of the new column. The computed_column_expression is the calculation you wish to be performed in order to derive the column’s value. Adding the PERSISTED keyword actually causes the results of the calculation to be physically stored.

In this example, anew, calculated column is added to an existing table:

ALTER TABLE Production.TransactionHistory
ADD CostPerUnit AS (ActualCost/Quantity) 

 

The next example creates aPERSISTEDcalculated column, which means the calculated data will actually be physically stored in the database (but still automatically calculated by SQL Server):

CREATE TABLE HumanResources.CompanyStatistic
(CompanyID int NOT NULL,
StockTicker char(4) NOT NULL,
SharesOutstanding int NOT NULL,
Shareholders int NOT NULL,
AvgSharesPerShareholder AS (SharesOutStanding/Shareholders) PERSISTED)

 

6:Dropping aTable Column

You can use ALTER TABLEto drop acolumn from an existing table.
The syntax for doing so is as follows:

ALTER TABLE table_name
DROP COLUMN column_name
NET C# 入门级 .NET C# 专业级 .NET 架构级 BS系统专业级 BS系统安全
1.开篇及C#程序、解决方案的结构
2.源码管理之TFS入门
3.打老鼠初级
……
21.CMS之主要功能实现
22.进程和线程基础
23.类型转换
24.算法基础
25.初级课程之剩余知识点
1.消灭打老鼠游戏中的自定义委托
2.垃圾回收
3.Dispose模式
……
16.异常使用指导
17.最常用的重构指导
18.Debug和IDE的进阶
19.Resharper的使用
20.ILSPY的使用
1.Socket入门
2.打造打老鼠游戏网络版
3.WCF入门
……
10.依赖注入
11.万物兼可测试
12.软件指标之覆盖率计算
13.软件指标之代码行
14.软件指标之圈复杂度、嵌套深度
1.HTML
2.WebForm原理
3.CSS必知必会
……
19.让浏览器缓存Shop
20.Asp.net的生命周期
21.Asp.net网站的发布以及调试晋级
22.BS程序的本质
23.压力测试我们的Shop
1.Fiddler必知必会
2.IE开发者工具必知必会
3.跨站脚本防范
4.权限欺骗防范
5.参数越界防范
6.会话劫持防范
7.CSRF防范
8.盗链防范
9.静态文件的保护


 
 
Creative Commons License本文基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
2月前
|
SQL 关系型数据库 MySQL
《SQL必知必会》个人笔记(一)
《SQL必知必会》个人笔记(一)
42 0
|
7月前
|
SQL 数据库 索引
阿里P8整理出SQL笔记:收获不止SOL优化抓住SQL的本质
开头我先说: 有人就有江湖,有江湖就有IT系统,有IT系统就有数据库,有数据库就有SQL,SQL应用可一字概括:“"广"。加之其简单易学,SQL实现也可一字概括:“乐”。
|
5月前
|
SQL 分布式计算 HIVE
pyspark笔记(RDD,DataFrame和Spark SQL)1
pyspark笔记(RDD,DataFrame和Spark SQL)
55 1
|
16天前
|
SQL 存储 关系型数据库
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-2
【4月更文挑战第5天】两阶段提交是为确保`redo log`和`binlog`逻辑一致,避免数据不一致。若先写`redo log`, crash后数据可能丢失,导致恢复后状态错误;若先写`binlog`,crash则可能导致重复事务,影响数据库一致性。一天一备相较于一周一备,能缩短“最长恢复时间”,但需权衡额外的存储成本。
16 1
|
2月前
|
SQL 存储 数据库
【数据库SQL server】自学终极笔记
【数据库SQL server】自学终极笔记
95 0
|
2月前
|
SQL 存储 安全
《SQL必知必会》个人笔记(四)
《SQL必知必会》个人笔记(四)
32 0
|
2月前
|
SQL 关系型数据库 数据库
《SQL必知必会》个人笔记(三)
《SQL必知必会》个人笔记(三)
28 0
|
2月前
|
SQL 存储 Oracle
《SQL必知必会》个人笔记(二)
《SQL必知必会》个人笔记(二)
30 0
|
3月前
|
SQL 前端开发 Java
SQL注入天书笔记(1)布尔盲注
SQL注入天书笔记(1)布尔盲注
41 0
|
4月前
|
SQL 关系型数据库 MySQL
《SQL必知必会》个人笔记(一)
《SQL必知必会》个人笔记(一)
33 1
《SQL必知必会》个人笔记(一)