Constraint in SQL

Constraint: 

  1. Primary Key
  2. Not Null
  3. Check and
  4. References.
Creating Table using primary key and not null

create table student
        (
        rollno number primary key,
        name varchar2(15) not null,
        Branch varchar2(5),
        gender varchar2(7),
        DOB date,
        email_id varchar2(25),
        Address varchar2(25),
      phone_no number
     );

Creating Table using check and References

create table student_marks
   (
    rollno number references students_035,
    marks number
    constraint check_marks check(marks between 0 and 100)
    );

We apply the check Constraint on marks so if marks is not between the 0 and 100 then sql command is violated.
Example:
SQL> insert into student_marks values(1,67);

1 row created.

SQL> insert into student_marks values(40,107);
insert into student_marks values(40,107)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CHECK_MARKS) violated



Note: All commands are apply on above two tables for better understanding male your own table and attributes.
Query and any doubts please leave in comments.


Share this

Related Posts

Previous
Next Post »

Powered by Blogger.

Popular Posts