首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

外键怎么设置

2012-03-22 
外键如何设置我建两个表createtableCity(CityIDintidentity(1,1)primarykeynotnull,CityNamevarchar(20)no

外键如何设置
我建两个表
create   table   City
(  
CityID                     int   identity(1,1)   primary   key   not   null,
CityName                 varchar(20)   not   null,
DistrictCode         varchar(4)   not   null,
)
create   table   Weather
(
WeatherId           int   identity(1,1)   primary   key   not   null,
WeatherDate       dateTime   not   null,
CityID                 int     foreign   key   not   null,/*外键如何设置*/
Weather               varchar(256)   not   null,
)

[解决办法]
CityID int foreign key references city(cityid) not null,/*外键如何设置*/
[解决办法]

create table City
(
CityID int identity(1,1) primary key not null,
CityName varchar(20) not null,
DistrictCode varchar(4) not null,
)
create table Weather
(
WeatherId int identity(1,1) primary key not null,
WeatherDate dateTime not null,
CityID int foreign key not null,/*外键如何设置*/
Weather varchar(256) not null,
CONSTRAINT [FK_WEATHER_REFERENCE_CityID] FOREIGN KEY
(
[CityID]
) REFERENCES [City] (
[CityID ]
)
)

热点排行