MySQL基本数据类型

MySQL基本分为三类:数值日期字符串类型枚举类型和集合类型

数值类型

日期类型

DATETIME(1000-01-01,00:00:00,9999-12-31 23:59:59)常用、DATE、TIMESTAMP、TIME和YEAR

 字符串类型

枚举类型和集合类型

枚举为单选(enum)

create table t1(
     id int,
     name char(10),
     gender enum('male','female')   
);

集合为多选(set)

create table t1(
    id int,
    name char(10),
    course set('Math','Chinese','English')
);

完整约束

NOT NULL   DEFAULT 'XXX'  --不空且有默认值
UNIQUE   --唯一值
UNIQUE key(attr1,attr2)   --联合唯一
Primary key    
Foreign key 
Auto_incerement --自动增长,未给出id则自动创建,一般加在主键后面,从1开始每次+1,可以设置步长和初始值

基本应用例:

Create table t2(
    id primary key auto_increment,
    name char(10) not null,
    gender enum(‘male’,’female’) defalult ‘male’ not null,
    id_card int not null,
    foreign key(id_card) references t1(id)
);
原文地址:https://www.cnblogs.com/gracenana/p/10300458.html