怎样为一个表设置主键和外键??要完整的SQL语句!!!

我建立了一个表名为:test
A vachar(10)
B vachar(10)
c vachar(10)
我要设置A,为主键,b为外键与test2 的B字段关联!
[111 byte] By [huangbin277-微笑面对-光荣下岗] at [2008-6-10]
# 1
你真懒!

ALTER TABLE JL.EMP
ADD CONSTRAINT SYS_C005888
FOREIGN KEY (JOB)
REFERENCES JL.EMP_P (JOB)
ON DELETE CASCADE
/

ALTER TABLE STGAJK.USERB
ADD CONSTRAINT K_USERB
PRIMARY KEY (USERID)
USING INDEX PCTFREE 10
INITRANS 2
MAXTRANS 255
TABLESPACE TBS
STORAGE(INITIAL 16K
NEXT 16K
MINEXTENTS 1
MAXEXTENTS 121
PCTINCREASE 50
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT)
LOGGING
/
jlandzpa-jlandzpa at 2007-10-28 > top of Msdn China Tech,Oracle,开发...
# 2
alter table add primary key(a);
alter table add constraint 外键约束名 foreign key (b) reference test2 (b);
black_dragon-半仙 at 2007-10-28 > top of Msdn China Tech,Oracle,开发...
# 3
CREATE TABLE SCOTT.EMP
(
EMPNO NUMBER(4,0) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4,0),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2,0),
CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPTNO) REFERENCES SCOTT.DEPT (DEPTNO),
CONSTRAINT PK_EMP PRIMARY KEY (EMPNO)
)
/
sun9989-一品黄山 at 2007-10-28 > top of Msdn China Tech,Oracle,开发...
# 4
例:
CREATE TABLE classes (
department CHAR(3),
course NUMBER(3),
description VARCHAR2(2000),
max_students NUMBER(3),
current_students NUMBER(3),
num_credits NUMBER(1),
room_id NUMBER(5),
CONSTRAINT classes_department_course
PRIMARY KEY (department, course),
CONSTRAINT classes_room_id
FOREIGN KEY (room_id) REFERENCES rooms (room_id)
);
bzszp-SongZip at 2007-10-28 > top of Msdn China Tech,Oracle,开发...
# 5
-- Create table
create table T_PXRS
(
XMBH CHAR(20) not null,
JGBH CHAR(15) not null,
XQRS NUMBER,
SZDWBH CHAR(15)
)
tablespace USERS
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 128K
next 128K
minextents 1
maxextents 4096
pctincrease 0
);
-- Create/Recreate primary, unique and foreign key constraints
alter table T_PXRS
add primary key (XMBH,JGBH)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 128K
next 128K
minextents 1
maxextents 4096
pctincrease 0
);
-- Create/Recreate indexes
create index XIF39T_PXRS on T_PXRS (JGBH)
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 128K
next 128K
minextents 1
maxextents 4096
pctincrease 0
);
create index XIF9T_PXRS on T_PXRS (XMBH)
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 128K
next 128K
minextents 1
maxextents 4096
pctincrease 0
);
CHENGXB-阿困 at 2007-10-28 > top of Msdn China Tech,Oracle,开发...