0

我在这里不知所措,所以任何帮助将不胜感激。这是我正在运行的三个查询:

CREATE TABLE Contact
(ContactID  VARCHAR(10) NOT NULL,
ContactCompanyName  VARCHAR(80),
ContactFirstName    VARCHAR(30),
ContactLastName VARCHAR(30),
EmployeeID  VARCHAR(15),
CONSTRAINT Contact_PK PRIMARY KEY (ContactID));

GO

CREATE TABLE CompanyRepresentative
(EmployeeID VARCHAR(15) NOT NULL,
RepFirstName    VARCHAR(30),
RepLastName VARCHAR(30),
CONSTRAINT CompanyRepresentative_PK PRIMARY KEY (EmployeeID));

GO

其次是:

ALTER TABLE Contact
ADD CONSTRAINT fk_EmployeeID1
FOREIGN KEY(EmployeeID)REFERENCES CompanyRepresentative(EmployeeID);

GO

最后:

INSERT dbo.Contact (ContactID, ContactCompanyName, ContactFirstName,    

ContactLastName)
VALUES
('PC7432', 'Krueger Bakeries', 'Brian', 'Jones'),
('PC5317', 'Advanced Data Storage', 'Mark', 'Pulley'),
('PC2931', 'M&D Graphics and Design', 'Mike', 'Dorris'),
('PC1753', 'Apex Digital Solutions', 'Janice', 'Becker'),
('PC8527', 'Infensus Consultants', 'Narbeth', 'Pokrehl');

GO

INSERT dbo.CompanyRepresentative (EmployeeID, RepFirstName, RepLastName)
VALUES
('TBOC53297', 'Mike', 'Hartman'),
('TBOC32781', 'Lynn', 'Kynes'),
('TBOC12783', 'Jason', 'McCann'),
('TBOC43971', 'Susan', 'Mueller'),
('TBOC29753', 'Greg', 'Krebs');

GO

当我跑

select * from Contact

EmployeeID 字段不显示表 dbo.CompanyRepresentative 中外键 EmployeeID 的值,而是显示“NULL”。我错过了什么?

4

1 回答 1

0

你必须EmployeeID像这样插入。否则,默认情况下SQL Server会插入NULL该字段。

INSERT dbo.Contact (ContactID, ContactCompanyName, ContactFirstName, ContactLastName, EmployeeID)
VALUES
('PC7432', 'Krueger Bakeries', 'Brian', 'Jones', 'TBOC53297'),
('PC5317', 'Advanced Data Storage', 'Mark', 'Pulley','TBOC32781'),
('PC2931', 'M&D Graphics and Design', 'Mike', 'Dorris','TBOC12783'),
('PC1753', 'Apex Digital Solutions', 'Janice', 'Becker','TBOC43971'),
('PC8527', 'Infensus Consultants', 'Narbeth', 'Pokrehl','TBOC29753');

GO
于 2013-03-07T02:20:44.570 回答