MySQL数据字段属性

MySQL有如下字段属性:

MySQL数据字段属性

模板:

create table test(

id int unsigned auto_increment primary key,

name varchar(30) not null default “”,

age int not null default 0,

height float not null default 0.00

);

1.unsigned(无符号)

作用:可以让空间增长一倍,只能使用在整型中使用
例子:create table test(
    id int unsigned not null;
   )
注意:上面的id定义为无符号类型的,所以不能添加负值

2.zerofill(前导0)

作用:在位数不够时,前面用0补充。
例子:id int(5) zerofill
当你输入1234的时候,如果没有zerofill,查看时数据库里会显示:1234;加上zerofill时,会显示01234来补齐5位。

注意:如果我们定义的列是zerofill的,那么MySQL会自动给这个列加上unsigned属性。

3.auto_increment(自动增长)

作用:
  如果我们给一个列添加了autoincrement属性,那么当我们插入null或0值时,列值便会自动设置成下一个***,一般是当前value+1。
例子:
  id int auto_increment primary key;

注意:
  1.auto_increment字段的值不能有重复,所以创建时需要加索引;
  2.通常每个表都有一个ID设置为auto_increment;