創(chuàng)建表時設置非空約束
create table userinfo_1(id number(6,0),username varchar2(20) not null,userpwd varchar2(20) not null);向表中插入非空約束(設置之前表中最好沒有數(shù)據(jù))
alter table userinfomodify username varchar2(20) not null;去除表中非空約束
alter table userinfomodify username varchar2(20) null;創(chuàng)建表時設置主鍵約束
create table userinfo_p(id number(6,0) PRimary key(設置主鍵),username varchar2(20),userpwd varchar2(20));在創(chuàng)建表時對兩個字段id,username共同設置主鍵約束
eate table userin_p1(id number(6,0),username varchar2(20),userpwd varchar2(20),constraint pk_id_username primary key(id,username));查詢主鍵 主鍵約束名
desc user_constraintsOK
select constraint_name from user_constraints where table_name='USERINFOP1';更改約束名稱
alter table userinforename constraint pk_id(舊名字) to new_pk_id(新名字);禁用主鍵約束
alter table userinfodisable constraint new_pk_id;刪除主鍵約束(已知約束名字)
alter table userinfodrop constrain new_pk_id;或直接刪除主鍵約束
alter table userinfodrop primary key;查看約束名字和狀態(tài)
select constraint_name,status from user_constraints where table_name='USERINFO';在創(chuàng)建表時設置外鍵約束(兩個表的字段之間的約束)
在列級設置外鍵約束
create table typeinfo(主表)(typeid varchar2(10) primary key,typename varchar2(20));create table userinfo_f(從表)(id varchar2(10 primary key,username varchar2(20),typeid_new varchar2(10) (對主表的typeid設置外鍵約束)references typeinfo(typeid));向從表中的typeid_new輸入值: 主表中插入值
insert into typeinfo values(1,1);從表中插入值(插入的值必須是主表中的值或空值)
insert into userinfo_f(id,typeid_new)values(1,1);在表級設置外鍵約束
create table userinfo_f2(id varchar2(10) primary key,username varchar2(20),typeid _new varchar2(10),constaint fk_typeid_new foreign key(typeid_new)references typeinfo(typeid));新聞熱點
疑難解答