SQL Server数据库主键及复合主键的配置

数据库 SQL Server
本文主要介绍了SQL Server数据库中主键以及复合主键的一些配置问题,希望能对您有所帮助。

本文主要介绍了SQL Server数据库中主键以及复合主键的配置,接下来我们就开始介绍。

一般情况下一个表中的主键,id intidentity(1,1)primary key

这是最常见的咯,用注解的形式标记这种主键也很简单

 

@Id    
 
@GeneratedValue    
 
@Column(name="RecId")    
 
public int getRecId() {    
 
return RecId;    
 
}   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

 

复合主键个人认为用到的很少,呜呜还是碰到了,由于没有经验,东装西摸,浪费了很长时间才把复合主键配好了,并且还出了很多异常,如下所示:

一個裱中可以有多個字段組成的主鍵  

create table EL_TransIdTable(    
 
TableName nvarchar(50) ,    
 
LastTransId nvarchar(15),    
 
Prefix nchar(5),    
 
DomainId nvarchar(10) primary key(TableName,DomainId)    
 
)   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

 

其中TableName 、DomainId两个字段作为此表的主键。

在配置中主要分为两个步骤:

1 为复合主键,建立一个复合主键类,这个类包括两个字段,(有几个字段组成主键 就包含几个字段 )这个复合主键类实现Serializable接口,有public 无参的构造方法 重写equals 和hashcode方法。

2:在实体类里面用idclass标示复合主键类 详情如下:

新建复合主键类TableDomainIdPK.java。

 

package com.barcode.Model;    
 
import java.io.Serializable;    
 
public class TableNameDomainIdPK implements Serializable{    
 
public TableNameDomainIdPK(){    
 
}    
 
private String TableName;    
 
private String DomainId;    
 
public String getTableName() {    
 
return TableName;    
 
}    
 
public void setTableName(String tableName) {    
 
TableName = tableName;    
 
}    
 
public String getDomainId() {    
 
return DomainId;    
 
}    
 
public void setDomainId(String domainId) {    
 
DomainId = domainId;    
 
}    
 
@Override    
 
public int hashCode() {    
 
final int PRIME = 31;    
 
int result =1;    
 
result=PRIME*result+((TableName==null)?0:TableName.hashCode());    
 
result=PRIME*result+((DomainId==null)?0:DomainId.hashCode());    
 
return result;    
 
}    
 
@Override    
 
public boolean equals(java.lang.Object obj) {    
 
if(this ==obj){    
 
return true;    
 
}    
 
if(null ==obj ){    
 
return false;    
 
}    
 
final TableNameDomainIdPK other=(TableNameDomainIdPK)obj;    
 
if(DomainId==null){    
 
if(other.DomainId!=null){    
 
return false;    
 
}    
 
}else if(!DomainId.equals(other.DomainId)){    
 
return false;    
 
}    
 
if(TableName==null){    
 
if(other.TableName!=null){    
 
return false;    
 
}    
 
}else if (!TableName.equals(other.TableName)){    
 
return false;    
 
}           
 
return true;    
 
}    
 
}   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.

 

新建实体类EL_TransIdTable.java。

实体类中的配置如下:

 

package com.barcode.Model;    
 
import java.io.Serializable;    
 
import javax.persistence.Column;    
 
import javax.persistence.Entity;    
 
import javax.persistence.Id;    
 
import javax.persistence.IdClass;    
 
import javax.persistence.Table;    
 
@Entity       
 
@Table(name="EL_TransIdTable")    
 
@IdClass(TableNameDomainIdPK.class)    
 
public class EL_TransIdTable implements Serializable {    
 
private String TableName;    
 
private String LastTransId;    
 
private String Prefix;    
 
private String DomainId;    
 
@Id    
 
@Column(name="TableName"nullable = false)    
 
public String getTableName() {    
 
return TableName;    
 
}    
 
public void setTableName(String tableName) {    
 
TableName = tableName;    
 
}       
 
@Column(name="LastTransId")    
 
public String getLastTransId() {    
 
return LastTransId;    
 
}    
 
public void setLastTransId(String lastTransId) {    
 
LastTransId = lastTransId;    
 
}    
 
@Column(name="Prefix")    
 
public String getPrefix() {    
 
return Prefix;    
 
}    
 
public void setPrefix(String prefix) {    
 
Prefix = prefix;    
 
}       
 
@Id    
 
@Column(name="DomainId"nullable = false)    
 
public String getDomainId() {    
 
return DomainId;    
 
}    
 
public void setDomainId(String domainId) {    
 
DomainId = domainId;    
 
}    
 
public void Print_Info(){    
 
System.out.println(this.getDomainId()+this.getLastTransId()+this.getPrefix()+this.getTableName());    
 
}    
 
}   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.

 

关于SQL Server数据库复合主键的设置就介绍到这里,希望能够对您有所收获!

【编辑推荐】

  1. 用mysqldumpslow分析执行较慢的SQL语句
  2. 一些很实用的Oracle数据库优化策略总结篇
  3. 使用MySQL Proxy告终读写离别的操作实例
  4. 在SQL触发器或存储过程中获取登录用户信息
  5. 局域网所有机器都能连接MySQL数据库的设置命令
责任编辑:赵鹏 来源: CSDN博客
相关推荐

2011-08-03 10:04:57

SQL Server数没有主键的表

2011-03-28 14:29:46

SQL Server数主键列

2010-10-21 14:54:32

查询SQL Serve

2011-08-01 09:50:31

SQL Server数主键索引

2012-02-03 10:07:04

HibernateJava

2010-09-25 10:05:25

sql server主

2010-10-19 17:21:35

SQL SERVER主

2010-09-25 09:45:46

sql server主

2010-10-20 10:19:33

sql server删

2010-09-25 09:55:14

sql server主

2011-05-12 13:34:57

SQL Server

2010-07-05 15:12:30

SQL Server主

2010-10-19 17:34:10

sql server主

2011-04-13 14:20:52

SQL Server主键

2010-10-20 10:31:57

sql server联

2010-09-01 16:44:26

SQL删除主键

2010-10-26 15:54:02

连接oracle数据库

2010-07-15 17:28:50

SQL Server

2009-06-01 12:11:31

hibernatejpa复合主键

2019-08-29 07:13:50

oracle数据库主键
点赞
收藏

51CTO技术栈公众号