【问题标题】:Is there any way to automatically add a new row into a junction table in sql?有什么方法可以自动将新行添加到 sql 中的联结表中?
【发布时间】:2016-06-18 15:13:43
【问题描述】:

我有两个表,tbl_Interviewtbl_JobSeeker 两者之间的关系是多对多的,规范化规则要求我创建一个新表,我称之为 tbl_Interview_JobSeeker 与其他两个表的主键,我的问题是:如果表tbl_Interview_JobSeeker 是联结表,当我在tbl_Interview 表中创建新行以自动在联结表中创建新行时,是否仍然存在?

【问题讨论】:

  • 您可以创建一个触发器,在插入另一个表后更新联结表。您使用的是哪个版本的 SQL?
  • 我正在使用 SQL express 2014。我还必须创建 e 触发器才能删除正确?
  • 没有。 SQL Server 有级联删除,所以清理可能不会太麻烦。
  • 嗯,总结一下。我需要创建一个触发器来向联结表添加一个新行,并删除我以编程方式正确吗?
  • 我会使用 varchar 作为电子邮件,但包括一个 id 列,该列在所有用户中都是唯一的。

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


【解决方案1】:

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

创建表格

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

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 2020-09-02
    • 2015-02-21
    • 2014-07-30
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多