查看表结构 desc table t1
建表过程 就是创建表头的过程 (声明列的过程)
create table t1(id int(10) unsigned primary key auto_increment ,name varchar(20))
mysql常见数据类型:整型 十进制 十六进制 八进制 浮点数 字符串 null
整型 主要有 tinyint 1个字节 smallint 2个字节 mediumint 3个字节 int 4个字节 bigint 8个字节
整型可选参数 unsigned 无符号 列的值从0开始 不为负
zerofill 0填充至固定宽度 非负 常用于学号 编号 zerofill 4
浮点数主要有 单精度float 双精度 double
float(M,D) double(M,D) M是精度 总位数 D标度 小数点后面的位数 有精度损失
定点型 decimal(m,d)常用于保存货币,m表示数字的最大数(总共位数),d表示小数点右侧数字的数目
字符型
char(M) 定长类型 0<=M<=255 实际所占空间 M 空格字符会被丢失
varchar(M) 变长类型 0<=M<=65535 实际所占空间 存储字符+位置字符 长字节数据 使用变长类型 较为合理
枚举型 性别
create table t1(gender enum('男',‘女’));
日期类型 Date Time datetime
时间戳一般用int usigned存储
create table t1 (id int not null default 0,name char(10) not null default '');
主键 primary key 此列不重复 区分每一行 主键可以设置自增 auto_increment
建表常用原则 定长与变长分离 常用列与不常用列分离
列的删除增加修改
alter table t1 drop username;
alter table test add pid int not null default 0 first;
alter table t1 add name tinyint unsigned not null default 0 after weight ;
alter table t1 change height shengao smallint;
alter table test modify gender char(4) not null default ''; 不能修改名称
alter table test change oldname newname int unsigned not null default 0;