建立正确的逻辑数据库

时间:2018-06-29 22:39:19

标签: sql postgresql

我在数据库中使用4个表。

示例:

Create table applicant(
Id int not null primary key,
IdName integer
idSkill integer,
idContact integer
Constraint initial foreign key (idName) References Initiale(id)
CONSTRAINT contacT foreign key (idContact) References contact(id)
CONSTRAINT Skills foreign key (idSkill) references skill(id))

Create table Initiale(
Id int not null primary key,
 firstname text,
middlename text)

Create table contact(
Id int not null primary key,
phone text,
email text)

Create tabke Skills(
Id int not null primary key,,
Nane text)

我想在4个表格中迅速插入数据,但我不明白,是什么得到ID并插入申请人中。

1 个答案:

答案 0 :(得分:2)

Create tabke Skills .....将失败。您应该使用Create table Skills

如果您没有Id int not null primary key,,(两个逗号),也会失败。

您很有可能正在使用Id INTEGER PRIMARY KEY而不是Id int not null primary key

那是因为通常Id列应该是该行的唯一标识符。使用SQLite INTEGER PRIMARY KEY时,INT PRIMARY KEY does not有一个特殊的含义。

也就是说,如果使用INTEGER PRIMARY KEY,则该列将是 rowid 列的别名,该列必须是唯一的带符号的64位整数,并且重要的是,如果在插入一行,SQLite将分配一个唯一的整数。即所有重要的 ID

Id 最初将为1,然后可能为2,然后可能为3,依此类推,尽管不能保证 Id 将单调递增。

还有其他错误,主要是省略了逗号。以下应该工作:-

CREATE TABLE IF NOT EXISTS applicant(
Id INTEGER PRIMARY KEY,
IdName integer,
idSkill integer,
idContact integer,
Constraint initial foreign key (idName) References Initiale(id),
CONSTRAINT contacT foreign key (idContact) References contact(id),
CONSTRAINT Skills foreign key (idSkill) references Skills(id));

Create table IF NOT EXISTS Initiale(
Id INTEGER PRIMARY KEY,
 firstname text,
middlename text);

Create table IF NOT EXISTS contact(
Id INTEGER PRIMARY KEY,
phone text,
email text);

Create table IF NOT EXISTS Skills(
Id INTEGER PRIMARY KEY,
Nane text);

然后,您可以沿着:-

的行插入数据
INSERT INTO Initiale (firstname,middlename)  -- Note absence of Id so SQLite will generate
VALUES
    ('Fred','James'), -- very likely id 1
    ('Alan','Roy'), -- very likely id 2
    ('Simon','Gerorge')-- very likely id 3
;

INSERT INTO contact  -- alternative way of getting Id generated (specify null for Id)
VALUES
    (null,'0123456789','email01@email.com'), -- very likely id 1
    (null,'0987654321','email02@email.com'), -- very likely id 2
    (null,'3333333333','email03.@email.com') -- very likely id 3
;

INSERT INTO Skills (Nane)
VALUES
    ('Skill01'),('Skill02'),('Skill03') -- very likely id's 1,2 and 3
;

INSERT INTO applicant (IdName,idSkill,idContact)
VALUES
        -- First applicant
        (2, -- Alan Roy
        3, -- Skill 3
        1), -- Contact 0123456789 )

        -- Second Applicant
        (3, -- Simon George
        3, -- Skill 3
        2), -- Contact 0987654321 )

        -- Third Applicant
        (2, -- Alan Roy again????
        1, -- Skill 1
        3), -- contact 3333333333)

        (1,1,1) -- Fred James/ Skill 1/ Contact  0123456789
        --- etc
;

请注意,必须在表名,联系方式和技能表中存在行,然后才能将其插入到申请人表中。

然后您可以运行查询,例如:-

SELECT * FROM applicant 
    JOIN Initiale ON Initiale.Id = idName
    JOIN contact ON contact.Id = idContact
    JOIN Skills ON Skills.Id = idSkill

这将导致(使用上面插入的数据):-

enter image description here