Create view oracle sql - sql

I am making a database with tv-shows and users.
These users can follow each other and are saved in the database by an UserID.
Now I wan't to create a view with the names of the users instead of the UserID's.
--Drop all tables.
DROP TABLE Series CASCADE CONSTRAINTS;
DROP TABLE User1 CASCADE CONSTRAINTS;
DROP TABLE FollowingSeries CASCADE CONSTRAINTS;
DROP TABLE FollowingUser CASCADE CONSTRAINTS;
DROP TABLE Episode CASCADE CONSTRAINTS;
DROP TABLE Watched CASCADE CONSTRAINTS;
DROP VIEW Test CASCADE CONSTRAINTS;
--Create all tables.
CREATE TABLE Series
( SeriesID NUMBER(5) primary key,
Name VARCHAR2(100) NOT NULL,
Status VARCHAR2(20),
Premiered DATE,
Genre VARCHAR2(100),
Country VARCHAR2(100),
Network VARCHAR2(100),
Runtime VARCHAR2(25)
);
CREATE TABLE User1(
UserID NUMBER(5) primary key,
Username VARCHAR2(100) NOT NULL,
Country varchar2(100),
Gender VARCHAR2(10),
Birthday VARCHAR2(100),
About_me VARCHAR2(300),
Password VARCHAR2(100) NOT NULL
);
CREATE TABLE FollowingSeries (
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Ignore1 CHAR(1) DEFAULT 0,
constraint FollowingS_pk primary key(UserID, SeriesID),
constraint FollowingS_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint FollowingS_fk2 foreign key(SeriesID) REFERENCES Series(SeriesID)
);
CREATE TABLE FollowingUser (
UserID1 NUMBER(5) NOT NULL,
UserID2 NUMBER(5) NOT NULL,
constraint FollowingU_pk primary key(UserID1, UserID2),
constraint FollowingU_fk1 foreign key(UserID1) REFERENCES User1(UserID) ON DELETE SET NULL,
constraint FollowingU_fk2 foreign key(UserID2) REFERENCES User1(UserID) ON DELETE SET NULL
);
CREATE TABLE Episode(
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
Name varchar2(100) NOT NULL,
Airdate DATE,
Summary varchar2(1500),
constraint episode_pk primary key(SeriesID, Season, Episode),
constraint episode_fk foreign key(SeriesID) REFERENCES Series(SeriesID) ON DELETE SET NULL
);
CREATE TABLE Watched(
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
constraint watched_pk primary key(UserID, SeriesID, Season, Episode),
constraint watched_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint watched_fk4 foreign key(SeriesID, Season, Episode) REFERENCES Episode(SeriesID, Season, Episode)
);
--Add users.
INSERT INTO User1 VALUES (1, 'StefPhilipsen', 'Netherlands', 'Male', TO_DATE('02-01-1994','DD-MM-YYYY'), 'Ik ben Stef', 'abcd1234');
INSERT INTO User1 VALUES (2, 'Dorothy142', 'America', 'Female', TO_DATE('01-10-1963','DD-MM-YYYY'), 'I love mountainbiking', 'Oow6sai4Ie');
INSERT INTO User1 VALUES (3, 'Rudo12', 'England', 'Male', TO_DATE('05-6-1985','DD-MM-YYYY'), 'I watched Breaking Bad and a lot more', 'Oph9Ge2yai');
INSERT INTO User1 VALUES (4, 'JohnSmith', 'England', 'Male', TO_DATE('27-9-1945','DD-MM-YYYY'), 'I like potatoes.', 'phiShaip0c');
INSERT INTO User1 VALUES (5, 'EulaWGibson', 'America', 'Female', TO_DATE('12-8-1990','DD-MM-YYYY'), 'I''m Eula. I live in massachusetts and I''m happily married with my husband Hank.', 'EiM5wii2am8');
INSERT INTO User1 VALUES (6, 'Pedro', 'Spain', 'Male', TO_DATE('12-10-1945','DD-MM-YYYY'), 'I love watching Arrow.', 'Gith0yaiw');
INSERT INTO User1 VALUES (7, 'TravisC', 'England', 'Male', TO_DATE('12-7-1970','DD-MM-YYYY'), 'My favorite TV-shows are Modern Family and Dexter.', 'gie9aiPh2Ii');
INSERT INTO User1 VALUES (8, 'DonellaScott', 'America', 'Female', TO_DATE('31-1-1996','DD-MM-YYYY'), 'Living the life.', 'Gued1996');
--Who follows which series.
INSERT INTO FollowingSeries(UserID, SeriesID) VALUES (1,1);
INSERT INTO FollowingSeries(UserID, SeriesID) VALUES (1,2);
INSERT INTO FollowingSeries(UserID, SeriesID) VALUES (1,3);
INSERT INTO FollowingSeries VALUES (1,4,1);
--Who follows who.
INSERT INTO FollowingUser VALUES(1,2);
INSERT INTO FollowingUser VALUES (2,3);
INSERT INTO FollowingUser VALUES (3,4);
INSERT INTO FollowingUser VALUES (1,3);
INSERT INTO FollowingUser VALUES (5,8);
INSERT INTO FollowingUser VALUES (5,6);
INSERT INTO FollowingUser VALUES (6,5);
-- Which episodes are watched by whom.
INSERT INTO Watched VALUES(1, 4, 1, 1);
INSERT INTO Watched VALUES(1, 4, 1, 2);
INSERT INTO Watched VALUES(1, 4, 1, 3);
INSERT INTO Watched VALUES(1, 4, 1, 4);
INSERT INTO Watched VALUES(1, 4, 1, 5);
INSERT INTO Watched VALUES(1, 4, 1, 6);
INSERT INTO Watched VALUES(1, 4, 1, 7);
This is what I tried but then I get an error message
--Create view
CREATE VIEW follownaam AS
SELECT followerUser.Username AS Follower, followingUser.Username AS Following
FROM FollowingUser AS fu
INNER JOIN User1 followerUser ON(fu.UserID1 = followerUser.Username)
INNER JOIN User1 followingUser ON(fu.UserID2 = followingUser.Username)
WHERE fu.UserID1 = 1;
[Err] ORA-00911: invalid character
The table I have exists of 2 UserID's. But now I want exactly the same table, but this view must contain the Usernames of these UserID's.

