• Skip to primary navigation
  • Skip to main content

Tech Honey

The #1 Website for Oracle PL/SQL, OA Framework and HTML

  • Home
  • HTML
    • Tags in HTML
    • HTML Attributes
  • OA Framework
    • Item in OAF
    • Regions in OAF
    • General OAF Topics
  • Oracle
    • statement
    • function
    • clause
    • plsql
    • sql

sql

DROPPING, DISABLING and ENABLING CHECK CONSTRAINT in Oracle SQL – PLSQL

October 31, 2012 by techhoneyadmin

In Oracle SQL / PLSQL a CHECK CONSTRAINT allows us to specify a condition on each record / row of a table.
Important points about CHECK CONSTRAINT in Oracle SQL / PLSQL:

  1. In Oracle SQL / PLSQL a CHECK CONSTRAINT can never be defined on a VIEW but only on a table.
  2. A CHECK CONSTRAINT on a table can refer to columns of the same table and not of any other table.
  3. No sub-query is allowed in the CHECK CONSTRAINT.
  4. A CHECK CONSTRAINT can be created using CREATE TABLE or ALTER TABLE statement.

Syntax to DROP a CHECK CONSTRAINT in SQL / PLSQL is:

ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

Example:

view source
print?
1ALTER TABLE employee
2DROP CONSTRAINT employee_pk;

The above statement will drop the check constraint ‘employee_pk’ from the ‘employee’ table.


Disable a CHECK CONSTRAINT.

If we do not wish to delete the check constraint as we may need the same in future but for some time we want the check constraint not to function, then we can disable the check constraint.

Syntax for disabling the check constraint is:

ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;

Example:

view source
print?
1ALTER TABLE employee
2DISABLE CONSTRAINT employee_pk;

Here in the above ALTER statement we have disabled the CHECK CONSTRAINT ‘employee_pk’ on the ‘employee’ table.


Enabling a CHECK CONSTRAINT

We can enable a check constraint that has been disabled earlier, the syntax for enabling a check constraint is:

ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

Example:

view source
print?
1ALTER TABLE employee
2ENABLE CONSTRAINT employee_pk;

Here in the above statement we have enabled the check constraint ‘employee_pk’ in ‘employee’ table using ALTER TABLE statement.


Filed Under: sql Tagged With: CheckConstraintsPLSQL, dropping disabling and enabling a check constraint in oracle plsql, dropping disabling and enabling a check constraint in oracle sql, how to use dropping disabling and enabling a check constraint in oracle database query, how to use dropping disabling and enabling a check constraint in oracle plsql, how to use dropping disabling and enabling a check constraint in oracle sql, syntax and example of dropping disabling and enabling a check constraint in oracle database query, syntax and example of dropping disabling and enabling a check constraint in oracle plsql, syntax and example of dropping disabling and enabling a check constraint in oracle sql, using dropping disabling and enabling a check constraint in oracle database query, using dropping disabling and enabling a check constraint in oracle plsql, using dropping disabling and enabling a check constraint in oracle sql

CHECK CONSTRAINT in Oracle SQL – PLSQL

October 31, 2012 by techhoneyadmin

In Oracle SQL / PLSQL a CHECK CONSTRAINT allows us to specify a condition on each record / row of a table.

Important points about CHECK CONSTRAINT in Oracle SQL / PLSQL:

  1. In Oracle SQL / PLSQL a CHECK CONSTRAINT can never be defined on a VIEW but only on a table.
  2. A CHECK CONSTRAINT on a table can refer to columns of the same table and not of any other table.
  3. No sub-query is allowed in the CHECK CONSTRAINT.
  4. A CHECK CONSTRAINT can be created using CREATE TABLE or ALTER TABLE statement.

Syntax to create a CHECK CONSTRAINT in CREATE TABLE statement in Oracle SQL / PLSQL is:

