I have an entity like this,
@Entity
@Table(name = "Persons", schema="PU")
public class Persons {
@Basic
@Column(name = "PERSON_ID")
private String personId
@Basic
@Column(name = "PERSON_NAME")
private String personName
@Basic
@Column(name = "PERSON_NAME_COPY")
private String personNameCopy
}
I want to add a constraint in the personNameCopy column such that whatever value is being inserted in that column should be present in the personName column.
Is there any way to achieve this?
DROP TABLE IF EXISTS dbo.Person;
CREATE TABLE dbo.Person
(
PERSON_ID INT NOT NULL PRIMARY KEY,
PERSON_NAME VARCHAR(50)NOT NULL,
PERSON_NAME_COPY VARCHAR(50)NOT NULL,
CONSTRAINT CHK_PERSON_NAME_PERSON_NAME_COPY CHECK (PERSON_NAME=PERSON_NAME_COPY)
)
GO
INSERT dbo.Person(PERSON_ID,PERSON_NAME,PERSON_NAME_COPY)
VALUES(1,'KUMAR','KUMAR')
--THIS ROW IS TO BE REJECTED
INSERT dbo.Person(PERSON_ID,PERSON_NAME,PERSON_NAME_COPY)
VALUES(2,'JOHN','KUMAR')
SELECT *FROM DBO.Person;
if I understand your question correctly you can implement check constraint (example is above) Please also have look https://learn.microsoft.com/en-us/sql/relational-databases/tables/create-check-constraints?view=sql-server-ver16
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.