a) Constraint is a rule or restrictions which is imposed on the table data. For a new table, constraints can be specified at the create table syntax. For an existing table, constraints can be added by using alter table syntax. It is used to enforce business rules on the data.
2) Why we use constraints? a) if we want to enforce rules whenever a row is inserted, deleted or updated from that table i.e for every operation on the table data to succeed, constraint must be satisfied first.
3) List of constraints ?
a) NOT NULL b) UNIQUE C) CHECK d) PRIMARY KEY e) FORIEGN KEY and f) ON DELETE.
3) Different ways to add constraints? a) Column Level b) Table level and c) Alter level.
Whatever the way we choose, constraints are always attached to columns not to a table.
4) Constraint name should be unique to the owner. Format for the constraint name is TABLENAME_CONSTRAINT TYPE_COLNAME.
5) While adding the constraint, we don't need to specify the name. Oracle will automatically generate the name of the constraint. If we want to mention the name of the constraint then we need to use CONSTRAINT clause along with the constraint name.
Tables: 1) USER_CONSTRAINTS select constraint_name,constraint_type,search_condition from user_constraints; 2) DBA_CONSTRAINTS select constraint_name,constraint_type,search_condition from dba_constraints; 3) ALL_CONSTRAINTS select constraint_name,constraint_type,search_condition from all_constraints;
NOT NULL Constraint:- 1) Is used to avoid null values for a specific column. 2) We can add NOT NULL only at column level.
Eg:-
CREATE TABLE my_details(std_name VARCHAR2(10) NOT NULL ,mobile_num NUMBER ,Qualification VARCHAR2(20) )'
CREATE TABLE my_details(std_name VARCHAR2(20) CONSTRAINT mydetails_NN NOT NULL, ,mobile_num NUMBER ,Qualification VARCHAR2(20) )'
CHECK Constraint:- It is used to insert the values into the table based on specified condition for each record. It can be add at three levels:- 1) Column level 2) Alter level and 3) Table level.
Column Level:-
a) Without name:-
CREATE TABLE my_details(std_name varchar2(100),city varchar2(10) CHECK (city='Hyderabad'),course varchar2(10)); b) With name:- CREATE TABLE my_details(std_name varchar2(100),city varchar2(10) CONSTRAINT std_ck CHECK (city='Hyderabad'),course varchar2(10)); Table Level:- a) Without name:
ALTER TABLE tata_motors ADD PRIMARY KEY(model_name));
ALTER TABLE tata_motors ADD CONSTRAINT motor_pk PRIMARY KEY (model_name));
To drop a constraint ALTER TABLE tata_motors DROP CONSTRAINT motor_pk PRIMARY KEY(model_name);
FOREIGN KEY Constraint:-
Foreign keys are used to refer the parent table primary key column which does not allow duplicates. The Foreign key constraint provides referential integrity rules (either within a table or between tables). i.e. We can only place a value in TABLE B if the values exist as a primary key in TABLE A. For parent child relationships across tables, foreign key is always attached to the child table. We use foreign key if we want to ensure that for every child table record there is a reference in parent table.
We can add this constraint in all three levels. Column Level Table Level Alter Level