mma*_*tax 240 sql t-sql sql-server
我从来没有为SQL Server"手动编码"对象创建代码,并且外键修改在SQL Server和Postgres之间看似不同.这是我的sql到目前为止:
drop table exams;
drop table question_bank;
drop table anwser_bank;
create table exams
(
exam_id uniqueidentifier primary key,
exam_name varchar(50),
);
create table question_bank
(
question_id uniqueidentifier primary key,
question_exam_id uniqueidentifier not null,
question_text varchar(1024) not null,
question_point_value decimal,
constraint question_exam_id foreign key references exams(exam_id)
);
create table anwser_bank
(
anwser_id uniqueidentifier primary key,
anwser_question_id uniqueidentifier,
anwser_text varchar(1024),
anwser_is_correct bit
);
Run Code Online (Sandbox Code Playgroud)
当我运行查询时,我收到此错误:
消息8139,级别16,状态0,行9外键中的引用列数与引用列的数量不同,表'question_bank'.
你能发现错误吗?
Ale*_*use 318
如果您只想自己创建约束,可以使用ALTER TABLE
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) references MyOtherTable(PKColumn)
Run Code Online (Sandbox Code Playgroud)
我不推荐Sara Chipps提到的内联创建语法,因为我宁愿命名自己的约束.
Joh*_*ker 194
create table question_bank
(
question_id uniqueidentifier primary key,
question_exam_id uniqueidentifier not null,
question_text varchar(1024) not null,
question_point_value decimal,
constraint fk_questionbank_exams foreign key (question_exam_id) references exams (exam_id)
);
Run Code Online (Sandbox Code Playgroud)
Sar*_*pps 67
您还可以使用以下命令命名外键约束:
CONSTRAINT your_name_here FOREIGN KEY (question_exam_id) REFERENCES EXAMS (exam_id)
Run Code Online (Sandbox Code Playgroud)
Sha*_*ais 30
我喜欢AlexCuse的答案,但是每当你添加外键约束时你应该注意的是你希望如何处理引用表的行中引用列的更新,特别是你想要如何删除引用中的行待治疗的桌子.
如果创建约束,则:
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn )
references MyOtherTable(PKColumn)
Run Code Online (Sandbox Code Playgroud)
.. 如果引用表中有相应的行,那么引用表中的更新或删除将会出错.
这可能是你想要的行为,但根据我的经验,更常见的不是.
如果您改为创建它:
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn )
references MyOtherTable(PKColumn)
on update cascade
on delete cascade
Run Code Online (Sandbox Code Playgroud)
..然后,父表中的更新和删除将导致更新和删除引用表中相应行.
(我并不是说应该更改默认值,默认值是谨慎的,这很好.我只是说这是一个创建constaints的人应该始终注意的事情.)
顺便说一句,创建表时可以这样做,如下所示:
create table ProductCategories (
Id int identity primary key,
ProductId int references Products(Id)
on update cascade on delete cascade
CategoryId int references Categories(Id)
on update cascade on delete cascade
)
Run Code Online (Sandbox Code Playgroud)
Bij*_*mon 12
create table question_bank
(
question_id uniqueidentifier primary key,
question_exam_id uniqueidentifier not null constraint fk_exam_id foreign key references exams(exam_id),
question_text varchar(1024) not null,
question_point_value decimal
);
Run Code Online (Sandbox Code Playgroud)
- 那也行.Pehaps有点更直观的构造?
小智 7
在任何表上创建外键
ALTER TABLE [SCHEMA].[TABLENAME] ADD FOREIGN KEY (COLUMNNAME) REFERENCES [TABLENAME](COLUMNNAME)
EXAMPLE
ALTER TABLE [dbo].[UserMaster] ADD FOREIGN KEY (City_Id) REFERENCES [dbo].[CityMaster](City_Id)
Run Code Online (Sandbox Code Playgroud)
小智 7
如果要使用查询在关系中创建两个表的列,请尝试以下操作:
Alter table Foreign_Key_Table_name add constraint
Foreign_Key_Table_name_Columnname_FK
Foreign Key (Column_name) references
Another_Table_name(Another_Table_Column_name)
Run Code Online (Sandbox Code Playgroud)
像你一样,我通常不手动创建外键,但如果出于某种原因我需要脚本来这样做,我通常使用 ms sql server management studio 创建它,然后在保存然后更改之前,我选择表设计器 | 生成更改脚本
小智 5
这个脚本是关于用外键创建表,我添加了参照完整性约束sql-server。
create table exams
(
exam_id int primary key,
exam_name varchar(50),
);
create table question_bank
(
question_id int primary key,
question_exam_id int not null,
question_text varchar(1024) not null,
question_point_value decimal,
constraint question_exam_id_fk
foreign key references exams(exam_id)
ON DELETE CASCADE
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
440693 次 |
| 最近记录: |