DATABASE & SQL/PLSQL

The DEFAULT Constraint
Previous Home Next

A default constraint can be used to assign a constant value to a column, and the user need not insert values for such a column . Only one DEFAULT constraint values can be created for a column , but the column cannot be an IDENTITY column. The system-supplied values like USER, CURRENT_USER, and user defined values can be assigned as defaults.

CREATE TABLE  table_name
(column_name data_type  DEFAULT [constant_expression])

where, constraint_name specifies the name of the constraint to be created.

constant_expression specifies an expression that contains only constant values and can contain NULL.

Example:

CREATE TABLE  employee
(cCity char (14)  DEFAULT  'New Delhi', emp_id  varchar(15))

The above command use to create a DEFAULT constraint on the cCity attribute. If a city is not specified, then the cCity attribute would contain 'New Delhi' by default.

output

 

If table already created then using ALTER command we specified the DEFAULT constraint

ALTER TABLE  employee
ADD CONSTRAINT  defcCity  DEFAULT  'New Delhi'  FOR  cCity

output

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: