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.
Related
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.
I'm relatively new to SQL. I was wondering how I would go about adding data to my foreign keys. I have gotten errors with everything I've tried and google hasn't been much help... also, please ignore the nonsense data I've added.
Thank you!
Here's what I'm starting with:
CREATE TABLE GAME
(
GAME_ID INT NOT NULL AUTO_INCREMENT,
GAME_NAME VARCHAR(20) NOT NULL,
PLATFORM_ID INT NOT NULL,
PUBLISHER_ID INT NOT NULL,
DEV_ID INT NOT NULL,
GAME_GENRE VARCHAR(20),
PRIMARY KEY(GAME_ID)
);
CREATE TABLE DEVELOPER
(
DEV_ID INT NOT NULL AUTO_INCREMENT,
DEV_NAME VARCHAR(20) NOT NULL,
DEV_CIY VARCHAR(20),
DEV_COUNTRY VARCHAR(20),
DEV_CEO VARCHAR(20),
DEV_GAMES INT,
PRIMARY KEY(DEV_ID)
);
CREATE TABLE PUBLISHER
(
PUBLISHER_ID INT NOT NULL AUTO_INCREMENT,
PUBLISHER_NAME VARCHAR(20) NOT NULL,
DEV_ID INT NOT NULL,
PUBLISHER_CEO VARCHAR(20),
PUBLISHER_GAMES INT,
PUBLISHER_EMPLOYEES INT,
PRIMARY KEY(PUBLISHER_ID)
);
CREATE TABLE EMPLOYEE
(
EMPLOYEE_ID INT NOT NULL AUTO_INCREMENT,
EMPLOYEE_NAME VARCHAR(20) NOT NULL,
EMPLOYEE_ROLE VARCHAR(20) NOT NULL,
DEV_ID INT NOT NULL,
GAME_ID INT NOT NULL,
EMPLOYEE_NUM CHAR(10),
PRIMARY KEY(EMPLOYEE_ID)
);
CREATE TABLE GAMECREDITS
(
GAMECREDITS_ID INT NOT NULL AUTO_INCREMENT,
GAME_ID INT NOT NULL,
PUBLISHER_ID INT NOT NULL,
DEV_ID INT NOT NULL,
GAMECREDITS_YEAR INT,
PLATFORM_ID INT NOT NULL,
PRIMARY KEY(GAMECREDITS_ID)
);
CREATE TABLE PLATFORM
(
PLATFORM_ID INT NOT NULL AUTO_INCREMENT,
PLATFORM_PC BOOLEAN,
GAME_ID INT NOT NULL,
PLATFORM_PRICE NUMERIC,
PLATFORM_XBOOX BOOLEAN,
PLATFORM_SWOOTCH BOOLEAN,
PRIMARY KEY(PLATFORM_ID)
);
ALTER TABLE GAME
ADD FOREIGN KEY (PLATFORM_ID) REFERENCES PLATFORM(PLATFORM_ID),
ADD FOREIGN KEY (PUBLISHER_ID) REFERENCES PUBLISHER(PUBLISHER_ID),
ADD FOREIGN KEY (DEV_ID) REFERENCES DEVELOPER(DEV_ID);
ALTER TABLE PUBLISHER
ADD FOREIGN KEY (DEV_ID) REFERENCES DEVELOPER(DEV_ID);
ALTER TABLE EMPLOYEE
ADD FOREIGN KEY (DEV_ID) REFERENCES DEVELOPER(DEV_ID),
ADD FOREIGN KEY (GAME_ID) REFERENCES GAME(GAME_ID);
ALTER TABLE GAMECREDITS
ADD FOREIGN KEY (GAME_ID) REFERENCES GAME(GAME_ID),
ADD FOREIGN KEY (PUBLISHER_ID) REFERENCES PUBLISHER(PUBLISHER_ID),
ADD FOREIGN KEY (DEV_ID) REFERENCES DEVELOPER(DEV_ID),
ADD FOREIGN KEY (PLATFORM_ID) REFERENCES PLATFORM(PLATFORM_ID);
ALTER TABLE PLATFORM
ADD FOREIGN KEY (GAME_ID) REFERENCES GAME(GAME_ID);
INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_GENRE)
VALUES
(001, 'GTA Sons Andres', 'Sand-Box'),
(002, 'Not Destiny', 'FPS'),
(003, "Red Alive Redemption", 'TPS'),
(004, 'Animal Passing', 'Simulation'),
(005, 'Knights Creed', 'Action'),
(006, 'Simpsins hit and run', 'Action');
INSERT INTO DEVELOPER
(DEV_ID, DEV_NAME, DEV_CITY, DEV_COUNTRY, DEV_CEO, DEV_GAMES)
VALUES
(010, 'Kawcho Games', 'Texas', 'usa', 'Joe Houston', 6),
(020, 'NotStar Games', 'Vancouver', 'canada' 'Sam Haser', 7),
(030, 'Boungie', 'New York', 'usa' 'Pete Larson', 3),
(040, 'Pinnaple Games', 'Halifax', 'canada', 'Sam Haser', 6),
(050, 'Noontendo', 'Toronto', 'canada', 'Bowser Doug', 10),
(060, 'Oobiesoft', 'Washington', 'usa', 'Larry Seinfield', 5);
INSERT INTO PUBLISHER
(PUBLISHER_ID, PUBLISHER_NAME, PUBLISHER_CEO, PUBLISHER_GAMES, PUBLISHER_EMPLOYEES)
VALUES
(111, 'Kawcho Publishing', 'Cassie Haley', 44, 20),
(222, 'Walmort Publishers', 134, 'Perry Plat', 45, 23),
(333, 'Kents Publishing', 156, 'Linda Frend', 23, 15),
(444, 'Coffee House', 145, 'Susy Paige', 45, 15),
(555, 'Off the ground publishing', 189, 'Kerry Doug', 60, 25),
(666, 'yourWay', 1, 'beff, jezos', 46, 25);
INSERT INTO EMPLOYEE
(EMPLOYEE_ID, EMPLOYEE_NAME, EMPLOYEE_ROLE)
VALUES
(100, 'Fiacre Julen', 'Ui design'),
(200, 'Sebastian Aneta', 'Animator'),
(300, 'Xavia Guy', 'Developer'),
(400, 'Oakley Jillie', 'Character Design'),
(500, 'Keaton Lula', 'Marketing'),
(600, 'Liv Randell', 'Developer');
INSERT INTO GAMECREDITS
(GAMECREDITS_ID, GAMECREDITS_YEAR)
VALUES
(101, 2012),
(102, 2020),
(103, 1999),
(104, 2015),
(105, 2017),
(106, 2019);
INSERT INTO PLATFORM
(PLATFORM_ID, PLATFORM_PC, PLATFORM_PRICE, PLATFORM_XBOOX, PLATFORM_SWOOTCH);
VALUES
(123, FALSE, 49.99, TRUE, FALSE),
(145, TRUE, 25.00, FALSE, FALSE),
(167, TRUE 26.99, FALSE, FALSE),
(189, FALSE, 12.49, FALSE, TRUE),
(134, FALSE, 69.99, FALSE, TRUE),
(156, FALSE, 36.00, TRUE, FALSE);
I appreciate any help I can get as this is relating to a final assignment of mine. The only solutions I've found through google and my teacher would take up like 60 update queries to fill in my tables.
Give me any advice you can :)
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?
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.
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);