Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(20) DEFAULT 'John' ); Query OK, 0 rows affected (0.76 sec)
Let us check the description of table −
mysql> desc DemoTable;
This will produce the following output −
+----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | | UserName | varchar(20) | YES | | John | | +----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.17 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | John | +--------+----------+ 1 row in set (0.00 sec)
Here is the query to modify column default value. We have set the default UserName to Chris −
mysql> alter table DemoTable modify UserName varchar(20) DEFAULT 'Chris'; Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table using desc command −
mysql> desc DemoTable;
This will produce the following output −
+----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | | UserName | varchar(20) | YES | | Chris | | +----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
Now insert some records in the table using insert command. Since we haven’t added a name, therefore the default “Chris” will get added −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.11 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | John | | 2 | Chris | +--------+----------+ 2 rows in set (0.00 sec)