• SQLServer创建数据库表的语法
  • 发布于 2个月前
  • 136 热度
    0 评论

虽然大部分情况下,我们都习惯于用SQL Server Management Studio这种IDE的工具进行数据库表的设计和创建,但是,很多时候,当我们需要开发一些与SQL有关的自动化工具时,还是经常会在程序中直接使用SQL的方式创建数据库表。所以这里记录一下在SQL Server中如何使用SQL语法进行表的建立操作。

SQL建表实例:

CREATE TABLE [dbo].[ArticleInfo](
	[ID] [bigint] IDENTITY(1,1) NOT NULL,
	[Title] [nvarchar](100) NULL,
	[Thumbnail] [varchar](100) NULL,
	[PreviewInfo] [nvarchar](300) NULL,
	[Contents] [nvarchar](max) NULL,
	[UserID] [int] NOT NULL,
	[UserName] [varchar](30) NULL,
	[UserHeadPic] [varchar](100) NULL,
	[AddTime] [smalldatetime] NOT NULL,
	[Clicks] [int] NOT NULL,
	[EnableComment] [bit] NOT NULL,
	[ShowPlace] [int] NOT NULL,
	[LikeNum] [int] NOT NULL,
 CONSTRAINT [PK_ArticleInfo] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
实例解析:

CREATE TABLE 你要创建的表名(
	字段1 [bigint(字段数据类型)] IDENTITY(1,1) NOT NULL(表示字段内容不能为空),
	字段2 [nvarchar(字段数据类型)](100[100表示字段的长度]) NULL(null表示可为空),
	...
	[LikeNum] [int(字段数据类型)] NOT NULL(表示该字段内容不能为空),
 CONSTRAINT [PK_ArticleInfo](创建的约束名称) PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


用户评论