
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Set DEFAULT values for columns while creating a table in MySQL
To set default values for columns while creating a table, DEFAULT. Let us first see an example and create a table. As you can see below, while creating the table, we have set DEFAULT −
mysql> create table DemoTable803 ( UserId int DEFAULT 101, UserName varchar(100) DEFAULT 'Chris' ); Query OK, 0 rows affected (1.18 sec)
Insert some records in the table using insert command. For the values we are not inserting, the default values will get set automatically −
mysql> insert into DemoTable803 values(102,'Chris'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable803(UserName) values('Mike'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable803(UserId) values(103); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable803 values(); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable803 values(110,'Robert'); Query OK, 1 row affected (0.33 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable803;
This will produce the following output -
+--------+----------+ | UserId | UserName | +--------+----------+ | 102 | Chris | | 101 | Mike | | 103 | Chris | | 101 | Chris | | 110 | Robert | +--------+----------+ 5 rows in set (0.00 sec)
Following is the query to display the default values only −
mysql> select default(UserId),default(UserName) from DemoTable803 limit 1;
This will produce the following output -
+-----------------+-------------------+ | default(UserId) | default(UserName) | +-----------------+-------------------+ | 101 | Chris | +-----------------+-------------------+ 1 row in set (0.00 sec)
- Related Articles
- Display only the default values set for columns in MySQL
- Set AUTO_INCREMENT in a table while creating it in MySQL?
- Creating a Table in MySQL to set current date as default
- Populate null columns in a MySQL table and set values
- Set special characters for password while creating a new MySQL user?
- Set options while creating a MySQL table. Display the same options as well
- Can we use {} while creating a MySQL table?
- Set multiple values for custom columns in MySQL?
- Error occurs when table name “references” is set while creating a table
- How can we add columns with default values to an existing MySQL table?
- Set value only for NULL values in a MySQL table
- While creating a MySQL table use the reserved keyword ‘Key’
- MySQL query to set different combinations for values in a table?
- How can I return the values of columns from MySQL table as a set of values?
- Set conditions for columns with values 0 or 1 in MySQL

Advertisements