EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Constraints
Secondary Sidebar
Oracle Tutorial
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle Constraints

By Priya PedamkarPriya Pedamkar

Oracle Constraints

Introduction to Oracle Constraints

Oracle Constraints clause provides data integrity to the data that is being used by the application from the database by applying certain rules or conditions on a column of a database table which will define a very basic behavioral layer on the column of that particular table to check the sanctity of the data flowing into it like NOT NULL constraint on the column will not allow any data of that column to be NULL as it will not allow users to insert NULL data into the column.

Types of Oracle Constraints

Oracle has multiple types of constraints for multiple purposes. In this section, we are going to go through the different types of constraints in Oracle.

1. NOT NULL

If we just add a column, by default the column is allowed to hold NULL values but in case there is a requirement that the column should not hold any NULL values. We can use NOT NULL constraint on that particular column. This will enforce the column to always have a value and it will not allow any NULL record to be added in the column. We will add NOT NULL constraint using both CREATE TABLE and ALTER TABLE constraint. We will create a table STUDENT with student_id, LastName and zfirstName columns having NOT NULL constraint.

Let us look at the query.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Query:

CREATE TABLE STUDENT (
student_id int NOT NULL,
FirstName varchar(255) NOT NULL,
LastName varchar(255) NOT NULL,
Age int
);

Let us now the query in SQL Developer and look at the result.

Output:

Oracle Constraints Example 1

As you can see from the above screenshot we have successfully created the table with NOT NULL constraints. Let us now look at the ALTER statement query to add NOT NULL constraint to the age column of STUDENT TABLE.

Query:

ALTER TABLE STUDENT MODIFY AGE int NOT NULL;
desc STUDENT;

Let us now run the query in the SQL developer.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,354 ratings)

Output:

Oracle Constraints Example 1

As we can see from the output the column AGE has now NOT NULL constraint added to it.

2. UNIQUE

This constraint in Oracle ensures that all the values of the column are different from each other and there are no duplicates. We will again use two examples to understand. First using the CREATE statement and then using ALTER statement. We will create a table STUDENT with student_id having UNIQUE constraint.

Let us look at the query using the CREATE TABLE statement.

Query:

CREATE TABLE student (
student_id int NOT NULL UNIQUE,
FirstName varchar(25) NOT NULL,
LastName varchar(25),
Age int
);

Let us now run the above query in SQL developer.

Output:

Oracle Constraints Example 2

As we can see UNIQUE constraint has been added to the columns of the table. Now let add a UNIQUE constraint to age column using ALTER TABLE statement.

Query:

ALTER TABLE STUDENT ADD UNIQUE(age);

Let us run this query in SQL developer.

Output:

Oracle Constraints Example 2

As we can see the table student has been successfully altered.

3. PRIMARY KEY

Primary key constraint uniquely describes each value of a column. No duplicates or NULL value is allowed. One important point is that a table can have only one primary key which in itself can be a combination of single or multiple fields.

We will first create a table ‘student’ with student_id as the primary key using the CREATE TABLE statement.

Query:

CREATE TABLE STUDENT (
student_id int PRIMARY KEY,
FirstName varchar(255) NOT NULL,
LastName varchar(255),
Age int
);

Now, let us run the query in SQL Developer and check the result.

Output:

Oracle Constraints Example 3

As we can see the Table STUDENT has been successfully created. Let us now add primary Key using the ALTER TABLE statement to student_id column after the STUDENT table has been created.

Query:

ALTER TABLE STUDENT
ADD PRIMARY KEY (student_id);

Let us now run the query in SQL developer and check the result.

Output:

PRIMARY KEY Example 3

As we can see the table STUDENT has been successfully altered.

4. FOREIGN KEY Constraints

A foreign key is a field which refers to the PRIMARY KEY of another table and the table which actually has the foreign key is called child table. Let us now create a table order which we has student_id column as a foreign key refrencing student_id column of student table using CREATE TABLE statement.

Query:

CREATE TABLE Orders(
OrderID int PRIMARY KEY,
OrderNumber int NOT NULL,
student_id int REFERENCES student(student_id)
);

Let us now run the query in SQL developer and look at the result.

Output:

FOREIGN KEY Example 4

As we can see the orders table has been successfully created. Now we will use the ALTER TABLE statement to add a foreign key to the student_id column of orders table.

Query:

ALTER TABLE Orders
ADD FOREIGN KEY (student_id) REFERENCES student(student_id);

Let us run the query in SQL developer and look at the result.

Output:

FOREIGN KEY Example 4

As we can see the table orders has been successfully altered.

5. CHECK Constraint

The CHECK constraint is used to limit the value of the range that can be placed in a column. In case we want to restrict certain values in a column or a table we introduce the check constraint. We will introduce the CHECK constraint using both CREATE TABLE and ALTER TABLE statement.

We are going to create a table student with age column having a check constraint.

Query:

CREATE TABLE student (
student_id int NOT NULL,
FirstName varchar(25) NOT NULL,
LastName varchar(25),
Age int CHECK (Age>=18)
);

Let us run the query in SQL developer and look at the result.

Output:

CHECK Example 5

As we can see the table has been successfully created with the CHECK constraint. Let us now add CHECK constraint on the same column after the table student has been created using ALTER TABLE statement.

Query:

ALTER TABLE student ADD CHECK (Age>=19);

Let us now run the query in SQL developer and see the result.

Output:

CHECK Example 6

As we can see the CHECK constraint has been added to the table.

Recommended Articles

This is a guide to Oracle Constraints. Here we discuss the definition of Constraints in Oracle database. We also discussed the various types that can be used in Oracle database along with examples. You can also go through our suggested articles to learn more –

  1. Introduction to Oracle Aliases
  2. How Does Left Join Works in Oracle?
  3. What Is Oracle Database | Applications
  4. To 10 Oracle Database Interview Questions
  5. Guide to MySQL Constraints
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more