以下是我开始使用简单求职数据库的代码。 当我尝试使用insert语句实际填充数据库时,它会多次添加每一行。
CREATE TABLE IF NOT EXISTS Employers (
Id INTEGER PRIMARY KEY autoincrement,
Name VARCHAR(20) NOT NULL,
Industry VARCHAR(20) NOT NULL,
Description TEXT
);
CREATE TABLE IF NOT EXISTS Jobs (
Id INTEGER PRIMARY KEY autoincrement,
EmpId INTEGER FOREIGN KEY REFERENCES Employers(Id),
Title VARCHAR(20) NOT NULL,
Description TEXT,
Location VARCHAR(20) NOT NULL,
Ann_sal INT NOT NULL
);
INSERT INTO Employers(Name, Industry, Description )
VALUES
("Touchpoint Global", "IT", "We help achieve efficiency, reliability and accuracy.
We are experts at turning complex inputs into simple outputs.");
INSERT INTO Employers(Name, Industry, Description )
VALUES
("TyTech", "IT", "Here at Tytech, we specialise is building unique business solutions from the ground up. We focus on what you want, and how you want it.");
INSERT INTO Jobs( EmpId, Title, Description, Location, Ann_sal )
SELECT emp.Id, "Project Manager", "Project manager needed to oversee all future projects.", "GoldCoast", 80000
FROM Employers emp
WHERE emp.Name="Tytech";
INSERT INTO Jobs( EmpId, Title, Description, Location, Ann_sal )
SELECT emp.Id, "Project Manager", "Project manager needed to oversee all revolutionary projects.", "Kingscliff", 95000
FROM Employers emp
WHERE emp.Name="Touchpoint Global";
INSERT INTO Jobs( EmpId, Title, Description, Location, Ann_sal )
SELECT emp.Id, "Web Designer", "Web designer needed for upcoming business solution project.", "Kingscliff", 75750
FROM Employers emp
WHERE emp.Name="Touchpoint Global";
INSERT INTO Jobs( EmpId, Title, Description, Location, Ann_sal )
SELECT emp.Id, "Data Analist", "Data analist needed to review input data during testing phases and maintanence.", "Kingscliff", 70000
FROM Employers emp
WHERE emp.Name="Touchpoint Global";
答案 0 :(得分:0)
创建适当且有意义的唯一索引可以防止插入不需要的重复项。例如名称和行业,或标题。
答案 1 :(得分:0)
您的脚本多次执行。 每次重复执行都会插入更多记录。
要确保表为空,可以在INSERT之前添加以下语句:
DELETE FROM Employers;
DELETE FROM Jobs;
但是,为了确保在结构发生更改时更新表,您应该从CREATE TABLE语句中删除IF NOT EXISTS,并确保在这些CREATE TABLE语句之前删除它们:
DROP TABLE IF EXISTS Employers;
DROP TABLE IF EXISTS Jobs;