如何在数据库中使用多个主键 [英] how to use multiple primary key in a database

查看:388
本文介绍了如何在数据库中使用多个主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#.Net和Sql Server进行桌面应用程序.我采用了一种形式,其中有4个标签潜在客户ID,客户ID,客户名称和服务,并且它们是数据库的字段. ID和客户ID都不应有重复的值.因此,请告诉我该解决方案

我创建了两个表,一个表具有一个主键和两列

I am making desktop appication using c#.Net and Sql Server.I have taken a form in which there are 4 labels prospect id ,customer id,customer name and services and same are the fields of database.Nw i want that prospect id and customer id both should not have duplicate values.so please tell me the solution for this

I have created two tables one table having one primary key with two columns

create table PSM.dbo.conversion(Prospect_ID int,Customer_ID int ,Customer_Name varchar(50),Customer_Products_Services varchar(50),constraint pk1 primary key(Prospect_ID ,Customer_ID  ));



它成功运行


第二张表仅作为一列的外键



it run successfully


2nd table as foreign key of one column only

create table PSM.dbo.Customer_follow_up(Customer_ID int references conversion(Customer_ID),Customer_Name varchar(50),Customer_Products_Services varchar(50),Feedback nvarchar(1000));




在这里,得到一个错误


1776消息,级别16,状态0,第1行
在引用的表"PSM.dbo.conversion"中没有与外键"FK__Customer___Custo__5AEE82B9"中的引用列列表匹配的主键或候选键.
消息1750,级别16,状态0,第1行
无法创建约束.请参阅先前的错误.





问候




here,getting an error


Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table ''PSM.dbo.conversion'' that match the referencing column list in the foreign key ''FK__Customer___Custo__5AEE82B9''.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.





regards

推荐答案

您只能有一个唯一键 [
You can have only one Primary Key[^] in a table.

But you can have multiple Unique Key[^].


首先,您不能创建多个主键,可以在多个列上创建单个主键.

First of all you can not create multiple primary key, you can create single primary key on more than one column.

CREATE TABLE dbo.test
(
test1 int,
test2 int,
test3 int,
CONSTRAINT somename PRIMARY KEY (test1 , test2 )

)


尝试此链接

复合键 [
try this Link

Composite Key[^]


这篇关于如何在数据库中使用多个主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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