SQL Constraints
SQL Constraints
SQL Constraints
CONSTRAINTS
The SQL CONSTRAINTS are an integrity which defines some conditions that
restrict the column to remain true while inserting or updating or deleting data in
the column. Constraints can be specified when the table created first with
CREATE TABLE statement or at the time of modification of the structure of an
existing table with ALTER TABLE statement.
The SQL CONSTRAINTS are used to implement the rules of the table. If there is
any violation of the constraints caused some action not performing properly on
the table the action is aborted by the constraint.
Some CONSTRAINTS can be used along with the SQL CREATE TABLE
statement.
Constraint Description
NOT NULL This constraint confirms that a column cannot store NULL value.
UNIQUE This constraint ensures that each row for a column must have a different value.
PRIMARY This constraint is a combination of a NOT NULL constraint and a UNIQUE constraint.
KEY This constraint ensures that the specific column or combination of two or more columns for
a table have a unique identity which helps to find a particular record in a table more easily
and quickly.
CHECK A check constraint ensures that the value stored in a column meets a specific condition.
DEFAULT This constraint provides a default value when specified none for this column.
FOREIGN A foreign key constraint is used to ensure the referential integrity of the data. in one table to
KEY match values in another table.
Example:
The following example creates a table. Here is the field name and data types :
agent_code char 6 No
agent_name char 25 No
working_area char 25 No
SQL Code:
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output:
Example:
The following example creates a table. Here is the field name and data types:
ord_date date No
cust_code char 6 No
agent_code char 6 No
SQL Code:
-- Column: ord_num with a decimal data type (6 digits, 0 decimal places), NOT NULL, and UNIQUE
constraint
-- Column: ord_amount with a decimal data type (12 digits, 2 decimal places)
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output:
working_area char 25 No
SQL Code:
-- Column: agent_code with a data type CHAR(6), NOT NULL, and UNIQUE constraint
commission DECIMAL(5, 2)
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output:
Example :
The following example creates a table. Here is the field name and data types :
cust_name char 25 No
cust_city char 25 No
SQL Code:
grade INTEGER,
UNIQUE(cust_code, agent_code)
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output:
The job of CHECK constraint is, to limit the values for a column of a table.
Note :
The following example creates a table. The table contains a CHECK CONSTRAINT on commission column.The
constraint ensures that the 'commission' must be less than 1. Here is the field name and data types :
working_area char 25 No
SQL Code:
-- Column: agent_code with a data type CHAR(6), NOT NULL, and UNIQUE constraint
-- Column: agent_name with a data type CHAR(25), NOT NULL, and UNIQUE constraint
-- Column: commission with a decimal data type, with a CHECK constraint ensuring commission is
less than 1
);
Copy
To see the structure of the created table:
SQL Code:
DESCRIBE mytest;
Copy
Output :
SQL CREATE TABLE using DEFAULT CONSTRAINT
The SQL DEFAULT CONSTRAINT provides a default value when specified none for a column.
Example:
1. The 'working_area' should be 'Mumbai' when specified none for this column,
at the time of creating a table whose field names and data types are -
SQL Code:
-- Column: agent_code with a data type CHAR(6), NOT NULL, and UNIQUE constraint
-- Column: working_area with a data type CHAR(25), a DEFAULT value of 'Mumbai' if not specified
commission DECIMAL(8, 2)
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output :
Example:
2. The 'working_area' should be 'Mumbai' when specified none for this column,
at the time of creating a table which contains the following field names and data types -
Field Name Data Type Size Decimal Places NULL Constraint
SQL Code:
-- Column: agent_code with a data type CHAR(6), NOT NULL, and UNIQUE constraint
-- Column: agent_name with a data type CHAR(25), NOT NULL, and UNIQUE constraint
-- Column: working_area with a data type CHAR(25), a DEFAULT value of 'Mumbai' if not specified
commission decimal(8,2)
-- CHECK constraint ensuring commission is greater than 0.1 and less than 0.3
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output :
The condition for CHECK CONSTRAINT can be defined using any of the basic comparison operator, such as (>, <,
=,>=,<=,<>) as well as BETWEEN, IN, LIKE, and NULL operator.
Example:
1. The first one is on 'working_area' column which ensures that the working_area should be either 'London' or
'Brisban' or 'Chennai' or 'Mumbai',
2. The second one is on 'commission' column which ensures that commission must be less than 1,
in the following table which field name and data types are -
SQL Code:
( working_area IN('London','Brisban','Chennai','Mumbai')) ,
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output:
Example:
To include a CHECK CONSTRAINT on 'ord_date' column which ensures that the format of the 'ord_date' must be like
'--/--/----', for example, ('18/05/1998') at the time of creating a table with following field names and data types -
agent_code char 6 No
SQL Code:
ord_amount decimal(12,2) ,
);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output :
Example:
To include a CHECK CONSTRAINT on 'commission' and 'working_area' column which ensures that the 'commission'
must be less than .20 and 'working_area' must be 'London' at the time of creating the following table which consists
the field names and data types -
SQL Code:
working_area char(25) ,
commission decimal(8,2) ,
CHECK(commission<.20 OR working_area='London'));
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output :
SQL CREATE TABLE using CHECK CONSTRAINT and AND, OR operator
In the following topic, we are going to discuss the usage of OR and AND operator along with the CHECK
CONSTRAINT. The condition will start to work at the time of inserting the records in the table.
Example:
To include a CHECK CONSTRAINT on 'commission' and 'working_area' column which ensures that -
1. The 'commission' must be less than .14 and 'working_area' must be 'London',
2. or the 'commission' must be less than .15 and 'working_area' must be 'Mumbai',
3. or the 'commission' must be less than .13 and 'working_area' must be 'New York'
at the time of creating the table which fields name and data types are-
SQL Code:
working_area char(25) ,
commission decimal(8,2) ,
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output :
The DELETE CASCADE works across a foreign key link and removes the child records associated with the parent
records.
Example:
To create a table which contains the following field name and data types -
tranno decimal No
The table contains a PRIMARY KEY on 'itemcode' and a FOREIGN KEY on 'company_id' column which references
to the 'company_id' column of 'company' table.
SQL Code:
company_id varchar(6),
itemcode varchar(10),
coname varchar(35),
itemname varchar(35),
iqty integer,
PRIMARY KEY(itemcode),
FOREIGN KEY(company_id)
ON DELETE CASCADE);
Copy
To see the structure of the created table :
SQL Code:
DESCRIBE mytest;
Copy
Output:
Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.
The job of CHECK constraint is, to limit the values for a column of a table.
Note :
The following example creates a table. The table contains a CHECK CONSTRAINT on commission column.The
constraint ensures that the 'commission' must be less than 1. Here is the field name and data types :
working_area char 25 No
-- Column: agent_name with a data type CHAR(25), NOT NULL, and UNIQUE constraint
agent_name CHAR(25) NOT NULL UNIQUE,
-- Column: commission with a decimal data type, with a CHECK constraint ensuring commission is
less than 1
commission DECIMAL CHECK (commission < 1)
);
Name varchar(100),
dname varchar(100),
dloc varchar(100)
alter table emp add costraint fk_dept_id FOREIGN KEY(dept_id) REFERENCE DEPT(deptid);
1
2
d1
d2
d3
UNION
MINUS
JOIN
Q>
101 software 10
102 QA 10
103 engineering 10
105 developer 30
106 manager 20
107 software 20
108 QA 20
109 Engineer 20
This is the data.Please write me a query to get the jobs common in dept 10 and 20