Sql 在Oracle中创建表
我的要求是,Sql 在Oracle中创建表,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我的要求是,accno列没有空值,也没有重复项。name列没有空值,只接受A到Z(没有其他类似的数字或*$)。acctype列是一个仅允许('S','C','R')的字符,balance列没有空值。如果acktype是S,则余额应>=5000,当C时余额应>10000,当R>=5000 我试图将此应用于: create table kcb_acc_tab (accno varchar2(20) constraint kcb_acc_Pk primary key, na
accno
列没有空值,也没有重复项。name列没有空值,只接受A到Z(没有其他类似的数字或*$)。acctype
列是一个仅允许('S','C','R')的字符,balance
列没有空值。如果acktype
是S,则余额应>=5000,当C时余额应>10000,当R>=5000
我试图将此应用于:
create table kcb_acc_tab
(accno varchar2(20)
constraint kcb_acc_Pk
primary key,
name varchar2(20)
constraint kcb_name_NN
Not null
constraint kcb_name_CK
check((name =upper(name)) and (name like '[(A-Z)]')),
Acctype char
constraint kcb_acctype_ck
check (acctype in('S' ,'C' ,'R')) ,
Doo timestamp
default sysdate ,
bal number(7,2) kcb_bal_NN
constraint kcb_bal_ck
check((aacctype ='S' and bal >=5000) or
(acctype = 'C' and bal >=10000) or
(acctype ='R' and bal >=5000));
这听起来像是一个完美的用例,我认为这是您在约束中使用
like
的意图
我已经相当清楚地整理了您的陈述,您在kcb_bal__ck
的定义中缺少了一个逗号,我在结尾添加了约束条件并添加了空格。对我来说,这使我更容易看到发生了什么以及哪里可能有错误
create table kcb_acc_tab(
accno varchar2(20) not null
, name varchar2(20) not null
, acctype char(1) not null -- missing size of column
, doo timestamp default sysdate not null -- missing not null
, bal number(7,2) not null
, constraint kcb_acc_pk primary key (accno)
, constraint kcb_name_ck check ( regexp_like(name, '[A-Z]', 'c' ) )
, constraint kcb_acctype_ck check ( acctype in ( 'S' ,'C' ,'R' ) )
-- acctype was spelled incorrectly.
, constraint kcb_bal_ck check( ( acctype ='S' and bal >= 5000 )
or ( acctype = 'C' and bal >= 10000 )
or ( acctype ='R' and bal >= 5000 )
) -- this parenthesis was missing
)
这里有一个例子来演示
这与您自己的主要区别在于
regexp_like(名称,[A-Z],'c')
。这确保列名称中的字符仅包含在A-Z组中,即大写拉丁字母集。match_参数'c'
指定匹配应区分大小写。默认区分大小写由NLS_排序参数决定,因此您可能不需要明确指定,但这样做是明智的。如果希望用户向您提供答案,您需要澄清您的意图和问题。你的问题是什么?是的,这是我们在@trideceth12工作时一直无法达成一致的编码标准。我更喜欢这种方式,因为它很容易看出你是否错过了一个。当然,每个人都有自己的权利。