此SQL语句怎么了?
Create table JaretsSchedule
(ScheduleID Int Primary Key NOT NULL,
FieldID Int NOT NULL,
HomeTeamID Int NOT NULL,
AwayTeamID Int NOT NULL,
GameTime DateTime NOT NULL);
Insert into JaretsSchedule
Values
(1, 1, 1, 2, '2012-10-02 12:00:00'),
(1, 1, 1, 3, '2012-10-17 12:00:00'),
(1, 1, 1, 4, '2012-09-23 12:00:00'),
(1, 2, 2, 1, '2012-09-12 12:00:00'),
(1, 3, 3, 1, '2012-08-19 12:00:00'),
(1, 4, 4, 1, '2012-07-25 12:00:00'),
(2, 2, 2, 1, '2012-09-15 12:00:00'),
(2, 2, 2, 3, '2012-09-06 12:00:00'),
(2, 2, 2, 4, '2012-08-28 12:00:00'),
(2, 1, 1, 2, '2012-10-02 12:00:00'),
(2, 3, 3, 2, '2012-08-11 12:00:00'),
(2, 4, 4, 2, '2012-07-17 12:00:00'),
(3, 3, 3, 1, '2012-08-19 12:00:00'),
(3, 3, 3, 2, '2012-08-11 12:00:00'),
(3, 3, 3, 4, '2012-08-03 12:00:00'),
(3, 1, 1, 3, '2012-10-17 12:00:00'),
(3, 2, 2, 3, '2012-10-17 12:00:00'),
(3, 4, 4, 3, '2012-07-09 12:00:00'),
(4, 4, 4, 1, '2012-07-25 12:00:00'),
(4, 4, 4, 2, '2012-07-17 12:00:00'),
(4, 4, 4, 3, '2012-07-09 12:00:00'),
(4, 1, 1, 4, '2012-09-23 12:00:00'),
(4, 2, 2, 4, '2012-08-28 12:00:00'),
(4, 3, 3, 4, '2012-08-03 12:00:00');
带有产生的错误代码;
“ Msg 2627,第14级,状态1,第8行违反主键约束'PK__JaretsSc__9C8A5B696CFE9A03'。无法在对象'dbo.JaretsSchedule'中插入重复的密钥。重复键值为(1)。该语句已终止。“
错误消息从字面上告诉您问题出在哪里。对于已定义为主键的ScheduleID
列,您不能多次插入相同的值。一旦解决,这里就是将这一列设为身份/自动增量列。然后,在插入时甚至不包含其值。相反,让SQL Server处理它:
CREATE TABLE JaretsSchedule (
ScheduleID INT NOT NULL IDENTITY PRIMARY KEY,
FieldID INT NOT NULL,
HomeTeamID INT NOT NULL,
AwayTeamID INT NOT NULL,
GameTime DateTime NOT NULL
);
INSERT INTO JaretsSchedule (FieldID, HomeTeamID, AwayTeamID, GameTime)
VALUES
(1, 1, 2, '2012-10-02 12:00:00'),
(1, 1, 3, '2012-10-17 12:00:00'),
(1, 1, 4, '2012-09-23 12:00:00'),
...
还请注意,我按顺序显式列出了作为插入目标的列。这也是最佳做法,可以避免以后可能不清楚VALUES
子句中的数据对应于哪些列的情况。