Default Constraint

What is default constraint? How we you apply it?

Questions by gagan87

Showing Answers 1 - 18 of 18 Answers

It specifies a default value for a new column or a new default for an existing column.
Oracle assigns this value to the column if a subsequent INSERT statement omits a value for the column.

 A DEFAULT expression cannot contain references to other columns, the pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM, or date constants that are not fully specified.

Default Contraint acts the same way as the other constraints like NOT NULL, UNIQUE, PRIMARY KEY............. do; they come into picture while data insertion is done into the table. It makes oracle insert a values automatically into table without being specified in the insert; for example:

CREATE TABLE TTEMP (NM VARCHAR2(10) DEFAULT 'Sreekanth', JOB VARCHAR2(10));

now we can skip from the NM column being filled:
INSERT INTO TTEMP VALUES (null,'sales'); or INSERT INTO TTEMP VALUES ('','sales');
in ordinary case without default constraint assigned.. NULL gets inserted but here 'Sreekanth' is inserted in each unspecified row.

gdurga

  • Jun 15th, 2009
 

A coulumn can be given a default value by using DEFAULT option. This option prevents null values from entering the column if a row is inserted with out a value in to the table.
Ex:
.......
Hire_date DATE DEFAULT SYSDATE,
.......

Sam Kolta

  • Apr 24th, 2010
 

1. What is default constraint?
Constraints are specific values that used to limit the type of data that can go into a table by ensure certain columns have a true condition when data is inserting, modifying or deleting to these columns.- Default constraint is used to insert a default value into a column of all new records if no other value is specified.- They are other 5 different constraints other then default value such as: Not Null Constraint, Unique constraint, Primary key constraint, Secondary Key constraint, and Check constraint

How you apply to default constraint?
In case of create table as in the following example:

CREATE TABLE clients
(
clien_Id int NOT NULL
, FirstName varchar(255) NOT NULL
, LastName varchar2(255)
, Address varchar2(255)
, City varchar2(255) Default ‘Detroit’
);

In case of insert system values by using function like GETDATE()CREATE TABLE clients
(
clien_Id int NOT NULL
, FirstName varchar(255) NOT NULL
, LastName varchar2(255)
, Address varchar2(255)
, City varchar2(255)
, contact date DEFAULT GETDATE()
);

In case of existing table:
ALTER TABLE Clients
ALTER COLUMN City SET DEFAULT ‘DETROIT’

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions