T-SQL笔记4:表

简介: 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 关系型数据库 MySQL
《SQL必知必会》个人笔记(一)
《SQL必知必会》个人笔记(一)
246 0
|
SQL 关系型数据库 MySQL
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-1
【4月更文挑战第4天】SQL更新语句执行涉及查询和日志模块,主要为`redo log`和`binlog`。`redo log`先写日志再写磁盘,保证`crash-safe`;`binlog`记录逻辑日志,支持所有引擎,且追加写入。执行过程分为执行器查找数据、更新内存和`redo log`(prepare状态)、写入`binlog`、提交事务(`redo log`转commit)。两阶段提交确保日志逻辑一致,支持数据库恢复至任意时间点。
149 0
|
10月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
SQL 存储 关系型数据库
SQL自学笔记(3):SQL里的DCL,DQL都代表什么?
本文介绍了SQL的基础语言类型(DDL、DML、DCL、DQL),并详细说明了如何创建用户和表格,最后推荐了几款适合初学者的免费SQL实践平台。
686 3
SQL自学笔记(3):SQL里的DCL,DQL都代表什么?
|
SQL 数据挖掘 数据库
SQL自学笔记(2):如何用SQL做简单的检索
本文深入介绍了SQL的基本语法,包括数据查询、过滤、排序、分组及表连接等操作,并通过实际案例展示了SQL在用户研究中的应用,如用户行为分析、用户细分、用户留存分析及满意度调查数据分析。
221 0
SQL自学笔记(2):如何用SQL做简单的检索
|
SQL 数据挖掘 关系型数据库
SQL自学笔记(1):什么是SQL?有什么用?
本文为用户研究新手介绍SQL(结构化查询语言),解释了SQL的基本概念、入门方法及在用户研究中的应用通过实际案例说明,如用户行为分析、用户细分和满意度调查数据分析,展示了SQL在用户研究中的重要作用。
3966 0
SQL自学笔记(1):什么是SQL?有什么用?
|
SQL 关系型数据库 MySQL
【MySQL系列笔记】常用SQL
常用SQL分为三种类型,分别为DDL,DML和DQL;这三种类型的SQL语句分别用于管理数据库结构、操作数据、以及查询数据,是数据库操作中最常用的语句类型。 在后面学习的多表联查中,SQL是分析业务后业务后能否实现的基础,以及后面如何书写动态SQL,以及完成级联查询的关键。
372 6
|
SQL 存储 关系型数据库
【MySQL系列笔记】SQL优化
SQL优化是通过调整数据库查询、索引、表结构和配置参数等方式,提高SQL查询性能和效率的过程。它旨在减少查询执行时间、减少系统资源消耗,从而提升数据库系统整体性能。优化方法包括索引优化、查询重写、表分区、适当选择和调整数据库引擎等。
414 3
|
SQL 存储 Oracle
《SQL必知必会》个人笔记
《SQL必知必会》个人笔记
176 1
|
SQL 存储 关系型数据库
技术笔记:MYSQL常用基本SQL语句总结
技术笔记:MYSQL常用基本SQL语句总结
131 0