如何在sybase中添加唯一列?
我有一个 sybase 数据库表,需要在其中添加一个新列。
条件:该列不能允许空值并且是唯一的。 实现此目的的alter table sql是什么?
编辑: 它是一个 varchar 类型列。是的,表现在是空的,但是当填充时,可以确保填充唯一值。
我厌倦了执行,
alter table Test add Name varchar not null unique
我收到错误消息,说必须指定默认值,因为给出的不是 null。 但我想添加唯一约束,所以我真的需要指定默认值吗?
谢谢
I have a sybase db table in which i need to add a new column.
The conditions: The column must not allow nulls and be unique.
what is the alter table sql to achieve this?
EDIT:
It is a varchar type column.Yes the table as of now is empty, but when filled it is ensured that unique values would be filled in.
I tired executing
alter table Test add Name varchar not null unique
i get error saying default value must be specified as not null is given.
but i want to add unique constraint so do i really need to specify default?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唯一值被指定为列索引的一部分,而不是在列定义本身中。
尝试:
ASE 参考手册< /a> 可以帮助提供更多细节。
Unique values are specified as part of an index on the column, not in the column definition itself.
Try:
The ASE reference manual can help with more detail.
创建表后,Sybase ASE 不允许直接添加 NOT NULL 列,除非为该列指定了默认值。但是,如果表仍然为空,您可以执行以下操作 -
alter table Test add Name varchar(100) null
alter table 测试修改 Name varchar(100) NOT NULL
,您将看到可以使用以下步骤将 Name 列修改为 NOT NULL 列。这是因为此时 Sybase 服务器检查表中没有数据,因此不检查 NOT NULL 约束,并将列设置为 NOT NULL。因此,我们可以跳过默认约束。
如果 Test 表中已经存在一些数据,那么我们需要在步骤 1 和 2 之间添加一个步骤,这将向表中的现有行添加默认值。这可以通过针对先前数据的脚本来完成,然后执行步骤 2。
要使该列仅允许该列的唯一值,您需要使用以下语法添加唯一键约束 -
alter table Test 添加约束 UK1 unique(Name )
Once a table has been created Sybase ASE does not allow addition of NOT NULL column directly unless a default is specified for the column. However, if the table is still empty you can do the following -
alter table Test add Name varchar(100) null
alter table Test modify Name varchar(100) NOT NULL
and you will see that you are able to modify the Name column to a NOT NULL column using these steps. This is because at this time Sybase server checks as there is no data in the table hence the NOT NULL constraint is not checked and the column is made NOT NULL. Hence, we are able to skip the default constraint.
In case there would have been some data already present in the table Test, then we need to add one more step in between steps 1 and 2 which will add default values to the existing rows in the table. This can be done via a script for previous data and then following the step 2.
To make the column only allow unique values for the column you need to add a unique key constraint using the following syntax -
alter table Test add constraint UK1 unique(Name)