Interview Answer

Its hard enough to find an error in your code when you are looking for it; its even harder when you ve assumed your code is error-free. Steve McConnell

Exaplin Oracle Constraints

January 1, 1970 12:00 am

Comments



Exaplin Oracle Constraints


Constraints apply specific rules to data for ensuring the data conforms to the requirements defined.

NOT Null Constraint

The not null Constraint do not allow null values into the table. The primay should not contain null values.

UNIQUE Constraint

The unique Constraint do not allow duplicate values into the table. The primay should not contain duplicate values.

Primary Key Constraint

The Primary Key constraint uniquely identifies each record in a database table. It will not allow null or unique data.

Foreign Key Constraint

A Foreign Key in one table points to a Primary Key in another table. IT helps to link the table.


Solution

   					 
												--NOT Null Constraint
												 Create table person
												(
												pno   integer,
												pname varchar2(20) not null
												);


												--UNIQUE Constraint
												Create table person
												(
												pno   integer,
												pname varchar2(20) constraint uq_pname unique
												);

												--Primary Key Constraint
												Create table person
												(
												pno   integer constraint pk_pno PRIMARY KEY,
												pname varchar2(20)
												);

												-- Foreign Key Constraint
												CREATE TABLE DEPT
												(
												DEPTNO   INTEGER PRIMARY KEY,
												DEPTNAME VARCHAR2(20) NOT NULL,
												LOCATION VARCHAR2(20)
												);

												CREATE TABLE EMP
												(
												EMPNO    INTEGER PRIMARY KEY,
												EMPNAME  VARCHAR2(20) NOT NULL,
												SALARY   NUMBER,
												DEPTNO   INTEGER constraint fk_deptno references DEPT(deptno)
												);
																    

Comments



Please login to add comments.