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 :)
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.
create table Interventions
(
InterventionID BIGINT IDENTITY (1,1) PRIMARY KEY NOT NULL,
InterventionCustomerID BIGINT FOREIGN KEY REFERENCES Customer(CustomerID) NOT NULL,
InterventionMalfunctionDescription NVARCHAR(MAX) NOT NULL,
CustomerID BIGINT FOREIGN KEY REFERENCES Customer(CustomerID),
EmployeeID INT FOREIGN KEY REFERENCES Employees(EmployeeID),
TypeOfEquipementID INT FOREIGN KEY REFERENCES TypeOfEquipements(TypeOfEquipementID),
StatusID INT FOREIGN KEY REFERENCES TypeOfStatus(StatusID),
SerialNumber VARCHAR(255) FOREIGN KEY REFERENCES SerialNumbers(SerialNumber),
GroupID INT FOREIGN KEY REFERENCES TypeOfGroup(GroupID) NOT NULL,
InterventionCreateDate DATETIME NOT NULL,
InterventionStartDate DATETIME,
InterventionFinnishDate DATETIME,
InterventionArchiveDate DATETIME
);
create table Customer
(
CustomerID BIGINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
OrganizationName VARCHAR(255) NOT NULL,
Telephone VARCHAR(255),
MobileTelephone VARCHAR(255) NOT NULL,
CustomerAdressLine1 VARCHAR(255) NOT NULL,
CustomerAdressLine2 VARCHAR(255),
PostalCode VARCHAR(255) NOT NULL,
FederalTaxID VARCHAR(255) UNIQUE NOT NULL,
EmailAddress VARCHAR(255) UNIQUE NOT NULL,
SageCustomerID BIGINT UNIQUE
);
INSERT INTO Interventions(InterventionCustomerID, InterventionMalfunctionDescription, CustomerID, EmployeeID, TypeOfEquipementID, StatusID, SerialNumber, GroupID, InterventionCreateDate, InterventionStartDate, InterventionFinnishDate, InterventionArchiveDate) VALUES
('1', 'abc', '4', '4', '1', '1', 'SerialNumber1', '1', '2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04'),
('2', 'abc', '5', '5', '2', '2', 'SerialNumber2', '2', '2019-02-01', '2019-02-02', '2019-02-03', '2019-02-03'),
('3', 'abc', '6', '6', '3', '3', 'SerialNumber3', '3', '2019-03-01', '2019-03-02', '2019-03-03', '2019-03-04');
Select * from Customer;
Erro:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Intervent__Inter__6C190EBB". The conflict occurred in database "ADSGLOBAL", table "dbo.Customer", column 'CustomerID'.
You have a constraint on InterventionCustomerID that says it references Customer(CustomerID).
Your insert statement says you're trying to insert the values 1, 2, 3 in to that column.
Your SELECT * FROM customer shows that customers 1, 2, 3 don't exist in that table.
You can't insert records for customers 1, 2, 3 until you've created them in the Customer table.
create table STAFF
(
StaffID TINYINT IDENTITY NOT NULL,
StaffName varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk
foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk
foreign key (Mentor) references staff(StaffID)
);
Table was created successfully. But when I'm going to insert values like this:
insert into STAFF
values ('Adeesha', '077282018', 'M', '1997-11-30', '', '5'),
('Kavitha', '0772556899', 'F', '1956-11-28', '', '4'),
('Patee Aiya', '0775669844', 'M', '1954-01-04', '', '3'),
('Chanuka', '0772562984', 'M', '1997-02-24', '', '2'),
('Umesha', '0723328284', 'F', '1997-11-26', '', '3');
I get the following error:
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "mentor_fk".
The conflict occurred in database "F_T", table "dbo.STAFF", column 'StaffID'.
An empty string ('') is not the same as NULL. You should use NULL:
insert into STAFF(StaffName, Phone, Gender, DoB, Mentor, Payment_ID)
values ('Adeesha', '077282018', 'M', '1997-11-30', NULL, 5),
('Kavitha', '0772556899', 'F', '1956-11-28', NULL, 4),
('Patee Aiya', '0775669844', 'M', '1954-01-04', NULL, 3),
('Chanuka', '0772562984', 'M', '1997-02-24', NULL, 2),
('Umesha', '0723328284', 'F', '1997-11-26', NULL, 3);
Notes:
When using insert, list all the columns being inserted into the table. This is a best-practice.
Do not insert into the identity column. It is generated automatically.
Use NULL to mean NULL rather than an empty string. They are not the same.
If a value is a number, do not enclose it in single quotes.
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.
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);