语句:
alter table tableName rename column oldCName to newCName; -- 修改字段名
alter table tableName modify (cloumnName 数据类型); -- 修改数据类型
例如:
1、创建表:
CREATE TABLE Student(
id varchar2(32) primary key,
name varchar2(8) not null,
age number
);
2、修改字段名:
alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)
alter table Student rename column name to StuName;
3、修改数据类型:
alter table 表名 modify (字段名 字段类型 默认值 是否为空);
alter table Student modify (id varchar2(64));
4、删除字段
alter table 表名 drop column 字段名;
alter table Student drop column id;
alter table 表名 modify column 字段名 类型;
1、修改数据类型
alter table address modify column city char(30);
mysql修改字段类型:
--能修改字段类型、类型长度、默认值、注释
--对某字段进行修改
ALTER TABLE 表名 MODIFY COLUMN 字段名 新数据类型 新类型长度 新默认值 新注释; -- COLUMN可以省略
alter table table1 modify column column1 decimal(10,1) DEFAULT NULL COMMENT '注释'; -- 正常,能修改字段类型、类型长度、默认值、注释
mysql修改字段名:
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型;
alter table table1 change column1 column1 varchar(100) DEFAULT 1.2 COMMENT '注释'; -- 正常,此时字段名称没有改变,能修改字段类型、类型长度、默认值、注释
alter table table1 change column1 column2 decimal(10,1) DEFAULT NULL COMMENT '注释' -- 正常,能修改字段名、字段类型、类型长度、默认值、注释
alter table table1 change column2 column1 decimal(10,1) DEFAULT NULL COMMENT '注释' -- 正常,能修改字段名、字段类型、类型长度、默认值、注释
alter table table1 change column1 column2; -- 报错
1、增加字段:
alter table 表名 add 字段名 type not null default 0
2、删除字段:
alter table 表名 drop column 字段名;
3、修改字段名:
alter table 表名 rename column A to B
3-1、修改字段名:这个可用
注意,单引号不可省略。
exec sp_rename '[表名].[列名]','[新列名]'
4、修改字段类型:
alter table 表名 alter column 字段名 type not null
5、修改字段默认值
alter table 表名 add default (0) for 字段名 with values
如果字段有默认值,则需要先删除字段的约束,在添加新的默认值,
select c.name from sysconstraints a
inner join syscolumns b on a.colid=b.colid
inner join sysobjects c on a.constid=c.id
where a.id=object_id('表名')
and b.name='字段名'
根据约束名称删除约束
alter table 表名 drop constraint 约束名
根据表名向字段中增加新的默认值
alter table 表名 add default (0) for 字段名 with values