【发布时间】:2015-06-15 14:27:25
【问题描述】:
我创建了两个表,并在它们之间创建了关系。
表students:
create table students
(
[StudentId] NVARCHAR(50) NOT NULL PRIMARY KEY,
[Name] NVARCHAR(50) NOT NULL
);
表studentprofile:
create table studentprofile
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY(1, 1),
[StudentId] NVARCHAR(50) NOT NULL,
[Address] NVARCHAR(50) NOT NULL,
);
和关系:
alter table studentprofile
add constraint students_studentprofile_FK
foreign key (StudentId)
references students(StudentId)
on delete cascade on update cascade
但是,当我写下这一行时:
insert into students values('110111', 'Marik')
StudentId 的值(在表 studentprofile 中)没有自动更新。为什么?
你能告诉我如何设置StudentId(在表studentprofile中)的值可以在我插入表students时自动插入吗?
【问题讨论】:
-
为什么
inserting会对update cascade产生任何影响?insert != update -
你应该做 2 次插入。第一次插入
students没有studentprofile表的Address信息。 -
另外,您希望将什么内容插入到学生档案中?两个表之间看起来是一对多的关系,因此您可以插入无限数量的行,您如何决定应该自动插入表中的内容?特别是当列不可为空时
标签: sql-server foreign-keys primary-key