Question

I'm having difficulties writing an Integrity Constraint check for table creation. The following is my table creation statement:

CREATE TABLE User (
UName VARCHAR(15) PRIMARY KEY,
FName VARCHAR(15),
LName VARCHAR(15)
);

The integrity constraint I'm looking for is that the UName (Username) cannot contain the first name, and/or the last name.

Can I write this without PL/SQL?

Any help is greatly appreciated.

Was it helpful?

Solution

Yes, you need define it as a TABLE CHECK CONSTRAINT (not against the UNAME column, as it refers to other columns in the same table) as follows:

CREATE TABLE User
(
    UNAME VARCHAR(100),
    FNAME VARCHAR(100),
    LNAME VARCHAR(100),
    CONSTRAINT CHECK_USERS 
        CHECK(INSTR(UNAME, FNAME) = 0 AND INSTR(UNAME, LNAME) = 0)
)

OTHER TIPS

In order to do the sql more reliable I would recommend to use upper case. Users surely strive to make the login look like last/first name but I stumbled with cases where they use first capital letter in first/last name but lower case for user name and vice versa.

so consider using

CREATE TABLE User
(
    UNAME VARCHAR(100),
    FNAME VARCHAR(100),
    LNAME VARCHAR(100),
    CONSTRAINT CHECK_USERS 
        CHECK(INSTR(upper(UNAME), upper(FNAME)) = 0 AND INSTR(upper(UNAME), upper(LNAME)) = 0)
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top