怎样为一个表设置主键和外键??要完整的SQL语句!!!
我建立了一个表名为:test
A vachar(10)
B vachar(10)
c vachar(10)
我要设置A,为主键,b为外键与test2 的B字段关联!
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)
)
/
例:
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)
);
-- 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
);