This should work (although i don't understand why You want a view only for user with ID = 1).
CREATE VIEW follownaam AS
SELECT
followerUser.Username AS Follower, followingUser.Username AS Following
FROM FollowingUser fu
INNER JOIN User1 followerUser ON(fu.UserID1 = followerUser.UserID)
INNER JOIN User1 followingUser ON(fu.UserID2 = followingUser.UserID)
WHERE fu.UserID1 = 1;
I changed Username to UserId because it made much more sense.
Edit:
Here's sqlfiddle i created to test it and it works.

Related

I am receiving 'FOREIGN KEY constraint failed' errors but cannot find the source

I have these four tables that represent a hardware store:
PRAGMA foreign_keys = 1;
DROP TABLE IF EXISTS transactions;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS tools;
DROP TABLE IF EXISTS departments;
CREATE TABLE departments (
name PRIMARY KEY NOT NULL
);
CREATE TABLE tools (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tool_name TEXT NOT NULL UNIQUE,
tool_price DECIMAL(5, 2),
tool_department VARCHAR(250),
FOREIGN KEY (tool_department) REFERENCES departments(name) ON DELETE CASCADE
);
CREATE TABLE customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT NOT NULL UNIQUE,
last_name TEXT NOT NULL UNIQUE,
phone_number INTEGER UNIQUE NOT NULL
);
CREATE TABLE transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER,
customer_first_name TEXT NOT NULL,
customer_last_name TEXT NOT NULL,
customer_phone_number INTEGER NOT NULL,
tool_purchased TEXT NOT NULL,
item_quantity INTEGER NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
FOREIGN KEY (tool_purchased) REFERENCES tools(tool_name) ON DELETE CASCADE,
FOREIGN KEY (customer_first_name) REFERENCES customers(first_name) ON DELETE CASCADE,
FOREIGN KEY (customer_last_name) REFERENCES customers(last_name) ON DELETE CASCADE,
FOREIGN KEY (customer_phone_number) REFERENCES customers(phone_number) ON DELETE CASCADE
);
I am trying to insert this data into my tables:
INSERT INTO tools(tool_name, tool_price, tool_department)
VALUES
('Snow shovel', 16.50, 'Home & Garden'),
('Work light', 29.99, 'Electrical'),
('Wheelbarrow', 89.99, 'Home & Garden'),
('Pipe Wrench', 18.99, 'Plumbing'),
('Pipe Cutter', 36.36, 'Plumbing'),
('Rake', 15.45, 'Home & Garden');
INSERT INTO customers(first_name, last_name, phone_number)
VALUES
('John', 'Smith', 1111111111),
('Jane', 'Doe', 2222222222);
INSERT INTO transactions(customer_id, customer_first_name, customer_last_name, customer_phone_number, tool_purchased, item_quantity)
VALUES
(1, 'John', 'Smith', 1111111111, 'Work light', 1),
(1, 'John', 'Smith', 1111111111, 'Pipe Cutter', 2),
(2, 'Jane', 'Doe', 2222222222, 'Snow shovel', 3),
(2, 'Jane', 'Doe', 2222222222, 'Work light', 1),
(2, 'Jane', 'Doe', 2222222222, 'Pipe Wrench', 1),
(2, 'Jane', 'Doe', 2222222222, 'Pipe Cutter', 1),
(1, 'John', 'Smith', 1111111111, 'Wheelbarrow', 3);
However, I am getting these errors back:
Error: near line 1: FOREIGN KEY constraint failed
Error: near line 15: FOREIGN KEY constraint failed
You haven't defined the type of column name in table departments! Must be VARCHAR(250) like in table tools (FK).
CREATE TABLE departments (
name VARCHAR(250) PRIMARY KEY NOT NULL
);
Correct departments table and insert department in it in order to reference tools to this dept. You cannot reference a tool to department that does not exist yet. Also read docs about column types TEXT and VARCHAR.

