我创建了两个数据库表。 SQL Server不允许我将项目插入到我的第二个表中,即使用外键的表。我一直得到的错误是
Msg 207,Level 16,State 1,Line 53 列名称'ExemNonExemStat'无效。
以下是我的代码。请帮忙,这让我发疯了。
----Create Job Title Table
CREATE TABLE [WILLKudlerFineFoods].dbo.tblJobTitle
(
EmpeeJobID char (10) CONSTRAINT PK_EmpeeJobID PRIMARY KEY Not Null,
EmplerInfRep char (200) Not Null,
EmpeeJobTitle char (20) Not Null,
EmpeeJobDesc char (200) Not Null,
ExemNonExemStat char (20) Not Null
)
----Create Employee Table
CREATE TABLE [WILLKudlerFineFoods].dbo.tblEmployee
(
EmpeeID char (6) CONSTRAINT PK_EmpeeJobID PRIMARY KEY Not Null,
EmpeeJobID char (10)
CONSTRAINT FK_EmpeeJobID FOREIGN KEY (EmpeeJobID)
REFERENCES tblJobTitle (EmpeeJobID) Not Null,
EmpeeLName char (25) Not Null,
EmpeeFName char (20) Not Null,
EmpeeAdd char (100) Not Null,
EmpeeCity char (100) Not Null,
EmpeeState char (100) Not Null,
EmpeeTeleAreaCode char (3) Not Null,
EmpeeTeleNum char (7) Not Null,
EmplerInfRep char (200) Not Null,
EmpeeHirDate datetime Not Null,
EmpeeSal numeric (8) Not Null,
EmpeeGend char (6) Not Null,
EmpeeAge char (3) Not Null
)
--Insert record 1 into Job Title table
INSERT INTO [WILLKudlerFineFoods].dbo.tblJobTitle
(
[EmpeeJobID],
[EmplerInfRep],
[EmpeeJobTitle],
[EmpeeJobDesc],
[ExemNonExemStat]
)
VALUES
(
'0000000001','Office/Clerical','Accounting Clerk','Computes, classifies, records, and verifies numerical data for use in maintaining accounting records.','Exempt'
)
--Insert record 1 into Employee table
INSERT INTO [WILLKudlerFineFoods].dbo.tblEmployee
(
[EmpeeID],
[EmpeeJobID],
[EmpeeLName],
[EmpeeFName],
[EmpeeAdd],
[EmpeeCity],
[EmpeeState],
[EmpeeTeleAreaCode],
[EmpeeTeleNum],
[EmplerInfRep],
[EmpeeHirDate datetime],
[EmpeeSal numeric],
[EmpeeGend],
[EmpeeAge]
)
VALUES
(
'000001','0000000001','Glen','Edelman','175 Bishops Lane','La Jolla','CA','619','5550199','Sales Worker','10072003','10.75','M','64','01234567'
)
答案 0 :(得分:4)
您的脚本中有一些错误和一些eybrow-raiser。这是我在SQL Server 2008实例上运行的脚本:
/*
DROP TABLE tblEmployee
GO
DROP TABLE tblJobTitle
*/
----Create Job Title Table
CREATE TABLE tblJobTitle
(
EmpeeJobID char (10) CONSTRAINT PK_EmpeeJobID PRIMARY KEY Not Null,
EmplerInfRep char (200) Not Null,
EmpeeJobTitle char (20) Not Null,
EmpeeJobDesc char (200) Not Null,
ExemNonExemStat char (20) Not Null
)
----Create Employee Table
CREATE TABLE tblEmployee
(
EmpeeID char (6) CONSTRAINT PK_EmpeeID PRIMARY KEY Not Null,
EmpeeJobID char (10) CONSTRAINT FK_EmpeeJobID FOREIGN KEY (EmpeeJobID)REFERENCES tblJobTitle (EmpeeJobID) Not Null,
EmpeeLName char (25) Not Null,
EmpeeFName char (20) Not Null,
EmpeeAdd char (100) Not Null,
EmpeeCity char (100) Not Null,
EmpeeState char (100) Not Null,
EmpeeTeleAreaCode char (3) Not Null,
EmpeeTeleNum char (7) Not Null,
EmplerInfRep char (200) Not Null,
EmpeeHirDate datetime Not Null,
EmpeeSal numeric (8) Not Null,
EmpeeGend char (6) Not Null,
EmpeeAge char (3) Not Null
)
--Insert record 1 into Job Title table
INSERT INTO tblJobTitle
(
[EmpeeJobID],
[EmplerInfRep],
[EmpeeJobTitle],
[EmpeeJobDesc],
[ExemNonExemStat]
)
VALUES
(
'0000000001','Office/Clerical','Accounting Clerk','Computes, classifies, records, and verifies numerical data for use in maintaining accounting records.','Exempt'
)
--Insert record 1 into Employee table
INSERT INTO tblEmployee
(
[EmpeeID],
[EmpeeJobID],
[EmpeeLName],
[EmpeeFName],
[EmpeeAdd],
[EmpeeCity],
[EmpeeState],
[EmpeeTeleAreaCode],
[EmpeeTeleNum],
[EmplerInfRep],
[EmpeeHirDate],
[EmpeeSal],
[EmpeeGend],
[EmpeeAge]
)
VALUES
(
'000001',
'0000000001',
'Glen',
'Edelman',
'175 Bishops Lane',
'La Jolla',
'CA',
'619',
'5550199',
'Sales Worker',
'8/25/09',
'10.75',
'M',
'64'
)
PK_EmpeeJobID
)的两个PK约束。将第二个更改为PK_EmpeeID
。tblEmployee
插入中的列太多了。删除最后一个值,你就没事了。10072003
转换为datetime
列中的EmpeeHirDate
。我将其更改为8/25/09
。char
字段进行可变长度输入。使用varchar
,以便在比较时不必每列rtrim(ltrim())
。这样,您可以索引它们。当您必须对列执行字符串操作时,编译器无法使用索引,并被强制进行表扫描。这往往是性能瓶颈。EmpeeJobID int PRIMARY KEY NOT NULL IDENTITY(1,1)
,使其成为identity
列(也称为顺序)。此外,int
更快加入。对EmpeeID
执行相同的操作。希望这能让你指向正确的方向!
答案 1 :(得分:0)
第53行似乎是tblJobTitle的第一个插入。
我会尝试一些事情:
sp_help tblJobTitle以确保列实际存在于表中。
由于您在插入中使用了ExemNonExemStat周围的括号,因此在创建表时也请尝试使用它们。它不应该有所作为,但这里的某些东西绝对是奇怪的,所以绝对肯定是值得的。
一次执行一个查询,以确保错误发生的位置。
我认为这与外键没有任何关系,但也尝试删除它。