user3424840
user3424840

Reputation: 227

How to set varchar default value

Is it possible to create table where all varchar will have default value '' It is big table so I do not want to use syntax like: a varchar(10) default '' Is it possible to define it once for whole table?

Upvotes: 1

Views: 1864

Answers (1)

Bernd Buffen
Bernd Buffen

Reputation: 15057

in Linux you can change the creates Tables with one line in vi

:1,$s/\cvarchar(\([0-9]*\))/VARCHAR(\1) DEFAULT ''/g

Sample

CREATE TABLE `elements1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Person1` varchar(8),
  `Person2` varchar(28) NOT NULL,
  `Person3` varchar(12) NOT NULL,
  `Person4` varchar(128) NOT NULL,
  `Person5` varchar(28) NOT NULL,
  `Person6` varchar(18) NOT NULL,
  `tags` json NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

:1,$s/varchar(\([0-9]*\))/VARCHAR(\1) DEFAULT ''/g

CREATE TABLE `elements1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Person1` VARCHAR(8) DEFAULT '',
  `Person2` VARCHAR(28) DEFAULT '' NOT NULL,
  `Person3` VARCHAR(12) DEFAULT '' NOT NULL,
  `Person4` VARCHAR(128) DEFAULT '' NOT NULL,
  `Person5` VARCHAR(28) DEFAULT '' NOT NULL,
  `Person6` VARCHAR(18) DEFAULT '' NOT NULL,
  `tags` json NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

Upvotes: 1

Related Questions