Why is my FOREIGN KEY constraint not working:why are the values not present in the foreign column inserted anyway?

I am using the Online SQL Compiler.Even if I insert a StudentId into the Marks table, that is not present in the Student table, it inserts that value without throwing any error.
Why is my foreign key constraint not working? I tried many ways to write the constraint and it still does not work.
My code is:
CREATE TABLE Student
(
StudentId INTEGER PRIMARY KEY,
FName varchar(10) DEFAULT 'maya',
DOJ DATE DEFAULT '20-JAN-2021',
GENDER CHAR(1) CHECK(GENDER IN('M','F')),
ContactNo NUMBER(10) UNIQUE
);
INSERT INTO Student (StudentId, GENDER, ContactNo)
VALUES (1003, 'F', NULL);
INSERT INTO Student (StudentId, FName, GENDER, ContactNo)
VALUES (1004, 'Riya', 'F', 9846669343);
INSERT INTO Student (StudentId, FName, GENDER, ContactNo)
VALUES (1005, 'Diya', 'F', 9846649343);
CREATE TABLE Marks
(
CourseId INTEGER,
StudentId integer,
marks NUMBER(4,2),
FOREIGN KEY (StudentId) REFERENCES Student (StudentId)
ON DELETE CASCADE
);
INSERT INTO Marks VALUES (2, 1003, 80.54);
INSERT INTO Marks VALUES (3, 1004, 91.5);
INSERT INTO Marks VALUES (2, 2001, 93.55);
SELECT * FROM Marks;
My output is:
2|1003|80.54
3|1004|91.5
2|2001|93.55
Here StudentId:2001 is not present in StudentId column of the Student table,but it is inserted anyway.Why is this happening?

Violation of PRIMARY KEY constraint PK_Casting

