Question:
Difference b/w primary key &unique+notnull in oracle?
varma
2007-12-01 01:48:00 UTC
Difference b/w primary key &unique+notnull in oracle?
Five answers:
dgitts
2007-12-01 02:34:19 UTC
with regard to databases in general:

primary key is the database table record/row identifier, there has to be someway to identify each record in a table...

unique can be used in a sql query to pull up only unique records.

not null is a field identifier meaning that that field within the record cannot contain null values.
Sadeesh P
2007-12-01 06:38:47 UTC
Integrity Constraints



Data integrity allows to define certain data quality requirements that the data in the database needs to meet. If a user tries to insert data that doesn't meet these requirements, Oracle will not allow so.



Constraint types



There are five integrity constraints in Oracle.



Not Null



A column in a table can be specified not null. It's not possible to insert a null in such a column. The default is null. So, in the following create table statement, a null can be inserted into the column named c.



create table ri_not_null (

a number not null,

b number null,

c number

);



insert into ri_not_null values ( 1, null, null);

insert into ri_not_null values ( 2, 3, 4);

insert into ri_not_null values (null, 5, 6);



The first to records can be inserted, the third cannot, throwing a ORA-01400: cannot insert NULL into ("RENE"."RI_NOT_NULL"."A").

The not null/null constraint can be altered with



alter table ri_not_null modify a null;



After this modification, the column a can contain null values.



Unique Key



The unique constraint doesn't allow duplicate values in a column. If the unique constraint encompasses two or more columns, no two equal combinations are allowed.



create table ri_unique (

a number unique,

b number

);



However, if a column is not explicitely defined as not null, nulls can be inserted multiple times:



insert into ri_unique values (4, 5);

insert into ri_unique values (2, 1);

insert into ri_unique values (9, 8);

insert into ri_unique values (6, 9);

insert into ri_unique values (null,9);

insert into ri_unique values (null,9);



Now: trying to insert the number 2 again into a:



insert into ri_unique values (2,7);



This statement issues a ORA-00001: unique constraint (RENE.SYS_C001463 violated). Every constraint, by the way, has a name. In this case, the name is: RENE.SYS_C001463.

In order to remove that constraint, an alter table ... drop constraint ... is needed:



alter table ri_unique drop constraint sys_c001463;



Of course, it is also possible to add a unique constraint on an existing table:



alter table ri_unique add constraint uq_ri_b unique (b);



A unique constraint can be extended over multiple columns:



create table ri_3 (

a number,

b number,

c number,

unique (a,b)

);



It is possible to name the constraint. The following example creates a unique constraint on the columns a and b and names the constraint uq_ri_3.



create table ri_3 (

a number,

b number,

c number,

constraint uq_ri_3 unique (a,b)

);



Primary Key



On a technical level, a primary key combines a unique and a not null constraint. Additionally, a table can have at most one primary key. After creating a primary key, it can be referenced by a foreign key.



create table ri_primary_key (

a number primary key,

b number

);



Primary keys can explicitely be named. The following create table statement creates a table with a primary key whose name is pk_name.



create table ri_primary_key_1 (

a number,

b number,

c number,

constraint pk_name primary key (a, b)

);



Foreign Key



A foreign key constraint (also called referential integrity constraint) on a column ensures that the value in that column is found in the primary key of another table.

If a table has a foreign key that references a table, that referenced table can be dropped with a drop table .. cascade constraints.

It is not possible to establish a foreign key on a global temporary table. If tried, Oracle issues a ORA-14455: attempt to create referential integrity constraint on temporary table.

Check

A check constraint allows to state a minimum requirement for the value in a column. If more complicated requirements are desired, an insert trigger must be used.

The following table allows only numbers that are between 0 and 100 in the column a;



create table ri_check_1 (

a number check (a between 0 and 100),

b number

);



Check constraints can be added after a table had been created:



alter table ri_check_1

add constraint ch_b check (b > 50);



It is also possible to state a check constraint that check the value of more than one column. The following example makes sure that the value of begin_ is smaller than the value of end_.



create table ri_check_2

begin_ number,

end_ number,

value_ number,

check (begin_ < end_)

);
saravanan R
2007-12-01 02:23:55 UTC
hi,

Primary key and unique are Entity integrity constraints



Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist and no null values are entered.



Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values. (In oracle, one null is not equal to another null).
sowmya
2007-12-01 02:07:10 UTC
Primary Key is the Both Unique and Not Null. It creates Index on table also.



But Unique means it doesn't allow duplicate values and It doesn't create Index. It allows Null value also



Not Null means It doesn't allow Null value.
2016-12-17 08:24:39 UTC
common Key: A column in a table whose values uniquely % out the rows interior the table. a typical key fee won't be able to be NULL. unique Key: unique Keys are used to uniquely % out each and each row in an Oracle table. There could be one and in basic terms one row for each unique key fee. Surrogate Key: A equipment generated key without organization fee. oftentimes carried out with database generated sequences. effortless Diffrence between common key and unique secret is the aptitude of unique key for storing null values, a typical key on a column gurantees that the column ought to incorporate a fee for a given row, yet a special key in basic terms ensures that a column could be unique. with the exception of this distinction , in oracle unique and common key creates distinction in overall performance e.g : a typical key on a table advance skill of using index on go with count quantity(*) from table_name on condition that the table is analyzed yet a special key won't we are able to define common key on in basic terms one column in a table. yet for unique key there is not any decrease. we are able to define as many columns as we choose. that's punctiliously incorrect common key could additionally incorporate diverse column.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...