Oracle: using foreign keys in object relation database - database

I have a question about using primary keys and foreign keys in an object relational database in oracle. Below is my sql code and insert statements. I am trying to create a simple database with student, course, teacher and department. but whenever I run the below select statement, I do not see the DepartmentID and TeacherID that I entered. Instead i get [HR.DEPARTMENT_OBJTYP] as the value for departmentID and likewise for teacherID. is this because I have referenced the whole department rather than just the departmentID in the course table? I do not know how to use foreign keys and query them properly in an object relational database, and have not been able to find any useful easy to understand info on it. could someone please show me where I am going wrong?
for example in Course_objtyp I do... TeacherID REF Teacher_objtyp. is this the correct way to reference, I am not sure as i only want to reference the teacherID not the whole Teacher_objtyp?
CREATE TYPE Student_objtyp AS OBJECT (
FName VARCHAR2(20),
LName VARCHAR2(20),
StudentID NUMBER
);
/
CREATE TABLE Student_objtab OF Student_objtyp (StudentID PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
CREATE TYPE Department_objtyp AS OBJECT (
DeptID NUMBER,
DeptName VARCHAR2(20)
);
/
CREATE TABLE Department_objtab OF Department_objtyp (DeptID PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
CREATE TYPE Teacher_objtyp AS OBJECT (
TeacherID NUMBER,
FName VARCHAR2(20),
LName VARCHAR2(20)
);
/
CREATE TABLE Teacher_objtab OF Teacher_objtyp (TeacherID PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
CREATE TYPE Course_objtyp AS OBJECT (
CourseName VARCHAR(20),
CourseID NUMBER,
DeptID REF Department_objtyp,
TeacherID REF Teacher_objtyp
);
/
CREATE TABLE Course_objtab OF Course_objtyp (
PRIMARY KEY (CourseID),
FOREIGN KEY (DeptID) REFERENCES Department_objtab,
FOREIGN KEY (TeacherID) REFERENCES Teacher_objtab)
/
INSERT INTO Student_objtab VALUES('bill','smitts',1);
INSERT INTO Student_objtab VALUES('bob','jo',2);
INSERT INTO Teacher_objtab VALUES(1,'Mr','Higgins');
INSERT INTO Department_objtab VALUES(1111,'Science');
INSERT INTO Course_objtab
SELECT 'Chem101',001,
REF(D),
REF(T)
FROM Department_objtab D, Teacher_objtab T
WHERE D.DeptID = 1111 and T.TeacherID = 1;
the very simple select statement i am trying to run:
select * from Course_objtab;

The below works fine but I am not sure if you are looking for that.
CREATE TYPE Course_objtyp AS OBJECT (
CourseName VARCHAR(20),
CourseID NUMBER,
DeptID Number,
TeacherID Number
);
CREATE TABLE Course_objtab OF Course_objtyp (
PRIMARY KEY (CourseID),
FOREIGN KEY (DeptID) REFERENCES Department_objtab(DeptID),
FOREIGN KEY (TeacherID) REFERENCES Teacher_objtab(TeacherID));
INSERT INTO Course_objtab
SELECT 'Chem101',001, D.DeptID, T.TeacherID
FROM Department_objtab D, Teacher_objtab T
WHERE D.DeptID = 1111 and T.TeacherID = 1;

Related

how to insert data in subtype table and get key from super type in T-SQL?

For example got in super type a user:
create table Userr
(
id int primary key identity,
firstName nvarchar(20),
lastName nvarchar(20),
email nvarchar(15) unique not null,
userName nvarchar(15) unique not null,
passKey nvarchar(15) not null,
sex nvarchar(1),
userType nvarchar(1),
depID nvarchar(5) ,
)
Then in sub-types I got student and instructor :
create table Student
(
stuID int primary key,
gradYear date ,
constraint c2 foreign key(stuID) references Userr(id)
)
create table Instructor
(
insID int primary key,
salary money ,
constraint c3 foreign key(insID) references Userr(id)
)
All I want is to add new student and new instructor, how can I do this in an efficient way?
all I want is to add new student and new instructor ,how can I make it in efficient way?
The meaning of "subtype table" is not clear, but in order to INSERT new student and new instructor you should use simple INSERT query.
INSERT Userr(firstName,lastName,email, userName, passKey)
VALUES ('Ronen','Ariely', 'NotForYou','pituach','NoWay')
GO
INSERT Student(stuID, gradYear) VALUES (1, '2001-02-27')
GO
you had to have the referenced user in the userr table first. This mean that you had to first INSERT the referenced user if it does not exists already.

Adding a row to ORDERS table which then increases the ordered number of that product and the customer?

I am trying to learn SQL, and while my journey has been quite successful this far, I have run into problems with triggers. For the record, I am using SQL Server 2016. I would appreciate any help I can get. Please let me know if more details are needed.
Hope it will help
-------------
--cascade delete way
-------------
CREATE TABLE dbo.customers (
customer_id INT NOT NULL
, somedata UNIQUEIDENTIFIER NOT NULL
, CONSTRAINT PK_Customer PRIMARY KEY (customer_id)
);
CREATE TABLE dbo.orders (
OrderID INT NOT NULL
, customer_id INT NOT NULL
, somedata UNIQUEIDENTIFIER NOT NULL
, CONSTRAINT PK_Order PRIMARY KEY (OrderID)
, CONSTRAINT FK_CustomerOrder FOREIGN KEY (customer_id)
REFERENCES dbo.customers (customer_id) ON DELETE CASCADE
);
INSERT INTO dbo.customers (customer_id,somedata)
VALUES (1,NEWID()),(2,NEWID())
INSERT INTO dbo.orders(OrderID,customer_id,somedata)
VALUES (1,1,NEWID()),(2,2,NEWID())
DELETE FROM dbo.customers WHERE customer_id = 1
SELECT * FROM dbo.orders
-------------
--trigger way
-------------
CREATE TABLE dbo.customers1 (
customer_id INT NOT NULL
, somedata UNIQUEIDENTIFIER NOT NULL
, CONSTRAINT PK_Customer1 PRIMARY KEY (customer_id)
);
CREATE TABLE dbo.orders1 (
OrderID INT NOT NULL
, customer_id INT NOT NULL
, somedata UNIQUEIDENTIFIER NOT NULL
, CONSTRAINT PK_Order1 PRIMARY KEY (OrderID)
, CONSTRAINT FK_CustomerOrder1 FOREIGN KEY (customer_id)
REFERENCES dbo.customers1 (customer_id) ON DELETE CASCADE
);
GO
CREATE TRIGGER DELTRIG
ON dbo.customers1
FOR DELETE
AS
DELETE C
FROM dbo.orders1 AS C
INNER JOIN DELETED ON
C.customer_id = DELETED.customer_id
GO
INSERT INTO dbo.customers1 (customer_id,somedata)
VALUES (1,NEWID()),(2,NEWID())
INSERT INTO dbo.orders1(OrderID,customer_id,somedata)
VALUES (1,1,NEWID()),(2,2,NEWID())
DELETE FROM dbo.customers1 WHERE customer_id = 1
SELECT * FROM dbo.orders1
It is much safer if you would simply add a deleted flag in your customer table and in your trigger just update the deleted column to true.
But if you really need to delete the child records after the parent record is deleted, try using the cascade delete. You can refer from to this old post.
How do I use cascade delete with SQL Server?

i need Database table creating

i created my tables and I'm stuck at the last one,
here is the tables that been created correctly
CREATE TABLE Staff (
Staff_ID INT NOT NULL PRIMARY KEY,
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Username VARCHAR(10),
Password VARCHAR(10),
Address VARCHAR(30)
)
CREATE TABLE Category (
Category_ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(30)
)
CREATE TABLE Author (
Author_ID INT NOT NULL PRIMARY KEY,
First_Name VARCHAR(50),
Last_Name VARCHAR(50),
Birth_Place VARCHAR(30),
Birth_Date DATE
)
CREATE TABLE Publisher (
Publisher_ID INT NOT NULL PRIMARY KEY,
Name VARCHAR(50)
)
and this is the one I'm getting an error :
CREATE TABLE Book (
Book_ID INT NOT NULL PRIMARY KEY,
Title VARCHAR(50),
Edition INT(30),
Year_Published INT(4),
FOREIGN KEY (Publisher_ID) REFERENCES Publisher(Publisher_ID),
FOREIGN KEY (Author_ID) REFERENCES Author(Author_ID),
FOREIGN KEY (Category_ID) REFERENCES Category(Category_ID)
)
the error says:
"ORA-00907: missing right parenthesis"
INT can not have a scale associated with it so YEAR_PUBLISHED and EDITION are incorrect definitions.
I believe that, generally, you would be better off sticking to NUMBER for numeric datatypes, eg NUMBER(4), NUMBER(30).
In the database the INT datatype is simply a sub-type of NUMBER so you aren't gaining anything by using it:
type NUMBER is NUMBER_BASE;
subtype INTEGER is NUMBER(38,0);
subtype INT is INTEGER;
If you want to see the definitions for the various 'other' numeric datatypes take a look at the SYS.STANDARD package.
The INT data type does not have a precision.
You also need to define the Publisher_ID, Author_ID and Category_ID columns.
It is good practice to name your constraints.
A PRIMARY KEY column is both NOT NULL and UNIQUE so you do not need to include a second NOT NULL constraint.
Like this:
CREATE TABLE Book (
Book_ID INT CONSTRAINT Book__Book_id__PK PRIMARY KEY,
Title VARCHAR(50),
Edition INT,
Year_Published INT,
Publisher_ID INT CONSTRAINT Book__Publisher_ID__FK REFERENCES Publisher(Publisher_ID),
Author_ID INT CONSTRAINT Book__Author_id__FK REFERENCES Author(Author_ID),
Category_ID INT CONSTRAINT Book__category_ID__FK REFERENCES Category(Category_ID)
);

Using the last inserted record's ID as a foreign key

I am making a database which links together two tables via the primary key of the first table. The one with the primary key which links the two is created first but how do i make the second record get the ID of the record I just created?
create table Person
(
Person_ID int IDENTITY(100000,1) primary key,
First_Name varchar(20) not null,
Last_Name varchar(20) not null,
)
create table Employee
(
Employee_ID int identity(100000,1) primary key,
Person_ID int references Person(Person_ID),
Employee_Type varchar(10)
)
insert into Person(First_Name, Last_Name) values ('Michael', 'Chu');
insert into Employee(Person_ID, Employee_Type,) values (????????, 'Admin');
I've had a look at the 'last()' function but not really sure how to utilise that. Other then that, I have no idea. Can someone help me out or guide me in the right direction.
try this:
create table Person
(
Person_ID int IDENTITY(100000,1) primary key,
First_Name varchar(20) not null,
Last_Name varchar(20) not null,
)
create table Employee
(
Employee_ID int identity(100000,1) primary key,
Person_ID int references Person(Person_ID),
Employee_Type varchar(10)
)
DECLARE #myID AS INT
insert into Person(First_Name, Last_Name) values ('Michael', 'Chu');
SET #myID = ##IDENTITY
insert into Employee(Person_ID, Employee_Type,) values (#myID , 'Admin');

There are no primary or candidate keys in the referenced table

I have two tables:
Table1
c_ID float - PK
Field1
Field2
Field3
p_ID [uniqueidentifier]
and
Table2
p_ID nvarchar
s_ID float
where both p_ID and s_ID are part of the primary key.
I tried to create a foreign key on Table1:
ALTER TABLE Table1
ADD CONSTRAINT FK_Table1_Table2 FOREIGN KEY (p_ID)
REFERENCES Table2(p_ID)
and I got an error:
There are no primary or candidate keys in the referenced table
'dbo.Table2' that match the referencing column list in the foreign key
'FK_Table1_Table2'.
Am I getting this error because data type of p_ID is uniqueidentifier and in Table2 p_ID is nvarchar? Is there a workaround?
you just have to use a FK that includes the same columns that are in the PK
create table ParentTest (
SomeNumber int,
Name varchar(25),
PRIMARY KEY (SomeNumber, Name)
)
create table ChildTest (
SomeValue varchar(25),
SomeNumber int,
Name varchar(25),
FOREIGN KEY (SomeNumber, Name) references ParentTest (SomeNumber, Name)
)

Resources