Class 05
Class 05
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question: Can I create a foreign key against only part of a composite primary key?
Answer: NO
--> Why this error coming? Go to base concept that FK of one table refer to primary/unique key of another table. Here in above
example we created PK as a composite PK PRIMARY KEY (TypeID, AltID), so while creating FK, it should refer to both as a
composite, not as a part. but we are doing REFERENCES StuffType(TypeID)
composite key
A composite key is a key (primary or alternate) that consists of more than one column.
create table D (
ID numeric(1),
CODE varchar(2),
constraint PK_D primary key (ID, CODE)
);
This is a controversial point. If my table with a composite primary key is expected to have millions of rows, the index controlling the
composite key can grow up to a point where CRUD operation performance is very degraded. In that case, it is a lot better to use a simple
integer ID primary key whose index will be compact enough and establish the necessary DBE constraints to maintain uniqueness.
=========================================================================================
Constraints: (when ever we have to strict user over the database level, we have to use constraints)
Oracle Constraints
Oracle constraints are critical to the scalability, flexibility and integrity of your database data.
Constraints apply specific rules to data, ensuring the data conforms to the requirements defined.
There are a number of different kinds of constraints that you will be concerned with as a DBA. These
are:
1. Check
2. Not NULL
3. Primary key
4. Unique
5. Foreign Key
--> NOT NULL constrains strict users that we cannot insert NULL values in that column.
Insert Into my_status values (1,1,'X','43435'); --> This will give you check constraint violated
error, because we created a check constraint on column active_record and it should accept the values
'T' and 'F' only. here we are trying to insert 'X' which is not acceptable.
Example, you can explain in interview, that only age> 18 allowed in factory
not allowing value 10 in the age column, because we created a check constraint that always check the
value before insertion and it must be > 18.
If you need to add a check constraint to a table after the table creation, simply use the alter
table command. Here is an example:
====================================================================================================
============
Question:
Answer:
We have to create a check constraint, which will validate the record before insert.
Answer='B' or
Answer='C' or
Answer='D' or
Answer='P'
)
);
This will inserted, because values in Answer column is A, but if try to insert any other value other
than ‘A’,’B’,’C’,’D’,’P’ Then it will not allow:
*Cause: The values being inserted do not satisfy the named check
=================================================================================================