Skip to content
Advertisement

Creating a table where UserPassword not equal to UserName, but the following code returning a error

In SQL Server:

CREATE TABLE Credentials 
(
    UserName VARCHAR(20) NOT NULL,
    UserPassword VARCHAR(20) 
        CONSTRAINT chk_constraint CHECK (UserPassword <> UserName) NOT NULL,
)

I get this error:

Column check constraint for column ‘UserPassword’ references another column, table (Credentials).

Advertisement

Answer

You are defining column level constraints,

In a column level constraint, you can only define constraint for specific column.

For your requirement, you should go for table level constraints, from this you can define constraint for multiple columns, like:

CREATE TABLE Credentials 
(
    UserName VARCHAR(20) NOT NULL,
    UserPassword VARCHAR(20) NOT NULL,
    CONSTRAINT chk_constraint CHECK (UserPassword <> UserName)
)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement