Sei sulla pagina 1di 14

10/24/2015 Campus Commune

About

×
×

Contact Us

About Certifications

Hello user!

As you have successfully completed the Aspire course curriculum, you can now further your learning
process by signing up for a certification course.

Certifications establish credibility by serving as a proof that you have successfully completed a rigorous
online training and also serve as a token of recognition from well­established institutes. Completing one
or more certifications will give you an additional advantage during the Initial Learning Program.

Basic Certification

1.  Installing, Configuring, and Administering Microsoft Windows 7
2.  Installing, Configuring, and Administering Microsoft Windows 2008 Server
3.  Installing, Configuring, and Administering Microsoft Windows 2012 Server
4.  Red Hat Certified Engineer (RHCE) Certification
5.  Red Hat Certified Virtualization Administrator (RHCVA) Certification
6.  Cisco Certified Network Associate (CCNA) Certification
7.  ITIL 2011 Foundation
8.  Installing, Configuring, and Administering Microsoft SQL Server 2008
9.  Installing, Configuring, and Administering Microsoft SQL Server 2012
10.  Microsoft Certified Technology Specialist (MCTS): Microsoft Exchange Server 2010,
Configuration Certification
11.  MCTS: Administering and Deploying System Center 2012 Configuration Manager Certification

Leaderboards

Close
×

List of Badges

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 1/14
10/24/2015 Campus Commune

Close Genius
Intellect 
Guru
 Earn this! 
Unix/C++ Oracle Lounge
 Earn this!
 Earn this!
Oracle LogoutPre­ILP Home

1.Introduction to Information Management

2.SQL Joins and SubQueries

3.DBObjects & SQL Loader

4.Introduction to PLSQL

5.Named PLSQL Blocks

Q
 Course Completion Quiz
  

1.5. Constraints & Keys

1.1 Objective
•    Introduction
•    Constraint Types
•    Declaration Style
•    Primary Key
•    Unique Key
•    Foreign Key
•    Check
•    Not Null
•    Default

1.2 Course Content
1.2.1 Introduction     
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 2/14
10/24/2015 Campus Commune

•    SQL Constraints are used to specify rules for the data in the table, ensuring the data conforms to the
requirements defined. They are  critical to the integrity , flexibility and scalability of the data in the
table. 
•    Constraints can defined when the table is created (with the CREATE statement) or after the table is
created (with the ALTER statement). 
•    Every constraint is maintained with a name (CONSTRAINT_NAME) in the database. Hence when
we declare a constraint on a column , if we do not provide a constraint name , Oracle associates the
constraint with the name. 
•    The same constraint is name is not allowed within one user login. For example , a constraint name
c1_pk is created , constraints should not be defined again with the same name.

1.2.2 Constraint Types
There are six types of constraints available to provide rules to the tables.
•    PRIMARY KEY
•    UNIQUE
•    FOREIGN KEY
•    CHECK
•    NOT NULL
•    DEFAULT

1.2.3 Declaration Style     
Syntactically constraints can be defined on columns of tables in 2 different ways
•    Column Level (OR) In­line style : Constraint is defined as part of the definition of an individual
column or attribute. Constraints can be defined at column level using both the CREATE and ALTER
statements. They are usually specified when the  constraint is specific to the column only.
    Syntax:
    CREATE TABLE sample
    (col1 DATATYPE(size) CONSTRAINT_NAME,col2 DATATYPE(size)     CONSTRAINT_NAME,
…......);
•    Table Level (OR) Out­of­lilne style : Constraint is defined as part of the table definition. Constraints
can be defined at table level only with the help of CREATE statement. They are usually specified when
the constraint need to be applied on the combination of columns together.
    Syntax:
    CREATE TABLE sample
    (col1 DATATYPE(size), col2 DATATYPE(size),  col3 DATATYPE(size), 
    CONSTRAINT_NAME(col1), CONSTRAINT_NAME(col2),…......);

1.2.4 Primary Key
•    A PRIMARY KEY constraint uniquely identifies each record in the table. A PRIMARY KEY column
cannot have duplicate values.
•    A PRIMARY KEY column cannot have null values.
•    A PRIMARY KEY can consist of one or more columns.
•    Since PRIMARY KEY can be defined only once, if at all multiple columns need to be defined as a
PRIMARY KEY, it is possible. This is called as COMPOSITE PRIMARY KEY.

1.2.4.1 Restrictions
•    Only one PRIMARY KEY can be defined on a table.
•    PRIMARY KEY cannot be defined on the columns having LOB,LONG,LONG RAW,BFILE etc...
data types.
•    A Composite PRIMARY KEY can be defined only as a table level definition. A composite
PRIMARY KEY cannot have more than 32 columns.
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 3/14
10/24/2015 Campus Commune

1.2.4.2 Primary key with Create statement (Column Level)
A PRIMARY KEY constraint can be defined on a table with the help of a CREATE statement.
PRIMARY KEY can be defined immediately after the column definition.

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size) CONSTRAINT cons_name PRIMARY KEY,  col2
DATATYPE(size), col3 DATATYPE(size),........);

Ex: CREATE TABLE location( loc_id NUMBER(4) CONSTRAINT loc_pk PRIMARY KEY,
 street_address VARCHAR2(20), city VARCHAR2(15), state VARCHAR2(15), pincode NUMBER(6));

The above example specifies that every location must have a unique value to identify the location and
that should not be left blank.

1.2.4.3 Primary key with Create statement (Table Level)
A PRIMARY KEY can be defined as a table level definition once finishing all the column definitions.

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size),  col2 DATATYPE(size), col3 DATATYPE(size),
........, CONSTRAINT cons_name PRIMARY KEY(col1));

Ex: CREATE TABLE location ( loc_id NUMBER(4),  street_address VARCHAR2(20),   city
VARCHAR2(15), state VARCHAR2(15), pincode NUMBER(6) CONSTRAINT loc_pk PRIMARY
KEY(loc_id));

The above example specifies that every location must have a unique value to identify the location and
that should not be left blank.

1.2.4.4 Composite Primary key
A PRIMARY KEY can be defined on multiple columns if required. This type of defining on multiple
columns is called as composite PRIMARY KEY.

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size), col2 DATATYPE(size), ........, CONSTRAINT
cons_name PRIMARY KEY(col1,col2));

Ex: CREATE TABLE sales (sales_id NUMBER(6), cust_id NUMBER(4), prod_id NUMBER(5),  qnty
NUMBER(6), sold_date DATE, CONSTRAINT sales_pk PRIMARY KEY(sales_id,cust_id,prod_id));

The above example specifies that for a sales transaction need to happen , an individual sales_id , cust_id
to whom the products has been sold and prod_id for each and every product need to be given. Since all
the three attributes need to maintain the uniqueness and should not be null, a composite PRIMARY KEY
has been defined.

1.2.4.5 Primary key with Alter statement
Suppose a table has been created without a PRIMARY KEY and later it was identified to maintain a
PRIMARY KEY in the table , this can be achieved with the help of ALTER statement. If the existing
table consists of records , the table need to be taken as a back up and then need to be truncated. After
truncating the table , table structure can be altered.

Syntax:
ALTER TABLE table2 ADD CONSRAINT cons_name PRIMARY KEY(col1);
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 4/14
10/24/2015 Campus Commune

Ex: CREATE TABLE person ( aadhar_id VARCHAR2(10),  name VARCHAR2(30),  dob DATE,
phone_number NUMBER(12),  address VARCHAR2(30));
ALTER TABLE person ADD CONSTRAINT aadhar_pk PRIMARY KEY (aadhar_id);

The above example specifies that, the Central government wants to maintain the persons details in order
to issue the aadhar card. They have created a table with the person details. Since aadhar number should
be different for each and every individual , with the help of ALTER statement the aadhar_id is
maintained as a PRIMARY KEY.

In order to remove a PRIMARY KEY constraint which is defined on  a table , we need to drop the
constraint defined.

Syntax:
ALTER TABLE table1 DROP CONSTRAINT cons_name;

Ex: ALTER TABLE location DROP CONSTRAINT loc_pk;

From the above example we can observe that if at all the location table does not want to maintain a
PRIMARY KEY, that can be removed.

1.2.5 Unique Key
•    A UNIQUE KEY constraint allows to maintain uniqueness in the table column on which it is defined.
•    A column can have null values though it is defined as a UNIQUE.
•    A UNIQUE KEY constraint can consist of one or more columns.
•    When required to define multiple columns as UNIQUE KEY , a COMPOSITE UNIQUE constraint
can be defined.

1.2.5.1 Restrictions
•    Only one UNIQUE KEY constraint can be defined on a table.
•    UNIQUE KEY constraint cannot be defined on the columns having LOB,LONG,LONG RAW,BFILE
etc... data types.
•    Composite UNIQUE KEY constraint can be defined only as a table level definition.
•    A composite UNIQUE KEY constraint cannot have more than 32 columns.

1.2.5.2 Unique with Create statement (Column Level)
A UNIQUE KEY constraint can be defined on a table with the help of a CREATE statement. UNIQUE
KEY can be defined immediately after the column definition.

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size) CONSTRAINT cons_name UNIQUE, col2
DATATYPE(size), col3 DATATYPE(size),........);

Ex: CREATE TABLE supplier ( supp_id NUMBER(4) CONSTRAINT supid_unq UNIQUE, name
VARCHAR2(20),  contact_number NUMBER(15));

The above example specifies that every supplier must have a unique value to identify the supplier.

1.2.5.3 Unique with Create statement (Table Level)
A UNIQUE KEY constraint can be defined as a table level definition once finishing all the column
definitions.

Syntax :
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 5/14
10/24/2015 Campus Commune

CREATE TABLE table1 (col1 DATATYPE(size), col2 DATATYPE(size), col3 DATATYPE(size),
........, CONSTRAINT cons_name UNIQUE(col1));

Ex: CREATE TABLE supplier( supp_id NUMBER(4), name VARCHAR2(20), 
contact_number NUMBER(15), CONSTRAINT supid_unq UNIQUE(supp_id));

The above example specifies that every supplier must have a unique value to identify the supplier.

1.2.5.4 Composite Unique constraint
A UNIQUE KEY constraint can be defined on multiple columns if required. This type of defining on
multiple columns is called as composite UNIQUE constraint.

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size),  col2 DATATYPE(size), col3 DATATYPE(size),
........, CONSTRAINT cons_name UNIQUE(col1,col2));

Ex: CREATE TABLE warehouse(id NUMBER(6), name VARCHAR2(20), address VARCHAR2(20),
contact_number NUMBER(12), CONSTRAINT warehouse_unq UNIQUE(id,name));

The above example specifies that every warehouse need to have a specific id and specific name, which is
possible with composite UNIQUE constraint.

1.2.5.5 Unique with Alter statement
Suppose a table has been created without a UNIQUE KEY constraint and later it was identified to
maintain a UNIQUE constraint in the table , this can be achieved with the help of ALTER statement.

Syntax:
ALTER TABLE table2 ADD CONSRAINT cons_name UNIQUE(col1);

Ex:  CREATE TABLE new_sim_registration(name VARCHAR2(20), gender CHAR(1), dob DATE,
existing_contact_no NUMBER(12), new_sim_no NUMBER(12), address_proof VARCHAR2(10));
ALTER TABLE new_sim_registration ADD CONSTRAINT newsim_unq UNIQUE(new_sim_no);

The above example specifies that, whenever a customer wants to take a new sim card , the telephone
service provider will collect all the details of the customer and makes the new_sim_no as UNIQUE so
that the new_sim_no can be filled later once the address_proof submitted is verified.

In order to remove a UNIQUE KEY constraint which is defined on  a table , we need to drop the
constraint defined.

Syntax:
ALTER TABLE table1 DROP CONSTRAINT cons_name;

Ex: ALTER TABLE location DROP CONSTRAINT newsim_unq;

From the above example we can observe that if at all the government does not want to maintain a
UNIQUE constraint, that can be removed.

1.2.6 Foreign Key
•    A FOREIGN KEY means establishing the relationship between parent and child tables. (parent –
PRIMARY / UNIQUE defined table , child – FOREIGN KEY defined table)
•    A FOREIGN KEY in one table points to a PRIMARY KEY / UNIQUE in another table.
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 6/14
10/24/2015 Campus Commune

•    The PRIMARY KEY and the FOREIGN KEY can exist in the same table.
•    A composite FOREIGN KEY should be defined at table level. A composite FOREIGN KEY must
refer to the composite PRIMARY KEY / UNIQUE constraint.

1.2.6.1 Restrictions
•    The child and parent tables must be in the same database.
•    The PRIMARY KEY / UNIQUE and FOREIGN KEY fields must be of same data type.

1.2.6.2 Referential Integrity
•    It preserves the defined relationship between the tables when the records are inserted or deleted.
•    It ensures that key values are consistent across the tables.
•    When referential integrity is enforced , it prevents from
        1.     Adding records to a child table if there is no associated record     available in the parent table.
        2.     Changing values in the parent table is associated records available     in the child table.
        3.     Deleting records from the parent table if there are matching records     available in the child
table.

1.2.6.3 Foreign key with Create statement (Column Level)
A FOREIGN KEY constraint can be defined on a table with the help of a CREATE statement.
 FOREIGN KEY can be defined immediately after the column definition with the help of REFERENCES
keyword to refer the parent table.

Syntax :
CREATE TABLE table2 (col4 DATATYPE(size) CONSTRAINT cons_name REFERENCES
table1(col1),  col5 DATATYPE(size), col6 DATATYPE(size) , ........);

Ex: CREATE TABLE person(name VARCHAR2(30),dob DATE,gender CHAR(1),address
NUMBER(4) REFERENCES location(loc_id),gender CHAR(1));

The above example specifies that in order to maintain the address for each and every person, the
address's which are available in the location table are linked into the address field of the person table.

1.2.6.4 Foreign key with Create statement (Table Level)
A FOREIGN KEY constraint can be defined as a table level definition once finishing all the column
definitions.

Syntax :
CREATE TABLE table2 (col4 DATATYPE(size),  col5 DATATYPE(size), col6 DATATYPE(size),
........, CONSTRAINT cons_name FOREIGN KEY(col4) REFERENCES table1(col1));

Ex: CREATE TABLE person(name VARCHAR2(30),dob DATE,gender CHAR(1),address
NUMBER(4),gender CHAR(1),FOREIGN KEY(address) REFERENCES location(loc_id));

The above example specifies that in order to maintain the address for each and every person, the
address's which are available in the location table are linked into the address field of the person table.

1.2.6.5 Composite Foreign key
A FOREIGN KEY constraint can be defined on multiple columns if required. This type of defining on
multiple columns is called as composite FOREIGN KEY.

Syntax :
CREATE TABLE table2 (col4 DATATYPE(size),  col5 DATATYPE(size), col6 DATATYPE(size),
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 7/14
10/24/2015 Campus Commune

 ........, CONSTRAINT cons_name FOREIGN KEY (col4,col5) REFERENCES table1(col1,col2));

Ex: CREATE TABLE production(suppl_id NUMBER(4),warehouse_id NUMBER(6), warehouse_name
VARCHAR2(20),product_name VARCHAR2(20),qnty NUMBER(4),unit_price
NUMBER(6,2),CONSTRAINT suppl_fk  FOREIGN KEY(suppl_id) REFERENCES
supplied(supp_id),CONSTRAINT warehouse_fk (warehouse_id,warehouse_name) REFERENCES
(id,name));

The above example specifies the in order to maintain the production details, who is the supplier of the
products and in which warehouse they are been stored, the details of supplier and warehouse need to be
maintained which are in turn taken from the supplier table and warehouse table.

1.2.6.6 Foreign key with Alter statement
Suppose a table has been created without a FOREIGN KEY constraint and later it was identified to
maintain a FOREIGN KEY constraint in the table , this can be achieved with the help of ALTER
statement.

Syntax:
ALTER TABLE table2 ADD CONSRAINT cons_name FOREIGN KEY (col4) REFERENCES
table1(col1);

Ex:  CREATE TABLE person(name VARCHAR2(30),dob DATE,gender CHAR(1),address
NUMBER(4) ,gender CHAR(1));
ALTER TABLE person ADD CONSTRAINT location_fk FOREIGN KEY(address) REFERENCES
location(loc_id);

The above example specifies that in order to maintain the address for each and every person, the
address's which are available in the location table are linked into the address field of the person table.

In order to remove a FOREIGN KEY constraint which is defined on  a table , we need to drop the
constraint defined.

Syntax:
ALTER TABLE table1 DROP CONSTRAINT cons_name;

Ex:ALTER TABLE person DROP CONSTRAINT location_fk;
From the above example we can observe that if at all the government does not want to maintain a
FOREIGN KEY constraint, that can be removed.

1.2.6.7 Maintaining referential integrity

1.2.6.7.1    ON DELETE SET NULL

A foreign key with CASCADE DELETE means that if a record in the parent table is deleted , then the
corresponding records in the child table will be automatically deleted.

Syntax:
CREATE TABLE table2 (col4 DATATYPE(size),  col5 DATATYPE(size), ........, CONSTRAINT
cons_name FOREIGN KEY (col4,col5) REFERENCES table1(col1,col2) ON DELETE SET NULL);

Ex: CREATE TABLE production(suppl_id NUMBER(4),warehouse_id NUMBER(6), warehouse_name
VARCHAR2(20),product_name VARCHAR2(20),qnty NUMBER(4),unit_price
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 8/14
10/24/2015 Campus Commune

NUMBER(6,2),CONSTRAINT suppl_fk  FOREIGN KEY(suppl_id) REFERENCES supplied(supp_id),
CONSTRAINT warehouse_fk (warehouse_id,warehouse_name) REFERENCES (id,name) ON DELETE
SET NULL);

The above example specifies that if at all the records available in the warehouse table (id,name) are
deleted then automatically the records in the production table (warehouse_id , warehouse_name) will be
set to null.

Syntax:
ALTER TABLE table2 ADD CONSRAINT cons_name FOREIGN KEY (col4) REFERENCES
table1(col1) ON DELETE SET NULL;

Ex: CREATE TABLE person(name VARCHAR2(30),dob DATE,gender CHAR(1), address
NUMBER(4) ,gender CHAR(1));
ALTER TABLE person ADD CONSTRAINT location_fk FOREIGN KEY(address) REFERENCES
location(loc_id) ON DELETE SET NULL;

The above example specifies that if at all the records available in the location table (loc_id) are deleted
then automatically the records in the person table (address) will be set to null.

1.2.6.7.2    ON DELETE CASCADE
A FOREIGN KEY with SET NULL ON DELETE means that if a record in the parent table is deleted ,
then the corresponding records in the child table will have the foreign key fields set to null. The records
in the child table will not be deleted.

Syntax :
CREATE TABLE table2 (col4 DATATYPE(size) CONSTRAINT cons_name REFERENCES
table1(col1) ON DELETE CASCADE,col5 DATATYPE(size), col6 DATATYPE(size) , ........);

Ex: CREATE TABLE person(name VARCHAR2(30),dob DATE,gender CHAR(1),address
NUMBER(4) REFERENCES location(loc_id) ON DELETE CASCADE,gender CHAR(1));

The above example specifies that, If the data is deleted from the location table location id , automatically
they will be removed from person table address field.

Syntax:
CREATE TABLE table2 (col4 DATATYPE(size),  col5 DATATYPE(size), col6 DATATYPE(size),
 ........, CONSTRAINT cons_name FOREIGN KEY (col4,col5) REFERENCES table1(col1,col2) ON
DELETE CASCADE);

Ex: CREATE TABLE production(suppl_id NUMBER(4),warehouse_id NUMBER(6), warehouse_name
VARCHAR2(20),product_name VARCHAR2(20),qnty NUMBER(4),unit_price
NUMBER(6,2),CONSTRAINT suppl_fk  FOREIGN KEY(suppl_id) REFERENCES supplied(supp_id),
CONSTRAINT warehouse_fk (warehouse_id,warehouse_name) REFERENCES (id,name) ON DELETE
CASCADE);

The above example specifies that, If the data is deleted from the warehouse table id and name , automatic
deletion will happen in production table warehouse id and name.

Syntax:
ALTER TABLE table2 ADD CONSRAINT cons_name FOREIGN KEY (col4) REFERENCES
table1(col1) ON DELETE CASCADE;
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 9/14
10/24/2015 Campus Commune

Ex: CREATE TABLE person(name VARCHAR2(30),dob DATE,gender CHAR(1), address
NUMBER(4) ,gender CHAR(1));
ALTER TABLE person ADD CONSTRAINT location_fk FOREIGN KEY(address) REFERENCES
location(loc_id) ON DELETE CASCADE;

The above example specifies that , If the data is deleted from the location table location id , automatically
they will be removed from person table address field.

1.2.7 Check
•    CHECK constraint allows to specify conditions that each row in a column must satisfy.
•    CHECK constraint validates the values in a given column to meet the specified criteria.
•    CHECK constraint is used to limit the value range that can be placed in a column.

1.2.7.1 Restrictions
•    The CHECK constraint can refer any columns of the table , but cannot refer any columns of another
table.
•    CHECK constraint condition cannot include any view or sub query.

1.2.7.2 Check constraint with Create statement (Column Level)
A CHECK constraint can be defined on a table with the help of a CREATE statement.  

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size) CONSTRAINT cons_name CHECK (condition), col2
DATATYPE(size) , ........);
Ex:CREATE TABLE person(name VARCHAR2(10), dob DATE, gender CHAR(1) CONSTRAINT
gndr_chk CHECK(gender IN ('M','F'));

The above example specified in the gender fields only the values 'M' or 'F' only need to be entered.

1.2.7.3 Check constraint with Create statement (Table Level)
A CHECK constraint can be defined as a table level definition once finishing all the column definitions.

Syntax :
CREATE TABLE table1 (col1 DATATYPE(size),col2 DATATYPE(size), ........, CONSTRAINT
cons_name CHECK (condition),CONSTRAINT cons_name CHECK (condition));

Ex:CREATE TABLE person(name VARCHAR2(10), gender CHAR(1),dob DATE, CONSTRAINT
gndr_chk CHECK(gender IN ('M','F'));

The above example specifies that gender fields only the values 'M' or 'F' only need to be entered.

1.2.7.4 Check constraint with Alter statement 
Suppose a table has been created without a CHECK constraint and later it was identified to maintain a
CHECK constraint in the table , this can be achieved with the help of ALTER statement.

Syntax:
ALTER TABLE table2 ADD CONSRAINT cons_name CHECK (condition);

Ex: CREATE TABLE person(name VARCHAR2(10), gender CHAR(1),dob DATE);
ALTER TABLE person ADD CONSTRAINT gndr_chk CHECK(gender IN ('M','F'));

The above example specifies that gender fields only the values 'M' or 'F' only need to be entered.
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 10/14
10/24/2015 Campus Commune

In order to remove a CHECK constraint which is defined on  a table , we need to drop the constraint
defined.

Syntax:
ALTER TABLE table1 DROP CONSTRAINT cons_name;

Ex:ALTER TABLE person DROP CONSTRAIN gndr_chk;

From the above example we can observe that if at all the government does not want to maintain a
CHECK constraint for the gender field, that can be removed.

1.2.8 Not Null
•    NOT NULL are the in­line constraints which specify that a column cannot contain null values.
•    It should be defined at column level.
•    NOT NULL constraint specifies that the column cannot contain null values.

1.2.8.1 Not Null constraint with Create statement
Syntax :
CREATE TABLE table1 (col1 DATATYPE(size) CONSTRAINT cons_name NOT NULL,col2
DATATYPE(size) CONSTRAINT cons_name NOT NULL, col3 DATATYPE(size) , ........);

Ex:CREATE TABLE student (sid NUMBER(4) CONSTRAINT sid_pk PRIMARY KEY, sname
VARCHAR2(30) CONSTRAINT name_nn NOT NULL,dob DATE CONSTRAINT dob_nn NOT
NULL,gender CHAR(1) CONSTRAINT gndr­chk CHECK (gender ='M' OR gender='F'));

The above example specifies that student id should not contain any duplicate and null values , student
name and date of birth should always be entered with a  value and gender field is restricted with the
specified values.

1.2.8.2 Not Null constraint with Alter statement
Suppose a table has been created without a CHECK constraint and later it was identified to maintain a
CHECK constraint in the table , this can be achieved with the help of ALTER statement.

Syntax:
ALTER TABLE table2  MODIFY (col1 DATATYPE(size) NOT NULL);

Ex: CREATE TABLE student (sid NUMBER(4) CONSTRAINT sid_pk PRIMARY KEY, sname
VARCHAR2(30) ,dob DATE,gender CHAR(1) CONSTRAINT gndr­chk CHECK (gender ='M' OR
gender='F'));
ALTER TABLE student MODIFY(sname VARCHAR2(30) NOT NULL,dob DATE NOT NULL);

The above example specifies that student id should not contain any duplicate and null values , student
name and date of birth should always be entered with a  value and gender field is restricted with the
specified values.
In order to remove a NOT NULL constraint which is defined on  a table , we need to drop the constraint
defined.

Syntax:
ALTER TABLE table1 DROP CONSTRAINT cons_name;

Ex:ALTER TABLE person DROP CONSTRAIN dob_nn;

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 11/14
10/24/2015 Campus Commune

From the above example we can observe that if at all the government does not want to maintain a NOT
NULL constraint for the dob field, that can be removed.

1.2.9 Default
•    The DEFAULT constraint provides a default value to a column when the INSERT INTO statement
does not provide a specific value.
•    The default value will be added to all the records if no other values is specified.

1.2.9.1 Default constraint with Create statement
Syntax :
CREATE TABLE table1 (col1 DATATYPE(size) DEFAULT value,col2 DATATYPE(size) DEFAULT
value, col3 DATATYPE(size) , ........);

Ex: CREATE TABLE student (sid NUMBER(4), sname VARCHAR2(30), dob DATE,doa DATE
DEFAULT '10­MAR­13'));
INSERT INTO student VALUES(1,'A','10­MAR­86','21­MAR­13');
INSERT INTO student (sid,sname,dob) VALUES(2,'B','10­APR­85');
COMMIT;
SELECT * FROM student;
sid     sname    dob                   doa
1        A            10­MAR­86       21­MAR­13   
2        B            10­APR­85        10­MAR­13

The above example specifies that the date of admission of a student is by default the specified values.

1.2.9.2 Default constraint with Alter statement
Suppose a table has been created without a CHECK constraint and later it was identified to maintain a
CHECK constraint in the table , this can be achieved with the help of ALTER statement.

Syntax:
ALTER TABLE table2  MODIFY (col1 DATATYPE(size) NOT NULL);

Ex:CREATE TABLE student (sid NUMBER(4),sname VARCHAR2(30),dob DATE, gender
CHAR(1),doa DATE);
ALTER TABLE student MODIFY(doa DATE DEFAULT '10­MAR­13');

The above example specifies by default if the date of admission is not specified the specified value need
to be inserted.

1.2.10 Primary and Unique key differences

Test your knowledge by taking the Sample Quiz:

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 12/14
10/24/2015 Campus Commune

Related Videos

Ask a doubt  (Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

  
×

Quiz

Your Dashboard

Bharat Khatwani

 779 Miles

Current Course Progress

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 13/14
10/24/2015 Campus Commune

100

Overall Progress

100

 

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/constraints­and­keys 14/14

Potrebbero piacerti anche