oracle复合检查约束
假设我想在表列中只允许以数字开头的字符串,并且其中不应包含“$”。该表上的检查约束是什么样子的?
Say I want to allow in a table column only strings that begin with a numeral and should not contain '$' in it. How would a check constraint on this table look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 Oracle 10g+,您可以在 CHECK 约束中使用 Oracle 的正则表达式功能:
参考:
Assuming Oracle 10g+, you can use Oracle's regex functionality in a CHECK constraint:
References:
听起来您需要使用 Oracle 的正则表达式。有关相应 IF 约束的语法要求的有用链接可以在下面找到
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
Sounds like you need to use Oracle's Regular Expressions. Useful link for syntax requirement for the corresponding IF constraint can be found below
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
正则表达式看起来像
^\d[^$]*$
我不了解 Oracle,所以如果这实际上对您没有帮助,请原谅我。
The regex would look like
^\d[^$]*$
I don't know Oracle so forgive me if this doesn't actually help you.
我会做类似
ALTER TABLE YOUR_TABLE 的事情
ADD CONSTRAINT col_regx CHECK (substr(column_name,1,1) between '0' and '9' and column_name not like '%$%')
(未经测试,但可能比正则表达式更快)
I would do something like
ALTER TABLE YOUR_TABLE
ADD CONSTRAINT col_regx CHECK (substr(column_name,1,1) between '0' and '9' and column_name not like '%$%')
(not tested but probably faster than a regex)