Mysql – Setting a column’s default value by adding a constraint

constraintMySQL

I wrote this query:

ALTER TABLE test2 add constraint setting_value33 default male FOR namee;

But I get this error when running it:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default male FOR namee' at line 1

Best Answer

This is a rather contrived example, but you'll get the idea.

mysql> create table b(a int primary key, c varchar(3), unique key x_b_uk (c));
Query OK, 0 rows affected (0.46 sec)

mysql> alter table b alter c set default 'xyz';
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into b (a) values(1);   <<== didn't set field c!
Query OK, 1 row affected (0.39 sec)

mysql> select * from b;
+---+------+
| a | c    |
+---+------+
| 1 | xyz  |                    <<===== value of c = 'xyz'!
+---+------+
1 row in set (0.16 sec)

mysql> 

Check here.