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

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

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

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

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

 

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

 

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

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

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

 

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

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

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

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

新建复合主键类TableDomainIdPK.java。

 

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

 

新建实体类EL_TransIdTable.java。

实体类中的配置如下:

 

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

 

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

【编辑推荐】

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

2011-03-28 14:29:46

SQL Server数主键列

2011-08-03 10:04:57

SQL Server数没有主键的表

2011-08-01 09:50:31

SQL Server数主键索引

2010-10-21 14:54:32

查询SQL Serve

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删

2011-05-12 13:34:57

SQL Server

2010-09-25 09:55:14

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删除主键

2019-08-29 07:13:50

oracle数据库主键

2010-06-02 11:24:57

MySQL数据库主键

2009-06-01 12:11:31

hibernatejpa复合主键

2010-10-26 15:54:02

连接oracle数据库
点赞
收藏

51CTO技术栈公众号