违反主键约束“pk\uu transact\uuu”无法在对象“dbo.transactions\u id”中插入重复键重复键值为(1001)

np8igboo  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(218)

当我试图插入执行表时,我得到了那个错误。当我尝试插入更多行到 Employee_in 我出错了
表值构造函数中每行的列数必须相同

CREATE DATABASE EmployeeDatabase

USE EmployeeDatabase

CREATE TABLE Employee_In
(
    EmployeeID INT NOT NULL PRIMARY KEY,
    EmployeeName CHAR(30) NOT NULL,
    EmployeeCountry CHAR(30)NOT NULL,
    EmployeeSalary INT NOT NULL
)

USE EmployeeDatabase
GO

INSERT INTO Employee_In (EmployeeID, EmployeeName, EmployeeCountry, EmployeeSalary)
VALUES (1001, 'Sundar', 'USA', 125000),
       (1002, 'Satya', 'CANADA', 120000);

SELECT * FROM Employee_In

CREATE TABLE Transactions_ID
(
    TransactionID INT NOT NULL PRIMARY KEY,
    EmployeeID INT FOREIGN KEY REFERENCES Employee_In(EmployeeID),
    PostalDate VARCHAR(20) NOT NULL,
    Amount VARCHAR(40) NOT NULL,
    Description CHAR(25),
);

USE EmployeeDatabase
GO

INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES ('1001','1001','2020-04-07','20000')
6tr1vspr

6tr1vspr1#

违反主键约束“pk\uu transact\uuu”。无法在对象“dbo.transactions\u id”中插入重复键。重复键值为(1001)
这意味着您的表中已经有了pk transactionid的值1001,很可能来自insert和更早的insert。

INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES ('1001', '1001', '2020-04-07', '20000')

在这里,您尝试在数据类型为int的列中插入字符串。。。这应该是:

INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES (1001, 1001, '2020-04-07', '20000')

祝你好运!

相关问题