您的位置:首页 > 数据库

SQL Server 2005 建立外键约束(foreign key)

2009-07-27 08:11 495 查看
CREATE TABLE [dbo].[Varrily]
(
[ID] [bigint] NOT NULL ,
[Name] [char] (10) NULL ,
[Address] [char] (480) NULL ,
[Introduction] [ntext] NULL
)
 
CREATE TABLE [dbo].[MyTable]
(
[ID] [bigint] IDENTITY (1, 1) NOT NULL ,
[AuthorID] [bigint] NOT NULL ,
[Title] [char] (40) NULL ,
[Date_of_Created] [datetime] NULL
)
 
被引用的键必须唯一,可以设置为主键或者添加唯一约束。
 
alter table dbo.Varrily add constraint pk_id primary key (ID)
alter table dbo.Varrily add constraint un_id unique (ID)
alter table dbo.MyTable add constraint fk_AuthorID foreign key (AuthorID) references dbo.Varrily([ID])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql server null table date
相关文章推荐