I'm trying to add a new data base through a script, but getting some errors, already tried to let out the double fields, but that didn't work for me.
USE master
GO
IF EXISTS (SELECT * FROM sysdatabases WHERE NAME = 'MijnFilms')
BEGIN
ALTER DATABASE MijnFilms SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE MijnFilms
END
GO
-- Creatie databank Bibliotheek
CREATE DATABASE MijnFilms
GO
USE MijnFilms
GO
--Creatie tabel AUTEUR
CREATE TABLE ACTEUR
(
Acteur_id int NOT NULL,
ActeurNaam nvarchar(40),
CONSTRAINT PK_Acteur PRIMARY KEY(acteur_id)
)
GO
--Creatie tabel CATEGORIE
CREATE TABLE CASTING
(
Film_id int NOT NULL,
Acteur_id int NOT NULL,
CONSTRAINT PK_Casting PRIMARY KEY(Film_id),
CONSTRAINT FK1_Casting FOREIGN KEY(Acteur_id) REFERENCES Acteur(acteur_id)
)
GO
--Creatie tabel BOEKEN
CREATE TABLE FILM
(
Film_id int NOT NULL,
Titel nvarchar(40),
Jaar smallint,
Score int,
Stemmen int,
CONSTRAINT FK1_Film FOREIGN KEY(Film_id) REFERENCES Casting(film_id),
)
GO
--Opvullen van de tabellen met testdata
INSERT INTO ACTEUR(Acteur_id, ActeurNaam) VALUES (1, 'Tom Hanks')
INSERT INTO ACTEUR(Acteur_id, ActeurNaam) VALUES (2, 'Helen Hunt')
INSERT INTO ACTEUR(Acteur_id, ActeurNaam) VALUES (3, 'Catherine Zeta Jones')
GO
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (1, 1)
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (1, 2)
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (2, 1)
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (2, 3)
GO
INSERT INTO FILM(Film_id, Titel, jaar, Score, Stemmen) VALUES (1, 'Cast Away', 2000, 75,1)
INSERT INTO FILM(Film_id, Titel, jaar, Score, Stemmen) VALUES (2, 'The Terminal', 2004, 62,1)
GO
Right now, I get an error:
Msg 2627, Level 14, State 1, Line 60
Violation of PRIMARY KEY constraint 'PK_Casting'. Cannot insert duplicate key in object 'dbo.CASTING'. The duplicate key value is (2).
Well, you're trying to insert a film_id of 1 (and 2) into casting twice but primary keys need to be unique.
You could use a composite primary key here, consisting of the film's and the actor's ID.
Also the film's ID should be the primary key in film and a foreign key in casting not the other way round. That requires film to be created before casting though. That also applies for the INSERTs -- film has to come before casting.
...
CREATE TABLE film
(film_id integer
NOT NULL,
titel nvarchar(40),
jaar smallint,
score integer,
stemmen integer,
CONSTRAINT pk_film
PRIMARY KEY (film_id));
CREATE TABLE acteur
(acteur_id integer
NOT NULL,
acteurnaam nvarchar(40),
CONSTRAINT pk_acteur
PRIMARY KEY (acteur_id));
CREATE TABLE casting
(film_id integer
NOT NULL,
acteur_id integer
NOT NULL,
CONSTRAINT pk_casting
PRIMARY KEY (film_id,
acteur_id),
CONSTRAINT fk1_casting
FOREIGN KEY (film_id)
REFERENCES film
(film_id),
CONSTRAINT fk2_casting
FOREIGN KEY (acteur_id)
REFERENCES acteur
(acteur_id));
...
INSERT INTO film ...
INSERT INTO acteur ...
INSERT INTO casting ...
You are creating the table with constraints
CREATE TABLE CASTING
(
Film_id int NOT NULL,
Acteur_id int NOT NULL,
CONSTRAINT PK_Casting PRIMARY KEY(Film_id),
CONSTRAINT FK1_Casting FOREIGN KEY(Acteur_id) REFERENCES Acteur(acteur_id)
)
GO
and then you are inserting duplicated records in the table
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (1, 1)
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (1, 2)
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (2, 1)
INSERT INTO CASTING (Film_id, Acteur_id) VALUES (2, 3)
You need to remove the constraint or you need to make COMPOSITE KEY by creating the key on FILM_ID and ACTEUR_ID

SQL Plus table or view does not exist

