Saturday 11 February 2017

Oracle uses integrity constraint to prevent invalid data entry into base tables of the database.
You can define integrity constraint to enforce business rule that are associated with information in a database.
If any one of the result of a DML statement execution violate an integrity constraint,oracle rollback the statement and return error.
Business rule specify condition and relationship that must always be true or false
For example each company define its own policy about salaries,employee number.inventory  tracking
and so on.For example Salary Column should not have numeric value greter than 10,0000.

Type of Constraints
1.Not Null constraint
2.Unique Key Constraint
3.Primary Key constraint
4.Foreign Key Constraint
5.Check Constraint

Can foreign key column accept null values and duplicate values?

 YES 

Can we create a primary key constraint on 2 columns and foreign key on other column is it possible?

 We can create composite Primary key on 2 columns and foreign key on other  column.

Can we use truncate and delete command to disable the referential integrity constraint?

 No We can not use tru and delete command to disable the referential integrity
   
Can you modify a null column to not null column, if table has some data?
      
 YES,We can use alter table command.
 ALTER TABLE mytable MODIFY mycolumn NOT NULL
     
Can you put more than one constraint to a column?

 YES.We can put more than one constraint on column
    
Difference between column level constraints & table level constraints:

 Column Level: In this method we are defining constraint for individual column  That is when ever  we are creating  a column we are specify the constraint  type Composite key can not be defined at column level
  
 syntax:  Create table Sample (SR_NO number primary key);
   
 Table Level: In this method we are defining constraint on group of columns that  is first we defining all columns and at the last we must constraint type on group  of column.Not null constraint can not be defined at table level
 
 Create table Sample (SR_NO number, primary key(SR_NO));
      
 Can you use sysdate in check constraints?Why? 

 No, you can't use sysdate in check constraints. 

 All rows in a table for an enabled constraint must return true for its expression. 
 But sysdate is non-deterministic. You can get a different result every time you  call it. So the outcome (true/false) can (will) change over time. So Oracle can't  guarantee that the expression is always true for every row. 

Define primary key and foreign key in Oracle

Primary Key

  • Primary key does't allow duplicate and null Values.
  • Primary ley is combination of UNIQUE and NOT NULL.
  • ONLY ONE PRIMARY key is allowed per tabel.
  • Primary key can be declared for combination of columns then it is called Composite Primary Key.
  • Composite Key Consist of 32 Column.
  • A table can have only one primary Key.
  • Primary can not be implemented on column having long,lob,long raw,varray,nested table,object,bfile,timestamp with timezone
Syntax; 

<column name ><data type>(<size>) primary key  

Foreign Key:
  • Foreign Key is used to refer the parent table primary key column which does not allow null value.
  • The foreign key constraint provide referential integrity rules (either within table or between table) i.e we can place value in table B if value exist in table A
  • Foreign key is attached to Child table.
      Foreign Key(Column name1,column name2)  references parent table(                 column1,column2);

Difference between primary and unique 

1,We can create  one primary key to table whereas we can create more then one unique key on table.
2.Primary key do not allow null values whereas unique allow null value
3.Primary key created using  keyword PRIMARY whereas unique key created using UNIQUE.

Have you ever create sequence on primary key column?

No.

How to add a Foreign key in existing table?

We can add foreign key on existing table using ALTER TABLE Command.

ALTER TABLE <table name> add foreign key(child_column_name) references parent table(primary key column);

How to add foreign key in a table after the creation?

Createt table emp(eno number,ename varchar2(100),sal number(5),foreign key(deptno) references dept1(depno);

Explain integrity constraint violated, and there are no records in child table then can we delete parent table records?

Oracle uses integrity constraint to prevent invalid data entry into base tables of the database.
You can define integrity constraint to enforce business rule that are associated with information in a database.
If any one of the result of a DML statement execution violate an integrity constraint,oracle rollback the statement and return error.
Business rule specify condition and relationship that must always be true or false
For example each company define its own policy about salaries,employee number.inventory  tracking
and so on.For example Salary Column should not have numeric value greter than 10,0000.

If there are no records in child table then can we delete parent table records.

I have create a composite primary key on two columns and inserting on value and null value, is it possible, and create a unique index on other columns, inserting 3 rows and inserting 3 null value  on unique column, is it possible

Its not Possible when  when we try insert  value and null value on composite column.
we can insert null values into unique key column

If you created primary key which index created and if you created index which constraint created automatically?.

When  we create Primary Key on column,Unique index automatically created.
When We create index on column, Unique key create automatically 

No comments:

Post a Comment