重复录入' 1'关键' PRIMARY' SQL

时间:2014-11-30 21:29:17

标签: mysql sql

CREATE TABLE Editor (
UsernameID VARCHAR (30),
EditorName VARCHAR (30),
EMail VARCHAR (30),
DateOfBirth DATE,
BlogTitle VARCHAR(30),
PRIMARY KEY (UsernameID));

INSERT INTO Editor VALUES
('Mdbuzzer','Joshua', 'coker@hotmail.com', '1995-03-15', 'Nearly Bound');

INSERT INTO Editor VALUES
('Kally32','Kally', 'kally@hotmail.com', '1993-10-13', 'Tomorrows War');
SELECT * FROM Editor;



CREATE TABLE Post
(UsernameID VARCHAR (30),
PostID INT,
BlogTitle VARCHAR (30),
PostTitle VARCHAR (30),
CategoryID INT,
TimeofPost VARCHAR (20),
PRIMARY KEY(PostID),
FOREIGN KEY (UsernameID) REFERENCES Editor (UsernameID));

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War','Two Can Play That Game','2', '12:00pm');

INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound', 'Goal','3', '10:05pm');

INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound','Life After Death','4', '12:03pm');

INSERT INTO Post VALUES
('Mdbuzzer','2', 'Nearly Bound','Times Up','1', '14:06pm');

SELECT * FROM Post;

每次我尝试运行此脚本时,我都会收到错误,指出重复的“主要”条目为“1”并且我真的不明白如何修复它

3 个答案:

答案 0 :(得分:1)

如果将列设为主键,则列中的每个值都必须是唯一的。添加第一个元组时,POST_ID列中已有一个“1”实例:

('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');

如果你尝试在POST_ID列中添加另一个带有'1'实例的元组,例如:

('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');

你会收到错误。

答案 1 :(得分:1)

问题是您在POST表中插入了多行,PRIMARY KEY为1.如果您尝试实现主键,则它必须是唯一的。

所以这个:

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War','Two Can Play That Game','2', '12:00pm');

INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound', 'Goal','3', '10:05pm');

INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound','Life After Death','4', '12:03pm');

INSERT INTO Post VALUES
('Mdbuzzer','2', 'Nearly Bound','Times Up','1', '14:06pm');

可以更改为:

INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');

INSERT INTO Post VALUES
('Kally32','2','Tomorrows War', 'Let It Shine','2','10:05pm');

INSERT INTO Post VALUES
('Kally32','3','Tomorrows War','Two Can Play That Game','2', '12:00pm');

答案 2 :(得分:1)

您已将PostId声明为POST的主键。

使用insert时,应始终包含列。因此,这是表中的前两个插入:

INSERT INTO Post(UsernameID, PostID, BlogTitle, PostTitle, CategoryID, TimeofPost)
    VALUES('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');

INSERT INTO Post(UsernameID, PostID, BlogTitle, PostTitle, CategoryID, TimeofPost)
    VALUES('Kally32','1','Tomorrows War','Two Can Play That Game','2', '12:00pm');

在这两种情况下,您都将PostId设置为相同的值1。因此错误。

我希望代码看起来像:

CREATE TABLE Post (
    UsernameID VARCHAR (30),
    PostID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    BlogTitle VARCHAR (30),
    PostTitle VARCHAR (30),
    CategoryID INT,
    TimeofPost VARCHAR (20),
    FOREIGN KEY (UsernameID) REFERENCES Editor (UsernameID)
);

INSERT INTO Post(UsernameID, BlogTitle, PostTitle, CategoryID, TimeofPost)
    VALUES('Kally32', 'Tomorrows War', 'Let It Shine', 2, '10:05pm');