I am getting an error 'missing right parenthesis' on line 13 (S_dob DATE() line). I am also getting the error 'ORA-00942: table or view does not exist' when trying to insert values into the STUDENT table.
DROP TABLE ENROLLMENT CASCADE CONSTRAINTS PURGE;
DROP TABLE COURSE_SECTION CASCADE CONSTRAINTS PURGE;
DROP TABLE COURSE CASCADE CONSTRAINTS PURGE;
DROP TABLE TERM CASCADE CONSTRAINTS PURGE;
DROP TABLE STUDENT CASCADE CONSTRAINTS PURGE;
DROP TABLE FACULTY CASCADE CONSTRAINTS PURGE;
DROP TABLE LOCATION CASCADE CONSTRAINTS PURGE;
CREATE TABLE ENROLLMENT
(
Sid NUMBER(5) CONSTRAINT PK_ENROLLMENT PRIMARY KEY,
Csecid NUMBER(8),
Grade CHAR(1)
);
INSERT INTO ENROLLMENT
VALUES (100, 1000, 'A');
INSERT INTO ENROLLMENT
VALUES (100, 1003, 'A');
INSERT INTO ENROLLMENT
VALUES (101, 1000, 'C');
INSERT INTO ENROLLMENT
VALUES (102, 1000, 'C');
INSERT INTO ENROLLMENT
VALUES (102, 1001, NULL);
INSERT INTO ENROLLMENT
VALUES (102, 1003, 'I');
CREATE TABLE COURSE_SECTION
(
Csecid NUMBER(8) CONSTRAINT PK_COURSE_SECTION PRIMARY KEY,
Cid NUMBER(6),
Termid NUMBER(5),
Secnum NUMBER(2),
Fid NUMBER(4),
Day VARCHAR2(10),
Locid NUMBER(5),
Maxenrl NUMBER(4),
Currenrl NUMBER(4)
);
INSERT INTO COURSE_SECTION
VALUES (1000, 1, 2, 1, 12, 'MWF', 55, 100, 35);
INSERT INTO COURSE_SECTION
VALUES (1001, 1, 2, 2, 10, 'TTH', 54, 45, 35);
CREATE TABLE COURSE
(
Cid NUMBER(8) CONSTRAINT PK_COURSE PRIMARY KEY,
Callid VARCHAR2(10),
Cname VARCHAR2(30),
Ccredit NUMBER(2)
);
INSERT INTO COURSE
VALUES (1, 'MIS101', 'Intro. to Info. Systems', 3);
INSERT INTO COURSE
VALUES (2, 'MIS321', 'Systems Analysis and Design', 3);
INSERT INTO COURSE
VALUES (3, 'MIS349', 'Intro to Database Management', 3);
CREATE TABLE TERM
(
Termid NUMBER(5) CONSTRAINT PK_TERM PRIMARY KEY,
Tdesc VARCHAR2(20),
Status VARCHAR2(20)
);
INSERT INTO TERM
VALUES (1, 'Spring 2004', 'CLOSED');
INSERT INTO TERM
VALUES (2, 'Summer 2004', 'OPEN');
CREATE TABLE STUDENT
(
Sid NUMBER(5) CONSTRAINT PK_STUDENT PRIMARY KEY,
Slname VARCHAR2(25),
Sfname VARCHAR2(25),
Smi CHAR(1),
Saddr VARCHAR2(30),
Scity VARCHAR2(30),
Sstate CHAR(2),
Szip NUMBER(9),
Sphone NUMBER(10),
Sclass CHAR(2)
Sdob DATE(),
S_pin NUMBER(4),
Fid NUMBER(4)
);
INSERT INTO STUDENT
VALUES (100, 'McClure', 'Sarah', 'M', '144 Windridge Blvd.', 'Eau Claire', 'WI', 54703, 7155559876, 'SR', '14-JUL-1979', 8891, 10);
INSERT INTO STUDENT
VALUES (101, 'Bowie', 'Jim', 'D', '454 St. John Street', 'Eau Claire', 'WI', 54702, 7155552345, 'SR', '19-AUG-1979', 1230, 11);
INSERT INTO STUDENT
VALUES (102, 'Boone', 'Daniel', NULL, '8921 Circle Drive', 'Bloomer', 'WI', 54715, 7155553907, 'JR', '10-OCT-1977', 1613, 11);
CREATE TABLE FACULTY
(
Fid NUMBER(4) CONSTRAINT PK_FACULTY PRIMARY KEY,
Flname VARCHAR2(25),
Ffname VARCHAR2(25),
Fmi CHAR(1),
Fphone NUMBER(10),
F_rank VARCHAR2(4),
F_pin NUMBER(4)
);
INSERT INTO FACULTY
VALUES (10, 'Cox', 'Kim', 'J', 7155551234, 'ASSO', 1181);
INSERT INTO FACULTY
VALUES (11, 'Blanchard', 'Frank', 'R', 7155559087, 'FULL', 1075);
INSERT INTO FACULTY
VALUES (12, 'McClure', 'William', 'L', 7155556409, 'ADJ', 1690);
CREATE TABLE LOCATION
(
Locid NUMBER(5) CONSTRAINT PK_LOCATION PRIMARY KEY,
Bldg_Code VARCHAR2(10),
Room VARCHAR2(6),
Capacity NUMBER(5)
);
INSERT INTO LOCATION
VALUES (53, 'BUS', 424, 45);
INSERT INTO LOCATION
VALUES (54, 'BUS', 404, 35);
INSERT INTO LOCATION
VALUES (55, 'BUS', 433, 100);
Commit;
You have missed the comma while creating the student table
Sphone NUMBER(10),
Sclass CHAR(2)
Sdob DATE(),
replace below code
Sphone NUMBER(10),
Sclass CHAR(2),
Sdob DATE(),
There is no parenthesis after the DATE data type; substitute this:
Sclass CHAR(2),
Sdob DATE,
S_pin NUMBER(4),
Also, you won't be able to insert into a table until it is created! Take care of the syntax error in the create statement, then you should be good.

