Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the configurations of SQLServer database primary key and compound primary key

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the configuration of SQLServer database primary key and compound primary key". In daily operation, I believe many people have doubts about the configuration of SQLServer database primary key and compound primary key. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "SQLServer database primary key and compound primary key". Next, please follow the editor to study!

@ Id@GeneratedValue@Column (name= "RecId") publicintgetRecId () {returnRecId;}

Personally, I think that the composite primary key is rarely used, but I still encountered it. due to lack of experience, it took a long time to match the composite primary key, and there were a lot of exceptions, as shown below:

A master that can have multiple fields in a table

CreatetableEL_TransIdTable (TableNamenvarchar (50), LastTransIdnvarchar (15), Prefixnchar (5), DomainIdnvarchar (10) primarykey (TableName,DomainId)

The two fields TableName and DomainId are used as the primary keys of this table.

There are two main steps in the configuration:

1 for the compound primary key, establish a compound primary key class, this class includes two fields, (several fields make up the primary key contains several fields) this compound primary key class implements the Serializable interface, there are public no-parameter constructors to override equals and hashcode methods.

2: marking the compound primary key class with idclass in the entity class is as follows:

Create a new compound primary key class TableDomainIdPK.java.

Packagecom.barcode.Model;importjava.io.Serializable;publicclassTableNameDomainIdPKimplementsSerializable {publicTableNameDomainIdPK () {} privateStringTableName;privateStringDomainId;publicStringgetTableName () {returnTableName;} publicvoidsetTableName (StringtableName) {TableName=tableName;} publicStringgetDomainId () {returnDomainId;} publicvoidsetDomainId (StringdomainId) {DomainId=domainId;} @ OverridepublicinthashCode () {finalintPRIME=31;intresult=1;result=PRIME*result+ ((TableName==null) 0:TableName.hashCode ()); result=PRIME*result+ ((DomainId==null) 0:DomainId.hashCode ()); returnresult;} @ Overridepublicbooleanequals (java.lang.Objectobj) {if (this==obj) {returntrue;} if (null==obj) {returnfalse;} finalTableNameDomainIdPKother= (TableNameDomainIdPK) obj If (DomainId==null) {if (other.DomainIdwords null) {returnfalse;}} elseif (! DomainId.equals (other.DomainId)) {returnfalse;} if (TableName==null) {if (other.TableNameplate null) {returnfalse;}} elseif (! TableName.equals (other.TableName)) {returnfalse;} returntrue;}}

Create a new entity class EL_TransIdTable.java.

What are the configurations of SQLServer database primary key and compound primary key

The configuration in the entity class is as follows:

Packagecom.barcode.Model;importjava.io.Serializable;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.Id;importjavax.persistence.IdClass;importjavax.persistence.Table;@Entity@Table (name= "EL_TransIdTable") @ IdClass (TableNameDomainIdPK.class) publicclassEL_TransIdTableimplementsSerializable {privateStringTableName;privateStringLastTransId;privateStringPrefix;privateStringDomainId;@Id@Column (name= "TableName", nullable=false) publicStringgetTableName () {returnTableName;} publicvoidsetTableName (StringtableName) {TableName=tableName;} @ Column (name= "LastTransId") publicStringgetLastTransId () {returnLastTransId;} publicvoidsetLastTransId (StringlastTransId) } @ Column (name= "Prefix") publicStringgetPrefix () {returnPrefix;} publicvoidsetPrefix (Stringprefix) {Prefix=prefix;} @ Id@Column (name= "DomainId", nullable=false) publicStringgetDomainId () {returnDomainId;} publicvoidsetDomainId (StringdomainId) {DomainId=domainId;} publicvoidPrint_Info () {System.out.println (this.getDomainId () + this.getLastTransId () + this.getPrefix () + this.getTableName ());}} at this point, the study on "what are the configurations of SQLServer database primary keys and compound primary keys" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report