sqlsever实现更改字段名

新建表:
create table [表名]
(
[自动编号字段] int identity (1,1) primary key ,
[字段1] nvarchar(50) default ‘默认值’ null ,
[字段2] ntext null ,
[字段3] datetime,
[字段4] money null ,
[字段5] int default 0,
[字段6] decimal (12,4) default 0,
[字段7] image null ,
)

删除表:
drop table [表名]

插入数据:
insert into [表名] (字段1,字段2) values (100,’51windows.net’)

删除数据:
delete from [表名] where [字段名]>100

更新数据:
update [表名] set [字段1] = 200,[字段2] = ’51windows.net’ where [字段三] = ‘haiwa’

新增字段:
alter table [表名] add [字段名] nvarchar (50) null

删除字段:
alter table [表名] drop column [字段名]

修改字段:
alter table [表名] alter column [字段名] nvarchar (50) null

重命名表:(access 重命名表,请参考文章:在access数据库中重命名表)
sp_rename ‘表名’, ‘新表名’, ‘object’

新建约束:
alter table [表名] add constraint 约束名 check ([约束字段] <= ‘2000-1-1’)

删除约束:
alter table [表名] drop constraint 约束名

新建默认值
alter table [表名] add constraint 默认值名 default ’51windows.net’ for [字段名]

删除默认值
alter table [表名] drop constraint 默认值名

另外以上的只是sql的语法在 access 下大部份也都是一样的

项一项查阅,并经自己使用验证,确认在access 数据库中添加自动编号字段使用以下方法比较合适: create table 数据表名称 (id counter constraint primarykey primary key) 需要注意的地方是:第二个primary中间有空格,另外,关键字不区分大小写. 另外自己最近发现的一种方法是: sql=”create table mytb (id autoincrement(25,1) primary key,age int)” sql2=”create table testtb (id autoincrement,age int,email char, primary key (id))” 其中在access中,autoincrement为自动编号类型字段,(25,1)分别为初始值及步长值,如果不写的话,默认是1,1,primary key指定了主键,以上示例,两种指定方法都可以

2、更改字段名(列名)

sp_rename ‘表名.原列名’,’修改后的列名’,’column’

如把tb_wf_approveinfo表中的doc_id列名改为bind_id:

sp_rename ‘tb_wf_approveinfo.doc_id’,’bind_id’,’column’

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