server中表中的复合主键

server中表中的复合主键

本文介绍了Sql server中表中的复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表中创建复合主键,但是Ms Sql server给出了您无法创建复合主键的错误。为什么?或者任何其他选择。

我想创建表订阅与表付款之间的一对一关系,然后创建一对一的关系,表付款到PaymnetCash,PaymentWesternUnion,PaymentCreditCard。



 创建  TblSubscription 

SubscriptionId int identity 1 1 主要 密钥
UserId INT NOT NULL 外来 密钥 参考 TblAppUser( UserId),
PackageId INT NOT NULL 外来 密钥 引用 TblPackages(PackageID),
StartDate datetime
FinishDate datetime
IsActive 位,
IsPaid
) ;


创建 TblPayment

PaymentId int identity 1 1 主要 密钥
SubscriptionId int 主要 密钥
PaymentAmount float
PaymentDate datetime
PaymentMethod varchar 50 ),
PaymentStatus bit
);
解决方案

I want to create Composite Primary Key in a table, but Ms Sql server gives me the error that you cant create composite primary keys. why? or any alternative.
I want to create One-to-One relationship between Table Subscription against Table Payment then then One to One relationship against Table Payment to PaymnetCash,PaymentWesternUnion,PaymentCreditCard.

Create Table TblSubscription
(
SubscriptionId int identity(1,1) Primary key,
UserId INT NOT NULL Foreign key References TblAppUser(UserId),
PackageId INT NOT NULL Foreign key References TblPackages(PackageID),
StartDate datetime,
FinishDate datetime,
IsActive bit,
IsPaid bit
);


Create Table TblPayment
(
PaymentId int identity(1,1) Primary Key,
SubscriptionId int Primary key,
PaymentAmount float,
PaymentDate datetime,
PaymentMethod varchar(50),
PaymentStatus bit
);
解决方案


这篇关于Sql server中表中的复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:39