SQL插入一对多关系 [英] SQL Insert into One-to-Many relationship

查看:115
本文介绍了SQL插入一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
我有桌子GymMember GymMemberID是PK

HI
I have Table GymMember GymMemberID is PK

GymMemberID(PK)
Name
Surname
Password
TrainingProgramID
Email
BMI



TABLE Log WHERE Log#是PK



TABLE Log WHERE Log# is PK

Log#(PK)
Weight
height
Chest
Forearms
Arm
Sholulders
Thighs
Calves
date
BodyMassIndex
BeforePicturePath
AfterPicturePath



然后链接Table GymMemberLog



Then linking Table GymMemberLog

GymMemberID(PK)
Log#(PK)



我想为每个GymMember插入许多日志.
一个gymMember可以有很多日志,但是一个日志只能用于一个健身房成员.

希望获得有关如何插入的建议,例如每个gymMembers Log#应该以1开始并递增.



I want to insert many logs per GymMember.
A gymMember can have many logs but a log can only be for one gym member.

Id like advice on how to insert eg Each gymMembers Log#''s should start at 1 and increment.

推荐答案

您可以将IDENTITY(1,1)用于Log#,因此您不必在每次添加新Log时都增加它.
另外,您还需要向日志表中添加GymMemberID和外键约束.
这样,当您插入为新日志时,您将需要具有GymMemberID,并且能够检索新的日志#
即:
You can use IDENTITY (1,1) for the Log# so you do not to increment it every time you add a new Log.
Also you need to add GymMemberID and a Foreign Key constraint to Log Table.
This way when you insert as new Log you will need to have the GymMemberID and you will be able to retrieve the new Log#
ie:
declare @LogNum int
INSERT INTO Log
VALUES (@height,@Chest,@Forearms,@Arm,@Sholulders,@Thighs,@Calves,@date,@BodyMassIndex,@BeforePicturePath,@AfterPicturePath,@GymMemberID)
SET @LogNum = SCOPE_IDENTITY()


这篇关于SQL插入一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