- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - Default Constraint
In general, a default constraint is useful when the value has not been passed in the column that is specified with a default constraint. Then the column data will automatically be filled with the default value.
SQL - Default Constraint
The constraint that allows the user to fill the column with the fixed or default value is known as the Default.
Consider the following scenario − we have created a table with a few columns, without inserting any values into it. Then, by default, default values take over and add default data.
In the SQL table we can pass more than one default value of the Column. We have to define the value with the Default constraint.
To add the default constraint on the column while creating the table, we have to follow these steps.
Create a new table and add the default constraint.
Insert the records in the table
View the table data with the help SQL query
Syntax
Following is the syntax of the default constraint −
CREATE TABLE table_Name( Column1 datatype(size) DEFAULT value, Column2 datatype(size) DEFAULT value, …, ColumnN datatype(size) DEFAULT value );
Example
In the following SQL query, we are creating a table named emp_Info. While creating the table, we are adding a default constraint to the column.
CREATE TABLE emp_Info( E_ID INT NOT NULL, NAME VARCHAR(20) DEFAULT AmanKr , AGE INT DEFAULT 21, COURSE VARCHAR(20) DEFAULT CSE );
Inserting the record into the created table named emp_Info.
INSERT INTO emp_Info(E_ID) VALUES(101); INSERT INTO emp_Info(E_ID, NAME, AGE, COURSE) VALUES(102, SonuKr , 23, MECH );
View the table data using the following SQL query
SELECT * FROM emp_Info;
When we execute the above SQL query, we get the following table, where in the first row the name, age, and course are fetched by default.
+------+--------+------+--------+ | E_ID | NAME | AGE | COURSE | +------+--------+------+--------+ | 101 | AmanKr | 21 | CSE | | 102 | SonuKr | 23 | Mech | +------+--------+------+--------+
Note − If the column name is not included in the SQL query while inserting the data, we can still retrieve the default value from the created table by including the DEFAULT keyword in place of the value.
Following is the SQL query to insert the default value −
INSERT INTO emp_Info VALUES(103, DEFAULT, DEFAULT, BIO ); INSERT INTO emp_Info VALUES(104, VivekKr , DEFAULT, DEFAULT);
View the table data using the following SQL query
SELECT * FROM emp_Info;
When we execute the above SQL query, we get the following table − After inserting the two row s values, which are either the defaults or manually given by the user.
+------+---------+------+--------+ | E_ID | NAME | AGE | COURSE | +------+---------+------+--------+ | 101 | AmanKr | 21 | CSE | | 102 | SonuKr | 23 | Mech | | 103 | AmanKr | 21 | BIO | | 104 | VivekKr | 21 | CSE | +------+---------+------+--------+
Default Constraint in an Existing Table
The Default Constraint can also be added to an existing table in a database; not just while creating a new table.
Syntax
Following is the SQL query to add a default constraint to an existing table −
ALTER TABLE table_name ADD CONSTRAINT Default_name DEFAULT default_value FOR Column_name;
Example
The following SQL statement adds a default constraint to an existing table. when there is no default constraint at all. Thus, by using the following query, we are adding a default constraint.
ALTER TABLE customers ADD CONSTRAINT DF_ADDRESS DEFAULT NOIDA FOR ADDRESS;
Following is the insert query to insert the default constraint into the exiting table.
INSERT INTO customers(ID, NAME, AGE, SALARY) VALUES(05, AMAN , 23, 500000);
Verification
After the insertion of the column s default value, we are displaying the row that was inserted into the table. using the following query (SELECT * FROM customers;) −
+----+------+-----+---------+---------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+------+-----+---------+---------+ | 5 | AMAN | 23 | NOIDA |500000.00| +----+------+-----+---------+---------+
Drop Default Constraint
To drop the default constraint from the columns of the table, we have to use the ALTER TABLE… DROP statement. This allows the database users to delete the default constraint from the table.
Syntax
Following is the syntax to drop the default constraint −
ALTER TABLE table_name DROP Default_constraint_name;
Example
In the following example, we will see how to drop the default constraint from a table.
But before dropping the constraint, we must verify whether there is a default constraint in the customer s table or not.
Here in the following table, we can see that there is a default constraint on the age column.
+-------------------------+---------------------------------+---------------+-----------------+ | constraint_type | constraint_name | delete_action | constraint_keys | +-------------------------+---------------------------------+---------------+-----------------+ | DEFAULT on column AGE | DF_AGE | (n/a) | ((22)) | | PRIMARY KEY (clustered))| PK__CUSTOMER__3214EC270E48CD10 | (n/a) | ID | +-------------------------+---------------------------------+---------------+-----------------+
We are trying to drop the default constraint from the age column of the customers table, using the following SQL query −
ALTER TABLE customers DROP DF_AGE;
Verification
After the execution of the above SQL query, we re verifying the table details to see if there is a default constraint. Using the SQL query (EXEC sp_help dbo.customers );).
+-------------------------+---------------------------------+---------------+-----------------+ | constraint_type | constraint_name | delete_action | constraint_keys | +-------------------------+---------------------------------+---------------+-----------------+ | PRIMARY KEY (clustered))| PK__CUSTOMER__3214EC270E48CD10 | (n/a) | ID | +-------------------------+---------------------------------+---------------+-----------------+Advertisements