Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the default values for a column when creating tables in MySQL?

Tags:

What is the SQL to define DEFAULT values in MySQL?

In the code below what needs to be added / changed to give IsObsolete a default value of N?

CREATE TABLE Team (     TeamId              CHAR(16) NOT NULL,     DateCreated          TIMESTAMP NOT NULL,     IsObsolete           CHAR(1) NOT NULL DEFAULT N,     UpdateTime           TIMESTAMP NOT NULL ); 
like image 950
swisscheese Avatar asked Apr 27 '11 21:04

swisscheese


People also ask

What is a default value for a column of a table in MySQL?

If a data type specification includes no explicit DEFAULT value, MySQL determines the default value as follows: If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause. If the column cannot take NULL as a value, MySQL defines the column with no explicit DEFAULT clause.

Is it possible to specify the default value for a column while creating table?

When using CREATE TABLE , you can specify default values for columns by typing DEFAULT and then the desired value after it. If a row is inserted that does not specify a value for that column, the database will fill it in with the default value instead.


2 Answers

IsObsolete           CHAR(1) NOT NULL DEFAULT 'N' 
like image 109
Anthony Accioly Avatar answered Sep 22 '22 01:09

Anthony Accioly


You probably want to put quotes around it:

CREATE TABLE Team (     TeamId              CHAR(16) NOT NULL,     DateCreated          TIMESTAMP NOT NULL,     IsObsolete           CHAR(1) NOT NULL DEFAULT 'N',     UpdateTime           TIMESTAMP NOT NULL ); 
like image 22
CanSpice Avatar answered Sep 24 '22 01:09

CanSpice