- Define a rule on a column that specifies a default value for the column if no value is specified during an insert operation.
- If new row is inserted and no value is inserted in the column with default constraint then the default value specified in the constraint will be inserted in that column
- While Creating the table
CREATE TABLE table_name (
column1 datatype DEFAULT default_value,
column2 datatype DEFAULT default_value,
...
);
E.g.
CREATE TABLE tblperson (
PersonID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
GenderID INT DEFAULT 3
);
CREATE TABLE Employees (
EmployeeID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATETIME DEFAULT GETDATE()
);
- After Creating the Table
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
DEFAULT default_value FOR column_name;
E.g.
ALTER TABLE tblperson
ADD CONSTRAINT DF_tblperson_Gender
DEFAULT 'Unknown' FOR Gender;
Comments
Post a Comment