爱程序网

MySQL创建/删除/清空表,添加/删除字段

来源: 阅读:

创建表:

create table tablename (column_name column_type);
create table table_name(   id       int not null auto_increment,   column1  varchar(32) not null,
  column2 int(2) default 'test',
primary key ( id )
);
not null    : 该字段不能为空。
default xxx : 缺省为xxx。
auto_increment:该字段自动增加。
primary key : 主键;主键不能重复,并且唯一。

删除表:

drop table tablename

清空数据表内容:

delete from tablename;truncate table tablename;虽然两种方法都能删除表中数据,但truncate比delete快,并且不记录mysql日志,也不可以恢复数据。

添加字段:

alter table tablename add column newcolumn int default 0 (after whichcolumn)(可选;在哪个字段后面添加)  

删除字段:

alter table tablename drop column oldcolumn

如有错误,请指正博主会及时修改。

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助