Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i was tried to validate the data in the sqlserver , i want to insert a new row in the table , before inserting the data i have to check the the two cloumns data should not repaeat , if not exists then insert the data , is there any contrsints or funtions in sqlserver

What I have tried:

create table check_con1( name varchar(25),vs varchar(10),vn varchar(10) ,check(vs!=vn))
Posted
Updated 5-Nov-20 0:03am

1 solution

Sounds like you are looking for SQL unique constraints (apply them to the two columns you don't want value to be repeated):
SQL UNIQUE Constraint[^]
SQL - UNIQUE Constraint - Tutorialspoint[^]

For example, the following SQL query creates a new table called CUSTOMERS and adds five columns. Here, the AGE column is set to UNIQUE, so that you cannot have two records with the same age.
SQL
CREATE TABLE CUSTOMERS(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL UNIQUE,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2),       
   PRIMARY KEY (ID)
);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900