博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sql Server系列:数据表操作
阅读量:6230 次
发布时间:2019-06-21

本文共 5651 字,大约阅读时间需要 18 分钟。

  表是用来存储数据和操作数据的逻辑结构,用来组织和存储数据,关系数据库中的所有数据都表现为表的形式,数据表由行和列组成。SQL Server中的数据表分为临时表和永久表,临时表存储在tempdb系统数据库中,当不再使用或退出SQL Server时,临时表会自动删除;永久表一旦创建之后,除非用户删除,否则将一直存在数据库文件中。

  创建数据表的两种方法:(1) 通过对象资源管理器创建;(2) 通过Transact-SQL语句进行创建

1 Transact-SQL创建表

1.1 语法

CREATE TABLE     [ database_name . [ schema_name ] . | schema_name . ] table_name         ( { 
|
|
} [
] [ ,...n ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ { TEXTIMAGE_ON { filegroup | "default" } ] [ FILESTREAM_ON { partition_scheme_name | filegroup | "default" } ] [ WITH (
[ ,...n ] ) ][ ; ]
::=column_name
[ FILESTREAM ] [ COLLATE collation_name ] [ NULL | NOT NULL ] [ [ CONSTRAINT constraint_name ] DEFAULT constant_expression ] | [ IDENTITY [ ( seed ,increment ) ] [ NOT FOR REPLICATION ] ] [ ROWGUIDCOL ] [
[ ...n ] ] [ SPARSE ]
::= [ type_schema_name . ] type_name [ ( precision [ , scale ] | max | [ { CONTENT | DOCUMENT } ] xml_schema_collection ) ]
::= [ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH ( < index_option > [ , ...n ] ) ] [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] | [ FOREIGN KEY ] REFERENCES [ schema_name . ] referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) }
::=column_name AS computed_column_expression [ PERSISTED [ NOT NULL ] ][ [ CONSTRAINT constraint_name ] { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH (
[ , ...n ] ) ] | [ FOREIGN KEY ] REFERENCES referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE } ] [ ON UPDATE { NO ACTION } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ]]
::=column_set_name XML COLUMN_SET FOR ALL_SPARSE_COLUMNS< table_constraint > ::=[ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] (column [ ASC | DESC ] [ ,...n ] ) [ WITH FILLFACTOR = fillfactor |WITH (
[ , ...n ] ) ] [ ON { partition_scheme_name (partition_column_name) | filegroup | "default" } ] | FOREIGN KEY ( column [ ,...n ] ) REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) }
::={ DATA_COMPRESSION = { NONE | ROW | PAGE } [ ON PARTITIONS ( {
|
} [ , ...n ] ) ]}
::={ PAD_INDEX = { ON | OFF } | FILLFACTOR = fillfactor | IGNORE_DUP_KEY = { ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF} | ALLOW_PAGE_LOCKS ={ ON | OFF} | DATA_COMPRESSION = { NONE | ROW | PAGE } [ ON PARTITIONS ( {
|
} [ , ...n ] ) ]}
::=
TO

2 Transact-SQL修改表

2.1 语法

ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name {     ALTER COLUMN column_name     {         [ type_schema_name. ] type_name             [ (                 {                    precision [ , scale ]                  | max                  | xml_schema_collection                 }             ) ]         [ COLLATE collation_name ]         [ NULL | NOT NULL ] [ SPARSE ]      | {
ADD | DROP } { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION | SPARSE } } | [ WITH { CHECK | NOCHECK } ] | ADD {
|
|
|
} [ ,...n ] | DROP { [ CONSTRAINT ] { constraint_name [ WITH (
[ ,...n ] ) ] } [ ,...n ] | COLUMN { column_name } [ ,...n ] } [ ,...n ] | [ WITH { CHECK | NOCHECK } ] { CHECK | NOCHECK } CONSTRAINT { ALL | constraint_name [ ,...n ] } | { ENABLE | DISABLE } TRIGGER { ALL | trigger_name [ ,...n ] } | { ENABLE | DISABLE } CHANGE_TRACKING [ WITH ( TRACK_COLUMNS_UPDATED = { ON | OFF } ) ] | SWITCH [ PARTITION source_partition_number_expression ] TO target_table [ PARTITION target_partition_number_expression ] [ WITH (
) ] | SET ( FILESTREAM_ON = { partition_scheme_name | filegroup | "default" | "NULL" } ) | REBUILD [ [PARTITION = ALL] [ WITH (
[ ,...n ] ) ] | [ PARTITION = partition_number [ WITH (
[ ,...n ] ) ] ] ] |
|
}[ ; ]

2.2 示例

  修改表添加新列

ALTER TABLE [dbo].[Product] ADD [UnitPrice] DECIMAL(18,2) NULL
ALTER TABLE [dbo].[Product] ADD [UnitsInStock] INT NULL, [UnitsOnSale] INT NULL

  修改列

ALTER TABLE [dbo].[Product] ALTER COLUMN [ProductName] VARCHAR(100) NOT NULL

  删除列

ALTER TABLE [dbo].[Product] DROP COLUMN [UnitPrice]

3 Transact-SQL删除表

3.1 语法

DROP TABLE [ database_name . [ schema_name ] . | schema_name . ]table_name [ ,...n ] [ ; ]

4 表操作示例

  查看数据库中全部的表

USE [Portal]GOSELECT * FROM INFORMATION_SCHEMA.TABLESGO

  INFORMATION_SCHEMA是一个特定的访问路径,用于显示系统数据库及其内容的相关元数据。如INFORMATION_SCHEMA.VIEWS、INFORMATION_SCHEMA.SCHEMATA。

  查看表的属性

  存储过程sp_help的功能是查看任意数据库对象、用户自定义数据类型或SQL Server数据类型的信息。

  执行存储过程sp_help的语法结构:

EXEC sp_help 

  查看Product表的属性:

EXEC sp_help Product

转载于:https://www.cnblogs.com/libingql/p/4087155.html

你可能感兴趣的文章
ASA防火墙的基本配置
查看>>
[ 1011 ] 有关文件的操作及图说明
查看>>
PXE网络装机
查看>>
人工智能赢了人类,那又如何?
查看>>
今天客户遇到的一个问题:Linux系统的主机碰上了ARP
查看>>
[Thinking In Java]代码整理之移位操作符(shift operators)
查看>>
windows 8预测版本(x64,x86)下载
查看>>
Android Studio 3.0 之后打包apk出现应用未安装问题
查看>>
我的友情链接
查看>>
为你的AliOS Things应用增加自定义cli命令
查看>>
我的友情链接
查看>>
Ez×××客户端在服务器侧没有配置隧道分离的情况下如何直接上公网
查看>>
如何备份cisco路由器配置文件
查看>>
部署Symantec Antivirus 10.0网络防毒服务器之六
查看>>
《paste命令》-linux命令五分钟系列之二十
查看>>
CTO职场解惑指南系列(一)
查看>>
安排!活动素材的亿级用户精准投放
查看>>
debian8.4下配置pgpool+pg9.5双主备
查看>>
用scrapy爬取ttlsa博文相关数据存储至mysql
查看>>
我的友情链接
查看>>