向具有两个关系的列插入数据

Insert data to column which has two relationships

所以,我想在这里做的是我想将数据插入到具有 'username' 字段的 table1,并且这个 'username' 字段有两个关系,是给学生 table 和老师 table。但是,当我向用户 table 插入数据时,我遇到了问题,这个问题是由 'username' 字段引起的。是因为'username'字段的数据和studenttable中的unique key不一样,改成一样的时候也报错,不过这次的数据与教师 table 中的唯一性不同。那么,是否有可能使这个 'username' 字段只获得 table 之一,就像学生 table 和老师 table 的数据之一在 'username',还是可以用的。或者也许这是错误的,因为错误的 ERD?这是我的 ERD,如果你问的话:

好吧,我知道这是非常非常糟糕的主意,但我将教师 table 和学生 table 中的名字设为唯一键,因为我无法创建外键我没有那样做。拜托,我真的很感谢你的回答。

这是 3 tables 的 ddl :

学生table:

CREATE TABLE [dbo].[student](
[studentid] [int] IDENTITY(2016000001,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[address] [text] NOT NULL,
[gender] [varchar](7) NOT NULL,
[dateofbirth] [date] NOT NULL,
[nohp] [varchar](13) NOT NULL,


CONSTRAINT [PK_student] PRIMARY KEY CLUSTERED 
(
[studentid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],


CONSTRAINT [IX_student] UNIQUE NONCLUSTERED 
(
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

老师Table:

CREATE TABLE [dbo].[teacher](
[teacherid] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[gender] [varchar](7) NOT NULL,

CONSTRAINT [PK_teacher] PRIMARY KEY CLUSTERED 

(
    [teacherid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
 CONSTRAINT [IX_teacher] UNIQUE NONCLUSTERED 
(
    [name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

用户table:

CREATE TABLE [dbo].[user](
[userid] [int] IDENTITY(1,1) NOT NULL,
[username] [varchar](50) NOT NULL,
[password] [varchar](20) NOT NULL,
[role] [varchar](10) NOT NULL,
 CONSTRAINT [PK_user] PRIMARY KEY CLUSTERED 
(
    [userid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[user]  WITH CHECK ADD  CONSTRAINT [FK_user_student] FOREIGN KEY([username])
REFERENCES [dbo].[student] ([name])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[user] CHECK CONSTRAINT [FK_user_student]
GO

ALTER TABLE [dbo].[user]  WITH CHECK ADD  CONSTRAINT [FK_user_teacher] FOREIGN KEY([username])
REFERENCES [dbo].[teacher] ([name])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[user] CHECK CONSTRAINT [FK_user_teacher]
GO

您面临的主要问题是因为您的数据结构不太理想。它做了一些事情,比如阻止你改变某人的名字。我整理了一个简洁设计的简单示例。这里有很多假设。我假设人们有名字和姓氏。如果我不至少指出假设不是你总能做出的事情,那我就是失职了。 https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ 但对于学校项目来说,这已经足够了。我还假设地址是美国地址。同样,这在许多现实世界场景中都行不通。最后一个假设是每个人都可以是男性或女性。在当今世界,情况并非总是如此,但足以证明该技术。

下面是我可能会如何进行此类设计。我建议您不要只是盲目地复制它,而是将其用作使您的设计更正确规范化的想法。

create table Users
(
    UserID int identity not null
    , FirstName varchar(50) not null
    , LastName varchar(50) not null
    , AddressLine1 varchar(50)
    , AddressLine2 varchar(50)
    , City varchar(50)
    , ST char(2)
    , ZipCode varchar(9)
    , Gender char(1)
    , constraint PK_Users primary key clustered
        (
            UserID
        )
    , constraint CHK_Users_Gender
        CHECK (Gender in ('M', 'F'))
    , constraint CHK_Users_ZipCode
        CHECK (LEN(ZipCode) in (5,9)) --This ensures you have either the 5 or 9 digiti zip code
)


create table Student
(
    StudentID int identity not null
    , UserID int not null
    , BirthDate date
    , constraint PK_Student primary key clustered
        (
            StudentID
        )
    , constraint FK_Student_Users foreign key (UserID) references Users(UserID)
)

create table Teacher
(
    TeacherID int identity not null
    , constraint PK_Teacher primary key clustered
        (
            TeacherID
        )
    , constraint FK_Teacher_Users foreign key (TeacherID) references Users(UserID)
)