Results 1 to 3 of 3
  1. #1
    Join Date
    May 2013
    Posts
    33

    Unanswered: Check constraint

    Code:
    Create table students
    (
      Sid  number(3),
      Sname varchar(20)  not null,
      Email varchar(20)  not null,
      Doj  Timestamp    default    sysdate    not null,
      Dob  date     not null,
      Phno number(10),
      Address varchar(20),
      Resume LONG,
      Gender char(1) Default  'M',
      Cid  number,
      Constraint Pk_stu_sid  Primary key(sid,cid),
      Constraint Un_stud_email Unique(email),
      Constraint  chk_stud_email Check(email like '%@%.%'),
      Constraint chk_stud_phno Check(length(phno) = 10),
      Constriant chk_stud_gender Check(gender in ('M','F')),
      Constraint Fk_stud_cid foreign key(cid) references
              course(cid))
    )

    when i run the above code i m getting error like this...I dont understand wat error it is...Because i didnt reference any column in the check constraint...Thn why it says that i m referencing some column..

    Error:

    Code:
    SQL> 
    SQL> Create table students
      2  (
      3    Sid  number(3),
      4    Sname varchar(20)  not null,
      5    Email varchar(20)  not null,
      6    Doj  Timestamp    default    sysdate    not null,
      7    Dob  date     not null,
      8    Phno number(10),
      9    Address varchar(20),
     10    Resume LONG,
     11    Gender char(1) Default  'M',
     12    Cid  number,
     13    Constraint Pk_stu_sid  Primary key(sid,cid),
     14    Constraint Un_stud_email Unique(email),
     15    Constraint  chk_stud_email Check(email like '%@%.%'),
     16    Constraint chk_stud_phno Check(length(phno) = 10),
     17    Constriant chk_stud_gender Check(gender in ('M','F')),
     18    Constraint Fk_stud_cid foreign key(cid) references
     19            course(cid))
     20  )
     21  /
      Constriant chk_stud_gender Check(gender in ('M','F')),
                                                           *
    ERROR at line 17:
    ORA-02438: Column check constraint cannot reference other columns

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    Code:
    02438, 00000, "Column check constraint cannot reference other columns"
    // *Cause: attempted to define a column check constraint that references
    //         another column.
    // *Action: define it as a table check constriant.
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    Quote Originally Posted by satheeshsharma View Post
    Code:
    Create table students
    (
      ....
      Resume LONG,
    )
    Do not use the LONG datatype. It has been deprecated for 15 years now. Use CLOB instead.

    Constriant chk_stud_gender Check(gender in ('M','F')),
    That should be CONSTRAINT not CONSTRIANT.

    So the correct DDL is:
    Code:
    Create table students
    (
      Sid  number(3),
      Sname varchar(20)  not null,
      Email varchar(20)  not null,
      Doj  Timestamp    default    sysdate    not null,
      Dob  date     not null,
      Phno number(10),
      Address varchar(20),
      Resume CLOB,
      Gender char(1) Default  'M',
      Cid  number,
      Constraint Pk_stu_sid  Primary key(sid,cid),
      Constraint Un_stud_email Unique(email),
      Constraint  chk_stud_email Check(email like '%@%.%'),
      Constraint chk_stud_phno Check(length(phno) = 10),
      Constraint chk_stud_gender Check(gender in ('M','F')),
      Constraint Fk_stud_cid foreign key(cid) references
              course(cid))
    );
    Last edited by shammat; 08-14-13 at 12:27.
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags: http://www.dbforums.com/misc.php?do=bbcode#code

    Tips for good questions:

    http://tkyte.blogspot.de/2005/06/how...questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •