Constraints
Constraints
-----------
Constraints are used to prevent invalid data entry or deletion if there are
dependencies.
These are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the database.
Constraints can be created either at the same time as the table is created or after
the table has been created.
Constraints can be defined at the column or table level.
Constraint defined for a specific table can be viewed by looking at the USER-
CONSTRAINTS data dictionary table.
You can define any constraint at the table level except NOT NULL which is defined
only at column level.
NOT NULL: NOT NULL Constraint ensures that the column contains no null values.
UNIQUE KEY: UNIQUE Key Constraint ensures that every value in a column or set of
columns must be unique, that is, no two rows of a table can have duplicate values
in a specified column or set of columns. If the UNIQUE constraint comprises more
than one column, that group of columns is called a Composite Unique Key. There can
be more than one Unique key on a table. Unique Key Constraint allows the input of
Null values. Unique Key automatically creates index on the column it is created.
PRIMARY KEY: Uniquely identifies each row in the Table. Only one PRIMARY KEY can be
created for each table but can have several UNIQUE constraints. PRIMARY KEY ensures
that no column can contain a NULL value. A Unique Index is automatically created
for a PRIMARY KEY column. PRIMARY KEY is called a Parent key.
FOREIGN KEY: Is also called Referential Integrity Constraint. FOREIGN KEY is one in
which a column or set of columns take references of the Primary/Unique key of same
or another table. FOREIGN KEY is called a child key. A FOREIGN KEY value must match
an existing value in the parent table or be null.
CHECK KEY: Defines a condition that each row must satisfy. A single column can have
multiple CHECK Constraints. During CHECK constraint following expressions is not
allowed:
1) References to CURRVAL, NEXTVAL, LEVEL and ROWNUM Pseudo columns.
2) Calls to SYSDATE, UID, USER and USERENV Functions
------------------------Method1---------------------------------
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
------------------------Method2---------------------------------
ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID);
------------------------Method3---------------------------------
ALTER TABLE CUSTOMER ADD CONSTRAINT CST_PK PRIMARY KEY(ID);
------------------------To Drop Primary Key---------------------------------
ALTER TABLE CUSTOMER DROP PRIMARY KEY;
------------------------Method1---------------------------------
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
------------------------Method2---------------------------------
ALTER TABLE ORDERS
ADD FOREIGN KEY (ID) REFERENCES CUSTOMERS (ID);
------------------------Method3---------------------------------
ALTER TABLE ORDERS
ADD CONSTRAINT FK_ORD FOREIGN KEY (ID) REFERENCES CUSTOMERS (ID);
------------------------To Drop Primary Key---------------------------------
ALTER TABLE ORDERS DROP FOREIGN KEY;
Unique Constraint :
-----------------
-----------Method1--------------------------
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL UNIQUE,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
-------------Method2-------------------------
ALTER TABLE CUSTOMERS
MODIFY AGE INT NOT NULL UNIQUE;
-------------Method3-------------------------
ALTER TABLE CUSTOMERS
ADD CONSTRAINT myUniqueConstraint UNIQUE(AGE, SALARY);
--------------To Drop Unique----------------
ALTER TABLE CUSTOMERS
DROP CONSTRAINT myUniqueConstraint;
-----------Method1--------------------------
System Defined Name
-------------Method2-------------------------
ALTER TABLE CUSTOMERS_TEST
MODIFY ADDRESS INT NOT NULL;
-------------Method3-------------------------
ALTER TABLE CUSTOMERS_TEST
MODIFY ADDRESS INT constraint cons_name NOT NULL;
Default Constraint :
--------------------
-----------Method1--------------------------
CREATE TABLE CUSTOMERS_test(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2) ,--DEFAULT 5000.00,
PRIMARY KEY (ID)
);
-------------Method2-------------------------
ALTER TABLE CUSTOMERS_TEST
MODIFY SALARY DECIMAL (18, 2) DEFAULT 5000.00;
Check Constraint :
-----------Method1--------------------------
CREATE TABLE CUSTOMERS_TEST(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
-------------Method2-------------------------
ALTER TABLE CUSTOMERS_TEST
MODIFY AGE INT NOT NULL CHECK (AGE >= 18 );
-------------Method3-------------------------
ALTER TABLE CUSTOMERS_TEST
ADD CONSTRAINT myCheckConstraint CHECK(AGE >= 18);
--------------To Drop Unique----------------
ALTER TABLE CUSTOMERS_TEST
DROP CONSTRAINT myCheckConstraint;