创建一对一的关系T-SQL

时间:2015-05-07 17:56:46

标签: sql-server tsql

我有两张表CARCAR_BRAND

CREATE TABLE car
(
    car_id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    car_brand int NOT NULL,
    tech_passport nvarchar(17) NOT NULL,
    vin_code nvarchar(17) NOT NULL,
)

CREATE TABLE car_brand
(
    car_brand_id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    name_brand nvarchar(15),
    country_brand nvarchar(20),
)

如何在这两个表之间建立一对一的关系?一辆车有一个品牌

1 个答案:

答案 0 :(得分:2)

就像这样:

ALTER TABLE dbo.Car
ADD CONSTRAINT FK_Car_CarBrand
  FOREIGN KEY (Car_Brand) REFERENCES dbo.Car_Brand(Car_Brand_id)