读书人

外键怎么设置

发布时间: 2012-03-22 17:43:57 作者: rapoo

外键如何设置
我建两个表
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 ]
)
)

读书人网 >SQL Server

热点推荐