指点成金-最美分享吧

登录

sql server新增字段语句

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了sql server新增字段语句相关的知识,希望对你有一定的参考价值。

添加字段的SQL语句的写法:

通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数
增加字段: alter table [表名] add 字段名 smallint default 0 增加数字字段,整型,缺省值为0
alter table [表名] add 字段名 int default 0 增加数字字段,长整型,缺省值为0
alter table [表名] add 字段名 single default 0 增加数字字段,单精度型,缺省值为0
alter table [表名] add 字段名 double default 0 增加数字字段,双精度型,缺省值为0
alter table [表名] add 字段名 Tinyint default 0 增加数字字段,字节型,缺省值为0
alter table [表名] add 字段名 text [null] 增加备注型字段,[null]可选参数
alter table [表名] add 字段名 memo [null] 增加备注型字段,[null]可选参数
alter table [表名] add 字段名 varchar(N) [null] 增加变长文本型字段大小为N(1~255)
alter table [表名] add 字段名 char [null] 增加定长文本型字段大小固定为255
alter table [表名] add 字段名 Datetime default 函数增加日期型字段,其中函数可以是 now(),date()等,表示缺省值

(上面都是最常用的,还有其他的属性,可以参考下面的数据类型描述)
删除字段: alter table [表名] drop 字段名
修改变长文本型字段的大小:alter table [表名] alter 字段名 varchar(N)
删除表: drop table [表名]
创建表:
sql="CREATE TABLE [表名] ([字段1,并设置为主键] int IDENTITY
(1, 1) NOT NULL CONSTRAINT PrimaryKey PRIMARY KEY,"&
"[字段2] varchar(50),"&
"[字段3] single default 0,"&
"[字段4] varchar(100) null,"&
"[字段5] smallint default 0,"&
"[字段6] int default 0,"&
"[字段7] date default date(),"&
"[字段8] int default 1)"
conn.execute sql
有null 的表示字段允许零长
参考技术A alter table 表名 add 新增字段名 字段类型 默认值;
例如: alter table tb add col1 int 0;
参考技术B alter table tab
add colname varchar(200);
参考技术C alter table 表名
add 列名 数据类型

MYSQL表中新增字段指定位置,SQL语句该怎么写?

在表中指定位置新增一个字段,要新增的这个字段除了类型外还包含默认值以及注释befor 或者 after 这个放在什么位置?

数据表中添加一个字段的SQL语句写法为:

1、alter table 表名 ADD 字段 类型 NOT NULL Default 0

2、ALTER TABLE employee  ADD  spbh varchar(20) NOT NULL Default 0

3、在表employee 中加入 spbh  类型是varchar大小20 不为空 默认值是0

扩展资料:

其他常用sql语句:

1、修改数据表中某项字段属性,为其添加备注。

语句格式:comment on column  库名.表名.字段名 is  "输入的备注";  

示例: 我要在ers_data库中  test表 document_type字段添加备注,则sql语句为:

comment on column ers_data.test.document_type is "文件类型";

2、删除数据表中的某字段。

语句格式:alter table  表名  drop (字段);

参考技术A

只有两种 

    在某一列后面,就用AFTER,放在最后面

    在一张表的最前面,用FIRST关键字

    没有BEFORE关键字


ALTER [IGNORE] TABLE tbl_name  ADD [COLUMN] column_definition [FIRST | AFTER col_name ]

追问

上面的问题已经解决了,如果说在某个字段后一次性加入好几个字段,可不可以把要加入的几个字段之间用“,”做分割,在最后加入after某个字段。

如下图:

    追答

    其他字段都加上一个ADD就可以了。。

    追问

    非常感谢,问题以及解决了,谢谢

    以上是关于sql server新增字段语句的主要内容,如果未能解决你的问题,请参考以下文章