Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is DEFAULT Constraint in MySQL?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 372
    Comment on it

    In MySQL, the DEFAULT constraint is used to insert value into a column in a table. If there is no value is specified for a column then the default value will be added to column for all new records.

    DEFAULT Constraint on CREATE TABLE

    The following statement creates a DEFAULT constraint on the "country" column when the "employee" table is created.

    Example:

    CREATE TABLE employee
    (
    id int NOT NULL,
    first_name varchar(45) NOT NULL,
    last_name varchar(45),
    country varchar(45) DEFAULT 'Canada'
    );
    

    DEFAULT Constraint on ALTER TABLE

    We can also create DEFAULT constraint on a column when the table is already created, for that we use ALTER command. The below statement creates a DEFAULT constraint on the "country" column when the table "employee" is already created:

    ALTER TABLE employee
    ALTER country SET DEFAULT 'Canada';
    

    To DROP a DEFAULT Constraint

    We can also drop a DEFAULT constraint. To drop a DEFAULT constraint, use the below statement:

    ALTER TABLE employee
    ALTER country DROP DEFAULT;
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: