有没有办法在sql中自动将新行添加到联结表中?

时间:2016-06-18 15:13:43

标签: sql sql-server reference sql-server-2014-express junction-table

我有两个表,tbl_Interviewtbl_JobSeeker两者之间的关系是多对多的,规范化规则要求我创建一个新表,我用另一个表调用tbl_Interview_JobSeeker两个表的主键,我的问题是:如果表tbl_Interview_JobSeeker是一个联结表,无论​​如何我在tbl_Interview表中创建一个新行,以自动在联结表中创建一个新行?

1 个答案:

答案 0 :(得分:0)

我认为您希望使用存储过程来处理创建JobSeeker和Interview之间的关系,这些关系是独立实体并且独立存在。这种多对多关系在关系现实中非常普遍。下面是在关系数据库中处理关系的示例。虽然它使用人和课程的概念。

创建表格

create table Person (
    PersonId int identity(1, 1)
        primary key clustered,
    FirstName varchar(128),
    LastName varchar(128))

create table Course (
    CourseId int identity(1, 1)
        primary key clustered,
    CourseName varchar(128) unique);

create table PersonCourse (
    PersonId int not null
        references Person(PersonId),
    CourseId int not null
        references Course(CourseId),
    primary key clustered (PersonId, CourseId));
go

创建用于CRUD操作的存储过程

这些只是一些例子。许多这类程序更加复杂,可以防止不完整,不准确和/或无效的数据。

此外,不言而喻,这种方法有很多替代方案,并且对这些类型的检查所属的地方进行了大量辩论。只知道这是一种方法。

create proc dbo.CreatePerson
    @FirstName varchar(128),
    @LastName varchar(128),
    @PersonId int = null output
as begin try

    set nocount on;

    insert Person (FirstName, LastName)
        values (@FirstName, @LastName);

    set @PersonId = scope_identity();

end try
begin catch
    throw;
end catch
go

create proc dbo.CreateCourse
    @CourseName varchar(128),
    @CourseId int = null output
as begin try

    set nocount on;

    insert Course(CourseName)
        values (@CourseName);

    set @CourseId = scope_identity();

end try
begin catch
    throw;
end catch
go

create proc dbo.AddCourse
    @PersonId int,
    @CourseId int
as begin try

    set nocount on;

    if not exists (
        select * 
        from Person
        where PersonId = @PersonId)
            throw 50000, 'Person does not exist.', 1;

    if not exists (
        select *
        from Course
        where CourseId = @CourseId)
            throw 50001, 'Course does not exist.', 1;

    insert PersonCourse (PersonId, CourseId)
        values (@PersonId, @CourseId);

end try
begin catch
    throw;
end catch
go

创建一些示例数据

insert Person (FirstName, LastName)
    values
        ('Bruce', 'Wayne'),
        ('Clark', 'Kent'),
        ('Flash', 'Gordon');

insert Course (CourseName)
    values
        ('The History of Spandex'),
        ('Super Hero Physics'),
        ('Rocket Ships and the Stuff of Science Fiction');
go

添加一些课程

exec AddCourse 1, 1; -- Bruce wants to take The History of Spandex
exec AddCourse 2, 2; -- Clark wants to take Super Hero Physics
exec AddCourse 3, 1; -- Flash wants to take The History of Spandex
exec AddCourse 3, 3; -- Flash wants to also take Rocket Ships and the Stuff of Science Fiction

查询数据

select
    pc.PersonId,
    p.FirstName,
    p.LastName,
    c.CourseId,
    c.CourseName
from PersonCourse pc
join Person p
    on pc.PersonId = p.PersonId
join Course c
    on pc.CourseId = c.CourseId;

结果

PersonId    FirstName  LastName   CourseId    CourseName
----------- ---------- ---------- ----------- -----------------------------------------------
1           Bruce      Wayne      1           The History of Spandex
2           Clark      Kent       2           Super Hero Physics
3           Flash      Gordon     1           The History of Spandex
3           Flash      Gordon     3           Rocket Ships and the Stuff of Science Fiction