首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

为啥修改不了数据类型

2012-12-26 
为什么修改不了数据类型啊我创建了一个表,但是ponum 定义的长度太短,想给他改长点,用以以下语句,可一直提

为什么修改不了数据类型啊
我创建了一个表,但是ponum 定义的长度太短,想给他改长点,用以以下语句,可一直提示错误
ALTER TABLE purchase_order4 ALTER COLUMN ponum CHARACTER(10);

CREATE TABLE purchase_order4
(
  ponum character(5) NOT NULL,
  qty character(10),
  orderdate date,
  duedate date,
  receiveddate date,
  CONSTRAINT purchase_order4_pkey PRIMARY KEY (ponum)
)
[解决办法]

--查询约束
sp_helpconstraint purchase_order4
--删除约束
ALTER TABLE purchase_order4 
drop constraint purchase_order4_pkey


然后你再修改下字段试试..
[解决办法]
ALTER TABLE purchase_order4  add constraint purchase_order4_pkey PRIMARY KEY(ponum)

还有别忘了再加回约束..
[解决办法]


CREATE TABLE  purchase_order4

(
  ponum character(5) NOT NULL,
  qty character(10),
  orderdate datetime,
  duedate datetime,
  receiveddate datetime,
  CONSTRAINT purchase_order4_pkey PRIMARY KEY (ponum)

--改变前字符长度为5
/*
Column_name                                                                                                                      Type                                                                                                                             Computed                            Length      Prec  Scale Nullable                            TrimTrailingBlanks                  FixedLenNullInSource                Collation
-------------------------------------------------------- -------------------------------------------------------- ----------------------------------- ----------- ----- ----- ----------------------------------- ----------------------------------- ----------------------------------- --------------------------------------------------------


ponum                                                                                                                            char                                                                                                                             no                                  5                       no                                  no                                  no                                  Chinese_PRC_CI_AS
qty                                                                                                                              char                                                                                                                             no                                  10                      yes                                 no                                  yes                                 Chinese_PRC_CI_AS


orderdate                                                                                                                        datetime                                                                                                                         no                                  8                       yes                                 (n/a)                               (n/a)                               NULL
duedate                                                                                                                          datetime                                                                                                                         no                                  8                       yes                                 (n/a)                               (n/a)                               NULL


receiveddate                                                                                                                     datetime                                                                                                                         no                                  8                       yes                                 (n/a)                               (n/a)                               NULL

*/
alter table  purchase_order4
 drop constraint purchase_order4_pkey
go
alter table purchase_order4
 alter column ponum character(10) not null
go
alter table purchase_order4
 add constraint purchase_order4_pkey primary key clustered (ponum)
--改变后字符长度为10
/*
Column_name                                                                                                                      Type                                                                                                                             Computed                            Length      Prec  Scale Nullable                            TrimTrailingBlanks                  FixedLenNullInSource                Collation


-------------------------------------------------------- -------------------------------------------------------- ----------------------------------- ----------- ----- ----- ----------------------------------- ----------------------------------- ----------------------------------- --------------------------------------------------------
ponum                                                                                                                            char                                                                                                                             no                                  10                      no                                  no                                  no                                  Chinese_PRC_CI_AS
qty                                                                                                                              char                                                                                                                             no                                  10                      yes                                 no                                  yes                                 Chinese_PRC_CI_AS


orderdate                                                                                                                        datetime                                                                                                                         no                                  8                       yes                                 (n/a)                               (n/a)                               NULL
duedate                                                                                                                          datetime                                                                                                                         no                                  8                       yes                                 (n/a)                               (n/a)                               NULL


receiveddate                                                                                                                     datetime    
*/


[解决办法]
先取消主键约束再修改,修改完再创建主键约束,不过我比较感兴趣你报什么错?
[解决办法]
需要更改的列有约束,需要先取消约束,再进行更改
ALTER TABLE purchase_order4 DROP CONSTRAINT purchase_order4_pkey
GO
ALTER TABLE purchase_order4 ALTER COLUMN ponum character(10) not null
GO
ALTER TABLE purchase_order4  ADD CONSTRAINT purchase_order4_pkey PRIMARY KEY( ponum )

[解决办法]
去掉约束,修改,在添加约束。
[解决办法]
有主键约束了,要先删除再修改。或者有权限,直接GUI界面修改。
[解决办法]
个人觉得和约束没关系,原因可能是原字段中中已有数据了
[解决办法]
说出你报的错误
[解决办法]
我试了 只要在 character前加type 就可以了 我用的是postgresql

热点排行