Primary key ---- Urgent plse [message #11192] |
Thu, 11 March 2004 00:24 |
K.B.Shesha Saai
Messages: 14 Registered: February 2004
|
Junior Member |
|
|
In oracle, I have a table dept .. in which there are columns deptno, dname. I have created primary key on both the columns. Now i am creating emp table, in which i have columns like empno, ename and deptno. For deptno of emp table, i want to refer to the deptno of the dept table. Since there was a composite primary key on the dept table (on two cols), how can i refer deptno of emp column to deptno column of dept table ?
Please clarify
Thanks
|
|
|
|
|
Re: Primary key ---- Urgent plse [message #12204 is a reply to message #11205] |
Fri, 07 May 2004 00:05 |
Amit
Messages: 166 Registered: February 1999
|
Senior Member |
|
|
But, After creating a unique contraint on deptno in dept_table, then it won't allow to insert duplicate values for deptno which defeates the reason of creating composite Pk on deptno,dname.If I am having duplicate values in deptno because of which I had to create composite Pk then after creating Uk on deptno, I won't be able to insert deplicate records for deptno because of UK violations.
Pl. advise, because I am facing this problem in one of my scenario.
|
|
|
Re: Primary key ---- Urgent plse [message #12218 is a reply to message #12204] |
Fri, 07 May 2004 22:15 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
You can create an additional table, such as the deptno_table in the example below.
scott@ORA92> create table deptno_table
2 (deptno number,
3 constraint deptno_table_deptno_pk primary key (deptno))
4 /
Table created.
scott@ORA92> create table dept_table
2 (deptno number,
3 dname varchar2(14),
4 constraint dept_table_deptno_dname_pk primary key (deptno, dname),
5 constraint dept_table_deptno_fk foreign key (deptno) references deptno_table (deptno))
6 /
Table created.
scott@ORA92> create table emp_table
2 (empno number,
3 ename varchar2(10),
4 deptno number,
5 constraint emp_table_deptno_fk foreign key (deptno) references deptno_table (deptno))
6 /
Table created.
scott@ORA92> insert into deptno_table (deptno) values (1)
2 /
1 row created.
scott@ORA92> insert into dept_table (deptno, dname) values (1, 'deptno1a')
2 /
1 row created.
scott@ORA92> insert into dept_table (deptno, dname) values (1, 'deptno1b')
2 /
1 row created.
scott@ORA92> insert into emp_table (empno, ename, deptno) values (10, 'name1', 1)
2 /
1 row created.
scott@ORA92> insert into emp_table (empno, ename, deptno) values (20, 'name2', 1)
2 /
1 row created.
scott@ORA92> select * from deptno_table
2 /
DEPTNO
----------
1
scott@ORA92> select * from dept_table
2 /
DEPTNO DNAME
---------- --------------
1 deptno1a
1 deptno1b
scott@ORA92> select * from emp_table
2 /
EMPNO ENAME DEPTNO
---------- ---------- ----------
10 name1 1
20 name2 1
|
|
|