CREATE TABLE table_name
(column_name1 datatype NULL/NOT NULL
,column_name2 datatype NULL/NOT NULL
,column_name3 datatype NULL/NOT NULL
.
.
,column_nameN datatype NULL/NOT NULL
,CONSTRAINT constraint_name CHECK (column_name condition ) [DISABLE];

Note: The DISABLE keyword is optional, if we create a CHECK CONSTRAINT with disable keyword then the constraint will be created but will not be enforced.

Syntax to create a CHECK CONSTRAINT in ALTER TABLE statement is :

ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE];

Let’s take an example for understanding:

Scenario 1:

Step 1: Suppose we want to create a table named ‘employee’ in the database as shown below, with ‘employee_id’ column having CHECK CONSTRAINT.

Employee_Id Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10

We can achieve the same as:

view source
print?
1CREATE TABLE employee
2(employee_id   NUMBER(10)     NOT NULL
3,employee_name VARCHAR2(500)  NOT NULL
4,salary        NUMBER(20)     NOT NULL
5,department    VARCHAR2(300)  NOT NULL
6,commission    NUMBER(20)
7,CONSTRAINT employee_pk CHECK (employee_id BETWEEN 101 AND 500)
8);

Here with the help of the above SQL CREATE statement we have created a table named ‘employee’ that has 5 columns namely ‘employee_id’, ‘employee_name’, ‘department’, ‘salary’ and ‘commission’.

Also we are having CHECK CONSTRAINT on ‘employee_id’ column which ensures that we do not enter any employee_id less than 101 or more than 500.

Step 2: Inserting the data in the ‘employee’ table.

view source
print?
1INSERT INTO employee
2VALUES (101,'Emp A',10000,'Sales',10);
3 
4INSERT INTO employee
5VALUES (102,'Emp B',20000,'IT',20);
6 
7INSERT INTO employee
8VALUES (103,'Emp C',28000,'IT',20);
9 
10INSERT INTO employee
11VALUES (104,'Emp D',30000,'Support',NULL);
12 
13INSERT INTO employee
14VALUES (105,'Emp E',32000,'Sales',10);

The above SQL INSERT statements will insert 5 rows in the ‘employee’ table.

Now, if we query the employee table as:

view source
print?
1SELECT *
2FROM employee;

We will get the following result:

Employee_Id Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10

Here we have successfully created ‘employee’ table with column ‘employee_id’ having UNIQUE CONSTRAINT and also inserted data in ‘employee’ table.
If we try to insert one more record of employee_id = 50 as:

view source
print?
1INSERT INTO employee
2VALUES (50,'Emp F',40000,'Sales',30);

We get an error that check constraint is getting violated and the data cannot be entered, this is because ‘employee_id’ column has CHECK CONSTRAINT that enforces a condition that the ‘employee_id’ entered must be greater than 100 and lesser than 500 .


Scenario 2:

Using ALTER TABLE statement to create CHECK CONSTRAINT on a table
As mentioned earlier that an ALTER TABLE statement can be used to create CHECK CONSTRAINT on a table.

The syntax to create CHECK CONSTRAINT using ALTER TABLE statement is:

ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE];

Example:

view source
print?
1ALTER TABLE employee
2ADD CONSTRAINT employee_pk CHECK(employee_id BETWEEN 101 AND 500);

Here we have altered the ‘employee’ table and have successfully created CHECK CONSTRAINT on ‘employee_id’ column.


Filed Under: sql Tagged With: check constraint in oracle plsql, check constraint in oracle sql, CheckConstraintsPLSQL, how to use check constraint in oracle database query, how to use check constraint in oracle plsql, how to use check constraint in oracle sql, syntax and example of check constraint in oracle database query, syntax and example of check constraint in oracle plsql, syntax and example of check constraint in oracle sql, using check constraint in oracle database query, using check constraint in oracle plsql, using check constraint in oracle sql

DROPPING, DISABLING and ENABLING UNIQUE CONSTRAINT in Oracle SQL – PLSQL

October 31, 2012 by techhoneyadmin

In Oracle SQL / PLSQL a UNIQUE CONSTRAINT is single or group of column(s)/field(s) that can uniquely identify each record in a table.

Some of the column(s) / field(s) of unique constraint can have NULL values until the combination can uniquely identify each record of table.

Important points about UNIQUE CONSTRAINT in Oracle SQL / PLSQL:

  1. In Oracle SQL / PLSQL a UNIQUE CONSTRAINT can be created by using maximum of 32 columns / fields.
  2. A UNIQUE CONSTRAINT can be dropped, disabled and enabled in ALTER TABLE statement.

Syntax to DROP a UNIQUE CONSTRAINT in SQL / PLSQL is:

ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

Example:

view source
print?
1ALTER TABLE employee
2DROP CONSTRAINT employee_pk;

The above statement will drop the unique constraint ‘employee_pk’ from the ‘employee’ table.


Disable a UNIQUE CONSTRAINT.

If we do not wish to delete the unique constraint as we may need the same in future but for some time we want the unique constraint not to function, then we can disable the unique constraint

Syntax for disabling the unique constraint in Oracle SQL / PLSQL is:

ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;

Example:

view source
print?
1ALTER TABLE employee
2DISABLE CONSTRAINT employee_pk;

Here in the above ALTER statement we have disabled the UNIQUE CONSTRAINT ‘employee_pk’ on the ‘employee’ table.


Enabling a UNIQUE CONSTRAINT

We can enable a unique constraint that has been disabled earlier, the syntax for enabling a unique constraint in Oracle SQL / PLSQL is:

ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

Example:

view source
print?
1ALTER TABLE employee
2ENABLE CONSTRAINT employee_pk;

Here in the above statement we have enabled the unique constraint ‘employee_pk’ in ‘employee’ table using ALTER TABLE statement.


Filed Under: sql Tagged With: dropping disabling and enabling a unique constraint in oracle plsql, dropping disabling and enabling a unique constraint in oracle sql, how to use dropping disabling and enabling a unique constraint in oracle database query, how to use dropping disabling and enabling a unique constraint in oracle plsql, how to use dropping disabling and enabling a unique constraint in oracle sql, syntax and example of dropping disabling and enabling a unique constraint in oracle database query, syntax and example of dropping disabling and enabling a unique constraint in oracle plsql, syntax and example of dropping disabling and enabling a unique constraint in oracle sql, UNIQUECONSTRAINTPLSQL, using dropping disabling and enabling a unique constraint in oracle database query, using dropping disabling and enabling a unique constraint in oracle plsql, using dropping disabling and enabling a unique constraint in oracle sql

UNIQUE CONSTRAINT in Oracle SQL – PLSQL

October 31, 2012 by techhoneyadmin

In Oracle SQL / PLSQL a UNIQUE CONSTRAINT is single or group of column(s)/field(s) that can uniquely identify each record in a table.

Some of the column(s) / field(s) of unique constraint can have NULL values until the combination can uniquely identify each record of table.

Important points about UNIQUE CONSTRAINT in Oracle SQL / PLSQL:

  1. In Oracle SQL / PLSQL a UNIQUE CONSTRAINT can be created by using maximum of 32 columns / fields.
  2. A UNIQUE CONSTRAINT can be defined in CREATE TABLE or ALTER TABLE statement.

Syntax to create a UNIQUE CONSTRAINT in CREATE TABLE statement is:

CREATE TABLE table_name
(column_name1 datatype NULL/NOT NULL
,column_name2 datatype NULL/NOT NULL
,column_name3 datatype NULL/NOT NULL
.
.
,column_nameN datatype NULL/NOT NULL
,CONSTRAINT constraint_name UNIQUE (column_name1, column_name2, . . . ,column_nameN );

Syntax to create a UNIQUE CONSTRAINT in ALTER TABLE statement is:

ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column_name1, column_name2, . . . ,column_nameN );

Let’s take an example for understanding:

Scenario 1:

Step 1: Suppose we want to create a table named ‘employee’ in the database as shown below, with ‘employee_id’ column having UNIQUE CONSTRAINT.

Employee_Id Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10

We can achieve the same as:

view source
print?
1CREATE TABLE employee
2(employee_id   NUMBER(10)      NOT NULL
3,employee_name VARCHAR2(500)   NOT NULL
4,salary        NUMBER(20)      NOT NULL
5,department    VARCHAR2(300)   NOT NULL
6,commission    NUMBER(20)
7,CONSTRAINT employee_pk UNIQUE (employee_id)
8);

Here with the help of the above SQL CREATE statement we have created a table named ‘employee’ that has 5 columns namely ‘employee_id’, ‘employee_name’, ‘department’, ‘salary’ and ‘commission’. Also we are having UNIQUE CONSTRAINT on ‘employee_id’ column of the ‘employee’ table.

Step 2: Inserting the data in the ‘employee’ table.

view source
print?
1INSERT INTO employee
2VALUES (101,'Emp A',10000,'Sales',10);
3 
4INSERT INTO employee
5VALUES (102,'Emp B',20000,'IT',20);
6 
7INSERT INTO employee
8VALUES (103,'Emp C',28000,'IT',20);
9 
10INSERT INTO employee
11VALUES (104,'Emp D',30000,'Support',NULL);
12 
13INSERT INTO employee
14VALUES (105,'Emp E',32000,'Sales',10);

The above SQL INSERT statements will insert 5 rows in the ‘employee’ table.

Now, if we query the employee table as:

view source
print?
1SELECT *
2FROM employee;

We will get the following result:

Employee_Id Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10

Here we have successfully created ‘employee’ table with column ‘employee_id’ having UNIQUE CONSTRAINT and also inserted data in ‘employee’ table.

If we try to insert one more record of employee_id = 105 as:

view source
print?
1INSERT INTO employee
2VALUES (105,'Emp F',40000,'Sales',30);

We get an error that unique constraint is getting violated and the data cannot be entered, this is because ‘employee_id’ column has UNIQUE CONSTRAINT and for employee_id = 105 we already have a record in ‘employee’ table and hence one more record of the same key cannot be entered.


Scenario 2:

Creating a combination of columns as UNIQUE CONSTRAINT

Suppose we want to create a table named ‘employee’ in the database as shown below, with combination of ‘employee_id’ and ‘employee_name’ as the UNIQUE CONSTRAINT columns for the ‘employee’ table

Employee_Id Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10

We can achieve the same as:

view source
print?
1CREATE TABLE employee
2(employee_id   NUMBER(10)       NOT NULL
3,employee_name VARCHAR2(500)    NOT NULL
4,salary        NUMBER(20)       NOT NULL
5,department    VARCHAR2(300)    NOT NULL
6,commission    NUMBER(20)
7,CONSTRAINT employee_pk UNIQUE (employee_id,employee_name)
8);

Let’s insert some data in the newly created ‘employee’ table

view source
print?
1INSERT INTO employee
2VALUES (101,'Emp A',10000,'Sales',10);
3 
4INSERT INTO employee
5VALUES (102,'Emp B',20000,'IT',20);
6 
7INSERT INTO employee
8VALUES (103,'Emp C',28000,'IT',20);
9 
10INSERT INTO employee
11VALUES (104,'Emp D',30000,'Support',NULL);
12 
13INSERT INTO employee
14VALUES (105,'Emp E',32000,'Sales',10);

The above SQL INSERT statements will insert 5 rows in the ‘employee’ table.

Now, if we query the employee table as:

view source
print?
1SELECT *
2FROM employee;

We will get the following result:

Employee_Id Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10

Suppose we want to insert one more record with ‘employee_id = 105’ and ‘employee_name = Emp F’ then we can do this and oracle will not give any error because the combination of ‘employee_id’ and ‘employee_name’ is still unique.


Scenario 3:

Using ALTER TABLE statement to create UNIQUE CONSTRAINT on a table

As mentioned earlier that an ALTER TABLE statement can be used to create UNIQUE CONSTRAINT on a table.
The syntax to create UNIQUE CONSTRAINT using ALTER TABLE statement is:

ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column_name1,column_name2, . . column_nameN);

Example:

view source
print?
1ALTER TABLE employee
2ADD CONSTRAINT employee_pk UNIQUE (employee_id);

Here we have altered the ‘employee’ table and have successfully created UNIQUE CONSTRAINT on ‘employee_id’ column.

We can also create UNIQUE CONSTRAINT by a combination of columns using the ALTER TABLE statement

Example:

view source
print?
1ALTER TABLE employee
2ADD CONSTRAINT employee_pk UNIQUE (employee_id, employee_name);

Here we have altered the ‘employee’ table and have successfully created UNIQUE CONSTRAINT on ‘employee_id’ and ‘employee_name’ column.


Filed Under: sql Tagged With: how to use unique constraint in oracle database query, how to use unique constraint in oracle plsql, how to use unique constraint in oracle sql, syntax and example of unique constraint in oracle database query, syntax and example of unique constraint in oracle plsql, syntax and example of unique constraint in oracle sql, unique constraint in oracle plsql, unique constraint in oracle sql, UNIQUECONSTRAINTPLSQL, using unique constraint in oracle database query, using unique constraint in oracle plsql, using unique constraint in oracle sql

Oracle/SQL DROP, DISABLE, ENABLE PRIMARY Key

October 31, 2012 by techhoneyadmin

Oracle SQL allows to DROP PRIMARY KEY, DISABLE and ENABLE a PRIMARY KEY Constraint using the SQL ALTER TABLE Statement.

In other words we can say that, whenever we want to Drop, Disable or Enable a PRIMARY KEY in SQL, we have to use the ALTER TABLE Statement.


Important points about PRIMARY KEYS in Oracle PL/SQL:

  1. A table can have one and only one primary key.
  2. In Oracle SQL / PLSQL a primary key can be created by using maximum of 32 columns / fields.
  3. The columns / fields that are used to define a primary key on table cannot have NULL values.
  4. A primary key can be dropped, disabled and enabled in ALTER TABLE statement.

DROP PRIMARY KEY Constraint Syntax

ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

DROP PRIMARY KEY Constraint Example

The below example will DROP the PRIMARY KEY ‘employee_pk’ from ‘employee’ table.

ALTER TABLE employee
DROP CONSTRAINT employee_pk;

DISABLE PRIMARY KEY Constraint Syntax

ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;

DISABLE PRIMARY KEY Constraint Example

The below example will DISABLE the PRIMARY KEY ‘employee_pk’ from ‘employee’ table.

ALTER TABLE employee
DISABLE CONSTRAINT employee_pk;

To DISABLE a PRIMARY key can be a handy option if we wish to use the same PRIMARY key in near future.


ENABLE PRIMARY KEY Constraint Syntax

ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

ENABLE PRIMARY KEY Constraint Example

The below example will ENABLE the PRIMARY KEY ‘employee_pk’ of ‘employee’ table

ALTER TABLE employee
ENABLE CONSTRAINT employee_pk;

We can ENABLE only those PRIMARY KEYS which have been DISABLED earlier


Filed Under: sql Tagged With: dropping disabling and enabling a primary key in oracle plsql, dropping disabling and enabling a primary key in oracle sql, how to use dropping disabling and enabling a primary key in oracle database query, how to use dropping disabling and enabling a primary key in oracle plsql, how to use dropping disabling and enabling a primary key in oracle sql, PRIMARYKEYPLSQL, syntax and example of dropping disabling and enabling a primary key in oracle database query, syntax and example of dropping disabling and enabling a primary key in oracle plsql, syntax and example of dropping disabling and enabling a primary key in oracle sql, using dropping disabling and enabling a primary key in oracle database query, using dropping disabling and enabling a primary key in oracle plsql, using dropping disabling and enabling a primary key in oracle sql

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Page 5
  • Go to Next Page »

Copyright © 2025 · Parallax Pro on Genesis Framework · WordPress · Log in