为什么列名无效?

leong_dongzi 2012-03-23 07:13:31
alter table student_info
add 学生姓名 varchar(10) not null
update student_info
set 学生姓名=姓名
alter table stedent_info
drop column 姓名

使用Transact-SQL语句ALTER TABLE修改student_info表的“姓名”列,使其列名为“学生姓名”,数据类型为varchar(10),非空。
我使用上面的代码,执行之后出现错误,为什么会这样?
消息 207,级别 16,状态 1,第 0 行
列名 '学生姓名' 无效。
...全文
427 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
showlove1314 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 travylee 的回复:]

引用 6 楼 leong_dongzi 的回复:

引用 5 楼 dawugui 的回复:

SQL code
create table student_info(id int , 姓名 varchar(10))
go

alter table student_info add 学生姓名 varchar(10) null --如果是添加列,不能为 not null

go
……
[/Quote]

添加列需要注意楼上回复的这些信息
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 leong_dongzi 的回复:]

引用 5 楼 dawugui 的回复:

SQL code
create table student_info(id int , 姓名 varchar(10))
go

alter table student_info add 学生姓名 varchar(10) null --如果是添加列,不能为 not null

go

update student_info set 学生……
[/Quote]


ALTER TABLE 只允许添加满足下述条件的列:
列可以包含 Null 值;或者列具有指定的 DEFAULT 定义;
或者要添加的列是标识列或时间戳列;
或者,如果前几个条件均未满足,则表必须为空以允许添加此列。
不能将列"学生姓名 "添加到非空表"student_info"中,因为它不满足上述条件
leong_dongzi 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dawugui 的回复:]

SQL code
create table student_info(id int , 姓名 varchar(10))
go

alter table student_info add 学生姓名 varchar(10) null --如果是添加列,不能为 not null

go

update student_info set 学生姓名 = 姓名

alter table student_i……
[/Quote]添加列不能为not null,就是说这个实验步骤就是让验证不能实现啦?好的,谢谢
dawugui 2012-03-23
  • 打赏
  • 举报
回复
create table student_info(id int , 姓名 varchar(10))
go

alter table student_info add 学生姓名 varchar(10) null --如果是添加列,不能为 not null

go

update student_info set 学生姓名 = 姓名

alter table student_info drop column 姓名
go

drop table student_info
leong_dongzi 2012-03-23
  • 打赏
  • 举报
回复
原来的姓名列是 char(8)允许空。
使用Transact-SQL语句ALTER TABLE修改student_info表的“姓名”列,使其列名为“学生姓名”,数据类型为varchar(10),非空。
是不是上面的要求无法实现?上机实验,会不会就让验证这个无法实现的?
  • 打赏
  • 举报
回复


GO
IF OBJECT_ID('TBL')IS NOT NULL
DROP TABLE TBL
GO
CREATE TABLE TBL(
RName varchar(20),
SumQty decimal(18,2),
YearMonth varchar(6)
)
GO
INSERT TBL
SELECT '沪北小区',25452.00,'200901' UNION ALL
SELECT '沪北小区',35452.00,'200901' UNION ALL
SELECT '嘉定校区',26452.00,'200901' UNION ALL
SELECT '南校区', 25452.00,'200901' UNION ALL
SELECT '四平校区',25422.00,'200901' UNION ALL
SELECT '彭五小区',52452.00,'200901' UNION ALL
SELECT '沪北小区',21252.00,'200902' union all
SELECT '沪西小区',20452.00,'200902'

alter table TBL add RegionName varchar(20)
select * from tbl
update tbl set RegionName=RName

/*
RName SumQty YearMonth RegionName
沪北小区 25452.00 200901 沪北小区
沪北小区 35452.00 200901 沪北小区
嘉定校区 26452.00 200901 嘉定校区
南校区 25452.00 200901 南校区
四平校区 25422.00 200901 四平校区
彭五小区 52452.00 200901 彭五小区
沪北小区 21252.00 200902 沪北小区
沪西小区 20452.00 200902 沪西小区
*/
  • 打赏
  • 举报
回复
ALTER TABLE 只允许添加满足下述条件的列:
列可以包含 Null 值;或者列具有指定的 DEFAULT 定义;
或者要添加的列是标识列或时间戳列;
或者,如果前几个条件均未满足,则表必须为空以允许添加此列。
不能将列"学生姓名 "添加到非空表"student_info"中,因为它不满足上述条件
Felixzhaowenzhong 2012-03-23
  • 打赏
  • 举报
回复

alter table student_info
add 学生姓名 varchar(10) not null
GO
update student_info
set 学生姓名=姓名
GO
alter table stedent_info
drop column 姓名

这样写就OK了

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