当前位置: 代码迷 >> Sql Server >> 外键怎么设置
  详细解决方案

外键怎么设置

热度:19   发布时间:2016-04-27 20:46:33.0
外键如何设置
我建两个表
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 ]
)
)
  相关解决方案