MySQL Constraint – DEFAULT




MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

Hello everyone in the previous article we learn about MySQL Constraint – CHECK and in this article, we will learn DEFAULT constraint with example. Let’s go.

What is DEFAULT Constraint?
MySQL DEFAULT constraint sets a default value for a column when the insert into statement does not provide a specific value. Default constraint helps you assign a particular default value to every row of a column.  Only if specifically indicated DEFAULT constraint has been applied then a value different from the default can be stored in a field.

Following is the CREATE TABLE statement with DEFAULT constraint syntax:
CREATE TABLE table_name (
                                            column1_name data type () DEFAULT,
                                            column2_name data type () DEFAULT,
                                            column1_name data type () DEFAULT
):



Example of CREATE TABLE statement with DEFAULT constraint:
So we will create here Employee table with CREATE TABLE statement and I will set a default value for address column is ‘USA’.
CREATE TABLE EMPLOYEE (
                                            EID INT NOT NULL,
                                            First_Name VARCHAR (255),
                                            Last_Name VARCHAR (255),
                                            Address VARCHAR (255) DEFAULT ‘AUS’
);


You can add DEFAULT constraint when the table is already created using the ALTER TABLE statement. Following is the syntax and example:
The syntax for add DEFAULT constraint with ALTER TABLE statement:
                              ALTER TABLE table_name
                              ALTER column_name SET DEFAULT values

Example of add DEFAULT constraint with ALTER TABLE statement on Employee Table:
                              ALTER TABLE EMPLOYEE
                              ALTER Address SET DEFAULT ‘AUS’

DROP DEFAULT constraint with ALTER TABLE statement on Employee table:
ALTER TABLE EMPLOYEE
                              ALTER Address DROP DEFAULT


MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/


Comments

Popular posts from this blog

ALTER statement in MySQL

Order By Clause in MySQL

MySQL Constraint – UNIQUE