difficulty inserting values in database

I have created my tables as shown below (I am using Oracle XE). The problem that occurs is that when I try to insert values into the tables, I get an error because the value of one's foreign key, has not been entered as a primary key of the other. For example, see tables DEPARTMENT and EMPLOYEE. Below, along with table creation code, is also the data inserting part. How to bypass that problem?
CREATE TABLE DEPARTMENT
(
DNAME VARCHAR(10),
DEPTID INTEGER NOT NULL,
MNG_IDNO INTEGER NOT NULL,
PRIMARY KEY (DEPTID)
);
CREATE TABLE EMPLOYEE
(
IDNO INTEGER NOT NULL,
FNAME VARCHAR(10) NOT NULL,
LNAME VARCHAR(10) NOT NULL,
GENDER VARCHAR(6) NOT NULL,
CITY VARCHAR(20),
AREA VARCHAR(20),
STREET VARCHAR(50),
HOUSENO VARCHAR(4),
ZIPCODE INTEGER,
BIRTHDAY DATE,
SALARY INTEGER,
IBAN VARCHAR(34),
DEPT_ID INTEGER NOT NULL,
PRIMARY KEY (IDNO)
);
CREATE TABLE CUSTOMER
(
IDNO INTEGER NOT NULL,
FNAME VARCHAR(10) NOT NULL,
LNAME VARCHAR(10) NOT NULL,
GENDER VARCHAR(6) NOT NULL,
CITY VARCHAR(20),
AREA VARCHAR(20),
STREET VARCHAR(50),
HOUSENO VARCHAR(4),
ZIPCODE INTEGER,
BIRTHDAY DATE,
PRIMARY KEY (IDNO)
);
CREATE TABLE CAR
(
VIN INTEGER NOT NULL,
BRAND VARCHAR(10) NOT NULL,
MODEL VARCHAR(10) NOT NULL,
COLOR VARCHAR(10) NOT NULL,
TRANTYPE VARCHAR(10) NOT NULL,
FUELTYPE VARCHAR(10) NOT NULL,
ENGCC INTEGER NOT NULL,
ENTRYDATE DATE,
PRICE INTEGER NOT NULL,
PAYMENTMETHOD VARCHAR(10) NOT NULL,
DEPT_ID INTEGER NOT NULL,
CUST_IDNO INTEGER NOT NULL,
PRIMARY KEY (VIN)
);
CREATE TABLE CAR_PARTS
(
PARTNUM VARCHAR(25) NOT NULL,
PARTNAME VARCHAR(25) NOT NULL,
COST INTEGER NOT NULL,
PRIMARY KEY (PARTNUM)
);
CREATE TABLE USED_CAR
(
CAR_VIN INTEGER NOT NULL,
MILEAGE INTEGER NOT NULL,
REGDATE DATE NOT NULL,
PRIMARY KEY (CAR_VIN)
);
CREATE TABLE SERVICE
(
DUE_DATE DATE,
ARR_DATE DATE,
COST INTEGER NOT NULL,
SERVICE_NUM INTEGER NOT NULL,
CUST_IDNO INTEGER NOT NULL,
PAYMENTMETHOD VARCHAR(10) NOT NULL,
CAR_VIN INTEGER NOT NULL,
PRIMARY KEY (SERVICE_NUM)
);
CREATE TABLE CUST_PHONUMBER
(
PHONE_NUM INTEGER NOT NULL,
CUST_IDNO INTEGER NOT NULL,
PHONE_TYPE VARCHAR(10),
PRIMARY KEY (CUST_IDNO, PHONE_NUM)
);
CREATE TABLE CAR_DEFECTS
(
DEFECTS VARCHAR(100) NOT NULL,
CAR_VIN VARCHAR(100) NOT NULL,
PRIMARY KEY (DEFECTS, CAR_VIN)
);
CREATE TABLE EMPLOYEE_PHONUMBER
(
PHONENUM INTEGER NOT NULL,
EMPL_IDNO INTEGER NOT NULL,
PRIMARY KEY (PHONENUM, EMPL_IDNO)
);
CREATE TABLE SERVICE_DETAILS
(
SERVICENUM INTEGER NOT NULL,
PROBLEMS VARCHAR(100) NOT NULL,
SOLUTION VARCHAR(100),
PRIMARY KEY (PROBLEMS, SERVICENUM)
);
CREATE TABLE HAS
(
PART_NUM VARCHAR(25) NOT NULL,
CAR_VIN INTEGER NOT NULL,
PRIMARY KEY (PART_NUM, CAR_VIN)
);
CREATE TABLE USES
(
PART_NUM VARCHAR(25) NOT NULL,
SERVICE_NUM INTEGER NOT NULL,
PRIMARY KEY (PART_NUM, SERVICE_NUM)
);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY (MNG_IDNO) REFERENCES EMPLOYEE(IDNO);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT(DEPTID);
ALTER TABLE CAR ADD FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT(DEPTID);
ALTER TABLE CAR ADD FOREIGN KEY (CUST_IDNO) REFERENCES CUSTOMER(IDNO);
ALTER TABLE SERVICE ADD FOREIGN KEY (CUST_IDNO) REFERENCES CUSTOMER(IDNO);
ALTER TABLE SERVICE ADD FOREIGN KEY (CAR_VIN) REFERENCES CAR(VIN);
ALTER TABLE USED_CAR ADD FOREIGN KEY (CAR_VIN) REFERENCES CAR(VIN);
ALTER TABLE HAS ADD FOREIGN KEY (PART_NUM) REFERENCES CAR_PARTS(PARTNUM);
ALTER TABLE HAS ADD FOREIGN KEY (CAR_VIN) REFERENCES CAR(VIN);
ALTER TABLE USES ADD FOREIGN KEY (PART_NUM) REFERENCES CAR_PARTS(PARTNUM);
ALTER TABLE USES ADD FOREIGN KEY (SERVICE_NUM) REFERENCES SERVICE(SERVICE_NUM);
ALTER TABLE CUSTOMER_PHONUMBER ADD FOREIGN KEY (CUST_IDNO) REFERENCES CUSTOMER(IDNO);
ALTER TABLE EMPLOYEE_PHONUMBER ADD FOREIGN KEY (EMPL_IDNO) REFERENCES EMPLOYEE(IDNO);
ALTER TABLE SERVICE_DETAILS ADD FOREIGN KEY (SERVICENUM) REFERENCES SERVICE(SERVICE_NUM);
Data Insert:
INSERT INTO EMPLOYEE VALUES (1234,'ANTREAS','GEORGIOU','MALE','NICOSIA','STROVOLOS','PANAYIAS','12',2682,TO_DATE('01.01.1989', 'DD.MM.YYYY'),5000,9999999,1);
INSERT INTO EMPLOYEE VALUES (1235,'KOSTAS', 'KOSTA', 'MALE', 'NICOSIA','STROVOLOS','VIZANTIOU','12',2064,TO_DATE('01.02.1980', 'DD.MM.YYYY'),5000,9999998,2);
INSERT INTO EMPLOYEE VALUES (1236,'MARIA', 'ANTREOU', 'FEMALE', 'NICOSIA','EGKOMI','TSAROU','12',2522,TO_DATE('05.05.1988', 'DD.MM.YYYY'),5000,9999995,3);
INSERT INTO EMPLOYEE VALUES (1237,'GEORGIA', 'ANTREOU', 'FEMALE', 'NICOSIA','EGKOMI','TSAROU','13',2522,TO_DATE('05.05.1978', 'DD.MM.YYYY'),5000,9999996,4);
INSERT INTO DEPARTMENT VALUES ('SERVICE',1,1234);
INSERT INTO DEPARTMENT VALUES ('SALES',2,1235);
INSERT INTO DEPARTMENT VALUES ('ACCOUNTING',3,1236);
INSERT INTO DEPARTMENT VALUES ('MANAGEMENT',4,1237);
INSERT INTO CUSTOMER VALUES (4321, 'ANTREAS', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2525,TO_DATE('04.04.1954','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4322, 'MARIA', 'ANTREOU','FEMALE','PAFOS','GEROSKIPOU','VIZANTIOU','7',2525,TO_DATE('03.03.1953','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4323, 'KOSTAS', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2525,TO_DATE('04.04.1970','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4324, 'ELENA', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2252,TO_DATE('04.04.1985','DD.MM.YYYY'));
INSERT INTO CUSTOMER VALUES (4325, 'MARIOS', 'ANTREOU','MALE','PAFOS','GEROSKIPOU','VIZANTIOU','3',2525,TO_DATE('04.04.1987','DD.MM.YYYY'));
INSERT INTO CAR VALUES (1111111111, 'MAZDA', '3', 'BLUE', 'MANUAL', 'PETROL', 1600, TO_DATE('08.01.2013','DD.MM.YYYY'), 5000, 'CASH', 2, 4321);
INSERT INTO CAR VALUES (2222222222, 'TOYOTA', 'COROLLA','BLACK', 'MANUAL','PETROL',1600,TO_DATE('08.01.2013','DD.MM.YYYY'),7000,'FINANCE',2,4322);
INSERT INTO CAR VALUES (3333333333, 'TOYOTA', 'HILUX', 'GRAY', '4X4', 'DIESEL', 2500, TO_DATE('08.01.2013','DD.MM.YYYY'), 10000, 'CASH', 2 , 4325);
INSERT INTO CAR VALUES (4444444444, 'HONDA', 'CIVIC', 'BLACK', 'MANUAL', 'PETROL', 1300, TO_DATE('08.01.2013','DD.MM.YYYY'), 3000,'CASH', 2, 4323);
INSERT INTO CAR VALUES (5555555555, 'HONDA', 'INSIGHT', 'WHITE', 'AUTO', 'PETROL', 1500, TO_DATE('08.01.2013','DD.MM.YYYY'), 20000, 'FINANCE', 2, 4324);
INSERT INTO CAR_PARTS VALUES ('A20005B', 'RIGHT FRONT AXLE', 200);
INSERT INTO CAR_PARTS VALUES ('C15220C', 'AIR FILTER', 50);
INSERT INTO CAR_PARTS VALUES ('V99T', 'ENGINE GASKET', 80);
INSERT INTO CAR_PARTS VALUES ('A20004B', 'LEFT FRONT AXLE', 200);
INSERT INTO CAR_PARTS VALUES ('U9P002', 'INTAKE MANIFOLD', 300);
INSERT INTO USED_CAR VALUES (4444444444, 25000, TO_DATE('02.02.2000','DD.MM.YYYY'));
INSERT INTO USED_CAR VALUES (5555555555, 122000, TO_DATE('05.08.2003','DD.MM.YYYY'));
INSERT INTO SERVICE VALUES (TO_DATE('12.01.2013','DD.MM.YYYY'), TO_DATE('01.01.2013','DD.MM.YYYY'), 300, 0001, 4321, 'CASH', 1111111111);
INSERT INTO CUST_PHONUMBER VALUES (99123456, 4321, 'MOBILE');
INSERT INTO CUST_PHONUMBER VALUES (99849563, 4322, 'MOBILE');
INSERT INTO CUST_PHONUMBER VALUES (22568988, 4323, 'LANDLINE');
INSERT INTO CUST_PHONUMBER VALUES (25698974, 4324, 'LANDLINE');
INSERT INTO CUST_PHONUMBER VALUES (97584692, 4325, 'MOBILE');
INSERT INTO CAR_DEFECTS VALUES ('BROKEN SIDE MIRRORS|OIL LEAKS', 4444444444);
INSERT INTO CAR_DEFECTS VALUES ('RUSTY PAINT|WORN-OUT CLUTCH', 5555555555);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (97989565, 1234);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (97585858, 1235);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (96525263, 1236);
INSERT INTO EMPLOYEE_PHONUMBER VALUES (97484852, 1237);
INSERT INTO SERVICE_DETAILS VALUES (0001, 'BROKEN FRONT RIGHT AXLE', 'REPLACED FRONT RIGHT AXLE');
INSERT INTO HAS VALUES ('A20005B', 1111111111);
INSERT INTO HAS VALUES ('A20004B', 1111111111);
INSERT INTO USES VALUES ('A20005B', 0001);