Vous êtes sur la page 1sur 3

Constraints -- Limitations that are set for any particular table or column.

Constraints are of certain types: --Not null --Primary key --Foreign key --Unique key --Check constraint Constraints can be defines in two ways: --System defines constraints defined at system level --Table level constraints defined at table level **System constraints SQL> create table emp(eid number primary key); Table created. SQL> desc user_constraints Name ----------------------------------------OWNER CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME SEARCH_CONDITION R_OWNER R_CONSTRAINT_NAME DELETE_RULE STATUS DEFERRABLE DEFERRED VALIDATED GENERATED BAD RELY LAST_CHANGE INDEX_OWNER INDEX_NAME INVALID VIEW_RELATED Null? -------NOT NULL NOT NULL Type ---------------------------VARCHAR2(30) VARCHAR2(30) VARCHAR2(1) NOT NULL VARCHAR2(30) LONG VARCHAR2(30) VARCHAR2(30) VARCHAR2(9) VARCHAR2(8) VARCHAR2(14) VARCHAR2(9) VARCHAR2(13) VARCHAR2(14) VARCHAR2(3) VARCHAR2(4) DATE VARCHAR2(30) VARCHAR2(30) VARCHAR2(7) VARCHAR2(14)

SQL> select constraint_name, constraint_type from user_constraints where table_n ame='EMP'; CONSTRAINT_NAME C ------------------------------ SYS_C005425 P --Here we just provided the column eid. It used this column and created a constr aint on it. --Primary key limitations: 1)does not allow repetitive values. 2)Does not allow null values. SQL> insert into emp values(2);

1 row created. SQL> / insert into emp values(2) * ERROR at line 1: ORA-00001: unique constraint (HR.SYS_C005425) violated SQL> insert into emp values(null); insert into emp values(null) * ERROR at line 1: ORA-01400: cannot insert NULL into ("HR"."EMP"."EID")

**Foreign key SQL> create table dept (did number, dname varchar2(20)); Table created. SQL> create table emp2(eid number primary key,ename varchar2(20),did number,cons traint emp_fk foreign key(did) references dept(did)); create table emp2(eid number primary key,ename varchar2(20),did number,constrain t emp_fk foreign key(did) references dept(did)) * ERROR at line 1: ORA-02270: no matching unique or primary key for this column-list SQL> drop table dept; Table dropped. SQL> create table dept (did number primary key, dname varchar2(20)); Table created. SQL> create table emp2(eid number primary key,ename varchar2(20),did number,con straint emp_fk foreign key(did) references dept(did)); Table created. Referential Integrity-Rule 1 **************************** --You cannot insert a foreign key in the child table if there is no correspondin g primary key in the parent table. SQL> desc emp2 Name Null? ----------------------------------------- -------EID NOT NULL ENAME DID SQL> insert into emp2 values(1,'Mohan',2); Type ---------------------------NUMBER VARCHAR2(20) NUMBER

insert into emp2 values(4,'Mohan',2) * ERROR at line 1: ORA-02291: integrity constraint (HR.EMP_FK) violated - parent key not found SQL> select * from dept; DID ---------4 2 DNAME -------------------IT EXTC

SQL> insert into emp2 values (1,'Mohan',4); 1 row created. SQL> insert into emp2 values (2,'Kunal',2); 1 row created. Referential Integrity-Rule 2 **************************** You cannot delete a primary key from the parent table if a corresponding foreign key exists in the child table. SQL> delete dept; delete dept * ERROR at line 1: ORA-02292: integrity constraint (HR.EMP_FK) violated - child record found SQL> delete emp2 where eid=1; 1 row deleted. SQL> delete dept where did=4; 1 row deleted.

Vous aimerez peut-être aussi