Home » SQL & PL/SQL » SQL & PL/SQL » need help getting primary key from table
need help getting primary key from table [message #403341] Thu, 14 May 2009 23:12 Go to next message
callforhelp
Messages: 1
Registered: May 2009
Junior Member
hey guys, I'm trying to get an automatically generated primary key from a table called Product and insert it into another table.

CREATE TABLE Product (
name VARCHAR2 ( 100 ) NOT NULL,
price NUMBER ( 5 ) NOT NULL,
quantity NUMBER ( 5 ) NOT NULL,
manufact VARCHAR2 ( 100 ) NOT NULL,
Product_ID NUMBER ( 10 ) NOT NULL,
CONSTRAINT PK_Product6 PRIMARY KEY (Product_ID)
);
CREATE TABLE Manufacturer (
name VARCHAR2 ( 100 ) NOT NULL,
price NUMBER ( 5 ) NOT NULL,
Manufacturer_ID NUMBER ( 10 ) NOT NULL,
Product_ID NUMBER ( 10 ) NOT NULL,
CONSTRAINT PK_Manufacturer7 PRIMARY KEY (Manufacturer_ID)
);
ALTER TABLE Manufacturer ADD ( CONSTRAINT FK_Manufacturer6 FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID));

it complains if i try to create a row without adding in Product_ID like so:

INSERT INTO manufacturer (name,price) values ('ferrari',100)

and I can't get it to 'get' Product_ID from product like so:

INSERT INTO manufacturer (name,price,Product_ID) values ('ferrari',100,Product.Product_ID)

please help me Sad
Re: need help getting primary key from table [message #403344 is a reply to message #403341] Thu, 14 May 2009 23:20 Go to previous message
Michel Cadot
Messages: 64151
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
You have to create a TRIGGER on insert for this.

PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Regards
Michel
Previous Topic: Year to Date Amounts
Next Topic: Need to compare two sqls
Goto Forum:
  


Current Time: Fri Dec 09 21:36:15 CST 2016

Total time taken to generate the page: 0.12544 seconds